Email notifications conditions with loop

I need an email notification when particular item is quoted.

I was thinking of the following but am not allowed to save. Looks like my syntax is wrong.

for item in doc.items
    item.item_code=="SKU001"

Need advice on how to do it correctly.

Hi @pvanthony:

Is not possible as far I know.

conditions just can use frappe.utils methods and simple eval code.

You will need to write a simple server script for your requirement.

Hope this helps.

Thank you for the reply.

I will search the internet for writing server scripts. Hope I can find. :slight_smile:

You can do this using the server script with frappe.sendmail and set the condition according to the scenario. please check the simple reference and set your logic on “After Save” or “After Submit”.

1 Like

Thank you for sharing the code. It is very useful.

I will try it out.

recipients = ['email@example.com']
cc = ['email2@example.com, 'email3@example.com']
bcc = ['email3@example.com']

SendEmail = frappe.sendmail( 
    recipients=recipients,
    cc = cc,
    bcc = bcc,
    subject = 'T-Shirt in the quotation',
    content = 'Please call the client for T-shirt details'
    )

for item in doc.items:
    if item.item_code=="SKU001":
        SendEmail

Modified your code to this one for testing and it works!!!
Thank you very much for sharing the code.

Just incase someone comes to this page, the reason for this is to notify the team when someone orders a particular product so that they can followup with the client to get more details. Like if the product is painting the house.

Please add the break:

recipients = ['email@example.com']
cc = ['email2@example.com', 'email3@example.com']
bcc = ['email3@example.com']

for item in doc.items:
    if item.item_code == "SKU001":
        frappe.sendmail(
            recipients=recipients,
            cc=cc,
            bcc=bcc,
            subject='T-Shirt in the quotation',
            content='Please call the client for T-shirt details'
        )
        break  # Stop after sending the email once if the item is found

Otherwise the mail will go multiple times if the item is multiple or same.

Yes I was getting multiple emails!
I will add the “break”.

Thank you again for sharing the code.

recipients = ['email@example.com']
cc = ['email2@example.com', 'email3@example.com']
bcc = ['email3@example.com']
# call an email templete in erpnext
email_template = frappe.get_doc("Email Template", "Notify when T-Shirt in quotation")

# render the email template as it has variables
message = frappe.render_template(email_template.response_html, {'doc': doc})

SendEmail = frappe.sendmail( 
    recipients=recipients,
    cc = cc,
    bcc = bcc,
    subject = 'T-Shirt in the quotation',
    content = message
    )

for item in doc.items:
    if item.item_code=="SKU001":
        SendEmail
        break

The above code is modified from the screenshot in the above messages.
This will send email with quotation data, like customer name, mobile number and so on. It will be in html format too.

<h1>Testing html email</h1>
Customer Name: {{ doc.customer_name }}<br>
Customer Mobile: {{ doc.contact_mobile }}<br>
Quotation Number: {{ doc.name }}<br>
Title: {{ doc.title }}<br>
Naming Series: {{ doc.naming_series }}<br>

The above is the email template

Now I wonder how to create a task in the a project, when there is T-Shirts in a quotation. :slight_smile:

Looks like the following discussion could be the solution.
https://discuss.frappe.io/t/auto-create-task/116616

today = frappe.utils.today()
tomorrow = frappe.utils.add_days(today,1)

task = frappe.get_doc({
    "doctype": "Task",
    "subject": "New task",
    "status": "Open",
    "exp_start_date": today,
    "exp_end_date": tomorrow
})

task.insert()

The above is by @avc
Thank you for sharing the code.

How to make the task into a specific Project?

Found it. Just add project: project id. Here is the final code.
Please note I am not an expert and there might be hidden bugs. Or the code is not optimal.

recipients = ['email@example.com']
cc = ['email2@example.com', 'email3@example.com']
bcc = ['email3@example.com']
email_template = frappe.get_doc("Email Template", "Notify when T-Shirt in quotation")
message = frappe.render_template(email_template.response_html, {'doc': doc})

SendEmail = frappe.sendmail( 
    recipients=recipients,
    cc = cc,
    bcc = bcc,
    subject = 'T-Shirt in the quotation',
    content = message
    )
    
# For adding a task to T-Shirt project
today = frappe.utils.today()
fiveDays = frappe.utils.add_days(today,5)

task = frappe.get_doc({
    "doctype": "Task",
    "subject": "New T-Shirt Quotation",
    "project": "PROJ-0001",
    "status": "Open",
    "exp_start_date": today,
    "exp_end_date": fiveDays
})

for item in doc.items:
    if item.item_code=="SKU001":
        SendEmail
        task.insert()
        break

Thank you all for sharing the code. Otherwise there now way to do this by myself.
Thank you again.