Sending Email in Frappe Framework
Simply Send Email
frappe.sendmail(
recipients=[doc.email_id, doc.lead_owner],
subject='New Lead Created: ' + doc.name,
message=f'<p>Hello, a new lead has been created for {doc.company}.</p>', # Use f-strings for dynamic content
attachments=[
frappe.attach_print(
doc.doctype, doc.name,
file_name=f"{doc.doctype} - {doc.name}",
#print_format=print_format # Optional, will use standard print format if not specified
)
], # Optional: attach a PDF of the document
#sender='your_email@example.com', # Optional, uses default if not specified
#cc=['maythu@bcncl.com', 'thida@bcncl.com'], # Optional, CC
#delayed=0 #Optional, send email directaly or to email queue. default is 1
)
Enqueue and Send Email After
Server Script - API:
# API - Send Lead Creation Email (send_lead_creation_email)
doc = args.doc
delayed = 0 if args.delayed==0 else 1
doc = args.doc
attachments = [
frappe.attach_print(
doc.doctype, doc.name, file_name=f"{doc.doctype} - {doc.name}", print_format="Lead Print Format"
)
]
frappe.sendmail(
recipients=[doc.lead_owner],
subject='New Lead Created: ' + doc.name,
message=f'<p>Hello, a new lead has been created for {doc.company} by.</p>', # Use f-strings for dynamic content
attachments = attachments, # Optional: attach a PDF of the document
#sender='your_email@example.com', # Optional, uses default if not specified
#cc=[doc.email_id], # Optional, CC
#print_html=message, #Optional, Use this if you want the message to be the main body
delayed=delayed #Optional, send email directaly or to email queue. default is 1
)
Server Script - Document Event:
# Document Event - Lead - After Insert
# Enqueue the proccess
frappe.enqueue("send_lead_creation_email", doc=doc.as_dict(), now=False, enqueue_after_commit=True)
# OR send mail directly
run_script("Send Lead Creation Email", doc=doc.as_dict(), delayed=0)
Last updated 2 months ago
Was this helpful?