Email Script to send Welcome Email to Non System Users

Hi, I have a custom Doctype called Linked Third Party. It is basically a form with contact details, now I want to send an email based off of an Email Template (With Attachments) to an email within that form each time I save it. Let’s say my form has the email abc@abc.com, I want to send an email to this email when I save the form. I have been studying the user.py file and the hooks.py file to no avail.

I have two main issues:

  1. How to automate email each time the form is saved
  2. How to add attachments to Email Templates

Any help is appreciated, thanks in advance.

I have the same issue, I need help too

Hi @Vesper_Solutions,
regarding issue #1, you could make use of a Notification - I am not aware, if you can make use of E-Mail Templates within Notifications. But for sure, you can just add your custom HTML within that notification: Notification

You can use notification for that.

In notification you have to select your doctype as you mentioned ’ Linked Third Party’ and then select event after save.

In below one child table is there where you can select your email field where you want to send email.

If that field not available in selection then go to your doctype and select opetion as ‘Email’ for your email field so you will get that field in recipient child table of notification.

Hope this will solve your problem.

I can do that yes but that is not what I want to do. The email I wish to send (automatically) is not to a user of the app (a system user) but an external email. In the doctype Linked Third Party is a field called email (not for a system user). I wish to send an email to the address from the email field.

Thanks but notifications are for system users. The email address I want to send the mail to is not of system users but rather external emails saved within a doctype

Recipient email not must be system user that can anything.

I am not able to get what exactly you are facing problem?

If you can some screenshots then I can help you.

I aim to use the frappe.sendmail functionality, I read through this thread: Attachments with frappe.sendmail
to no avail. I did come up with this code, which does not work unfortunately. Basically I have a doctype called Secondary Sources. There is an email address in that form and I wish to send an email to that address each time a new Secondary Source document is created and saved. Here’s my code :

def sendmail():         
    content = "Test Content"
    recipient = self.email
    frappe.sendmail(recipients=[recipient],
        sender="xyz@gmail.com",
        subject="Test Subject", content=content)

I did not add this code in the .py file but rather created a server side script within ERPNext and added this code

But for that you do not need to write any script.

You can use notification for that.

Yes but I also wish to email attachments, notifications dont let me do that

This script might help you:

def send_email_to_party(self):
content = frappe.render_template('yourappname/templates/emails/email_template.html', {
		'recipient_name': self.recipient_name,
		'email': self.email
	    #put here the variables in the template
	})

	frappe.sendmail(
		recipients = self.email, #this is the email to your recipient as you want
		sender = "your_sender_email",
		subject = "The subject of your email",
		content = content,
		now = True
	)

Here are more parameters in frappe.sendmail(). Actually there are more but I don’t use them and can’t remember them :slight_smile:

recipients
cc
bcc
sender
subject
content
reply_to
reference_doctype
reference_name
header
now
1 Like

Actually, as I read somewhere, you can also send email via the Communication doctype and notification:

from frappe.core.doctype.communication.email import make, notify

def make_email(self, set, content_):
	make(
		doctype = 'Linked Third Party', 
		name = self.name, 
		content = content, 
		subject = "subject",
		sent_or_received = "Sent", 
		sender = sender_email,
		sender_full_name = sender_name,
		recipients = self.email, 
		communication_medium = "Email", 
		send_email = True, 
		cc = cc_email,
		print_letterhead = True, 
		email_template = None,
		reference_doctype = self.doctype,
		reference_name = self.name,
		read_receipt = True
	)

def	send_notif(self):
    notify(doc=, print_html=None, print_format=None, attachments=None, recipients=recipients, cc=cc, bcc=None, fetched_from_email_account=False)

But I can’t make it work.
So I prefer using the sendmail. And if necessary, do frappe.new_doc('Communication') to make a new Communication doctype when sending email.

1 Like

Just what I needed. TYSM :smiley:

Although I am a bit curios. Is there any way to put the sendmail function through a loop. Let’s say there is a child table with email fields, can I somehow send emails to all emails from the rows of the child table?

Honestly I don’t know.
But maybe you can put sendmail inside the loop instead of only looping the email inside sendmail.
One concern though, because sending email takes time, perhaps better to put sendmail in enqueue and have the background job send them.
Or if I’m not mistaken, using make (Communication) does put the email in que jobs.
Well, I’m not programmer so I might be wrong. And I intend to learn also if somebody has the answer.