Send Email Programmatically

Hi,

Is there any method / function to sent email programmatically?

Yes, check email rules and email alerts. That should do what you need?

@charlie-cook thanks for your reply but Email Alert or Email Rules won’t work in my case, i need python function to send email for sending email to the address which are not pre-defined.

Anybody please?

You can use frappe.sendmail() method, it is used vast over frappe app just check with it

1 Like

https://github.com/frappe/frappe/blob/960d56842749a85aeb3b188560f796e0ad082d9b/frappe/init.py#L397

2 Likes

@hereabdulla @magic-overflow many thanks of both of you,

for viewers :
here is method of sending emails,

def send_email(email_address):
	frappe.sendmail(recipients=email_address,
		subject="Subject of the email",
		message= "Content of the email"
2 Likes

Hey @shahid
How can we add particular date to send ,from python code

Sorry i didn’t get you, can you please elaborate?

Hello,
If your Email account is successfully linked you can use this method

def send_notifications(self):
from frappe.core.doctype.communication.email import make
msg="Your msg here"

try:
    make(subject = "Email Subject", content=msg, recipients='EmployeeEmail@example.com',
        send_email=True, sender="senderEmail lined in the email account")
    
    msg = """Email send successfully to Employee"""
    frappe.msgprint(msg)
except:
    frappe.msgprint("could not send")
1 Like

You can use the scheduler to send the email notification on any specific date and time.

inside hooks file put the scheduler entry for your email function.

Here’re some options for scheduler, means how frequently you can send the email:

Events

daily
daily_long
weekly
weekly_long
monthly
monthly_long
hourly
all

Frappe framework has following function to compare the dates and times and use these function to compare the date

frappe.utils.nowtime 
frappe.utils.now
frappe.utils.nowdate        
frappe.utils.now_datetime 
frappe.utils.nowtime        
scheduler_events = {
    "daily": [
        "erpnext.accounts.doctype.sales_invoice.sales_invoice.manage_recurring_invoices"
    ],
    "daily_long": [
        "erpnext.setup.doctype.backup_manager.backup_manager.take_backups_daily"
    ],
    "cron": {
        "15 18 * * *": [
            "frappe.twofactor.delete_all_barcodes_for_users"
        ],
        "*/6 * * * *": [
            "frappe.utils.error.collect_error_snapshots"
        ],
        "annual": [
            "frappe.utils.error.collect_error_snapshots"
        ]
    }
}

https://frappe.io/docs/user/en/guides/basics/hooks

3 Likes

Thanks for your response,

I would like to send email to suppliers one day before request date in purchase order.

and One more condition
If requested date is on monday, then mail should not be sent on sunday instead it should be sent on saturday

The ‘cron’ expression can specify what days of the week to run, so then 0-6 would omit Sunday https://www.computerhope.com/unix/ucrontab.htm

So in your case Sunday and Monday notices would both be scheduled to send out Saturday.

1 Like

Hi Omar,

By looking at the code, make() will create new doc of communication.
But I can’t figure out if it also automatically send email?
Will it put sending email also in enqueue?

Which part of make() that send email, is it this ?

if cint(send_email):
    frappe.flags.print_letterhead = cint(print_letterhead)
    comm.send(print_html, print_format, attachments, send_me_a_copy=send_me_a_copy)

I also can’t find any documentation about frappe.sendmail.
Can anyone suggest?

FYI, my scenario is to send notification email after (guest) user fill in a webform.
Thanks in advance for your or anyone comment.

yes this is the part of sending emails

by the way, I am using now frappe.sendmail function for emails issue

So… to have a record of email sent:

  • using frappe.sendmail
  • and create comm doc

?

Did you figured it out?

I also use frappe.sendmail. Simpler to figure out.