ERPNext Foundation ERPNext Cloud Chat Blog Discuss Frappé* Donate

[ Achieved ]Frappe.sendmail : Send email using scheduler with attachments

email
account
billing
customization
attachments

#1

Hello Community,

I am working on scheduler task in which i want to sent an email on every sunday to the customer whose payment ( Sales Invoice ) are Overdue.

I have done with auto emailing, but how can i attach file in email ?

Like if Customer A has 4 Overdue Invoice then i want to attach all 4 invoice in single mail with template.

Thanks,
Hardik Gadesha


#2

“I have done with auto emailing, but how can i attach file in email”

Good question! You will need to study the existing code, to then develop what you require.

For eg there’s this function https://github.com/frappe/frappe/blob/develop/frappe/email/email_body.py#L145

A search on ‘attach_file’ will give you pointers to learn from others?


#3

Thanks For your kind words @clarkej

I have achieved this feature :slight_smile:


#4

@Hardik_Gadesha sharing the steps may help fellow community developers

Regards,
@hereabdulla


#5

Yes Sure @hereabdulla

  1. Collect all Overdue Invoice which has status Overdue.
  2. Prepare Content for mail.
  3. Loop through all invoice and adjust all data in content.
  4. Fetch customer email id.
  5. Use frappe.sendmail to send mail.

I hope you got it. Will post detailed solution soon.


#6

can you share the code of frappe.sendmail


#7
def sendmail():
user_list = frappe.db.sql("""select name, employee_name from `tabEmployee` where designation = "Batch Supervisor";""")
for user_obj in user_list:
    user = user_obj[0];
    employee = user_obj[1];
    content = "<h4>Hello, "+ employee +"</h4><p>Following Batch will be expiry soon. Please make a note for that.</p><table class='table table-bordered'><tr><th>Batch Number</th><th>Expiry Date</th></tr>"
    batch_list = frappe.db.sql("""select batch_id, expiry_date  from `tabBatch` where DATEDIFF(expiry_date, CURRENT_DATE()) < 30;""")
    for batch_obj in batch_list:
        batch = batch_obj[0]
        expiry_date = str(batch_obj[1].strftime('%d/%m/%Y'))
        content = content + "<tr><td>"+batch+"</td><td>"+expiry_date+"</td></tr>"         
    content = content + "</table>"
    recipient = frappe.get_value("Employee", user, "prefered_email")
    frappe.sendmail(recipients=[recipient],
        sender="xyz@gmail.com",
        subject="Item Batch To Be Expire", content=content)

#8

do i have to import any package to use frappe.sendemail?


#9

No. it’s just as it.

i have written this code in default python file which auto created on making of doctype.


#10

Thanks


#11

Is that possible to send email to supplier on a particular date by custom code?
Now i can send mail by custom code using "“frappe.sendmail”.
I want it to be sent on particular date. (One day before Requested date).


#12

you can use this script to get next day document and send email using this…

@frappe.whitelist()
def getData():
	try:
		request_doc=frappe.get_all("Purchase Order",filters={"schedule_date":add_days(today(),1)},fields=["name"])
		return request_doc
		if request_doc:
			for doc in request_doc:
				po_doc=frappe.get_doc("Purchase Order",doc.name)
				supplier_email=getSupplierEmail(po_doc.supplier)
                            #write email send logic#
				

	except Exception as e:
		return generateResponse("F",error=e)

@frappe.whitelist()
def getSupplierEmail(supplier):
	supplier_doc=frappe.get_all("Contact",filters=[["Dynamic Link","link_doctype","=","Supplier"],["Dynamic Link","link_name","=",supplier]],fields=["email_id"])
	return supplier_doc[0].email_id

also you can use email alert and set alert days before …


#13

Hey @Maheshwari_Bhavesh

Thanks. this one works fine for me.

Now i am calling this function “getData():” from “Purchase order onload”,But i want to call it daily without that onload trigger function ,for that some guys has sugegsted me to use hook.py

Can some one help me to call this function( getData():slight_smile: daily from hook.py?


#14
scheduler_events = {
	"daily": [
		"erpnext.stock.reorder_item.reorder_item"
	]
}

#15

i have called.the how to check that? Do i have to restart the bench?


#16

how did you send email on every sunday? by scheduler?

Can you please share that code?


#17

Use Cron Expression For This.

Use This Site to Genrate cron as per your requirment https://crontab.guru/#0_0___7