Beispiel #1
0
def add(email,
        sender,
        subject,
        formatted,
        text_content=None,
        ref_doctype=None,
        ref_docname=None,
        attachments=None,
        reply_to=None):
    """add to bulk mail queue"""
    e = frappe.new_doc('Bulk Email')
    e.sender = sender
    e.recipient = email

    try:
        e.message = get_email(email,
                              sender=e.sender,
                              formatted=formatted,
                              subject=subject,
                              text_content=text_content,
                              attachments=attachments,
                              reply_to=reply_to).as_string()
    except frappe.InvalidEmailAddressError:
        # bad email id - don't add to queue
        return

    e.ref_doctype = ref_doctype
    e.ref_docname = ref_docname
    e.insert(ignore_permissions=True)
Beispiel #2
0
def add(email, sender, subject, formatted, text_content=None,
	reference_doctype=None, reference_name=None, attachments=None, reply_to=None,
	cc=(), message_id=None, in_reply_to=None, send_after=None, bulk_priority=1, email_account=None, communication=None):
	"""add to bulk mail queue"""
	e = frappe.new_doc('Bulk Email')
	e.recipient = email
	e.priority = bulk_priority

	try:
		mail = get_email(email, sender=sender, formatted=formatted, subject=subject,
			text_content=text_content, attachments=attachments, reply_to=reply_to, cc=cc, email_account=email_account)

		if message_id:
			mail.set_message_id(message_id)

		if in_reply_to:
			mail.set_in_reply_to(in_reply_to)

		e.message = cstr(mail.as_string())
		e.sender = mail.sender

	except frappe.InvalidEmailAddressError:
		# bad email id - don't add to queue
		return

	e.reference_doctype = reference_doctype
	e.reference_name = reference_name
	e.communication = communication
	e.send_after = send_after
	e.insert(ignore_permissions=True)
Beispiel #3
0
def add(email, sender, subject, formatted, text_content=None,
	reference_doctype=None, reference_name=None, attachments=None, reply_to=None,
	cc=(), message_id=None, send_after=None, bulk_priority=1):
	"""add to bulk mail queue"""
	e = frappe.new_doc('Bulk Email')
	e.sender = sender
	e.recipient = email
	e.priority = bulk_priority

	try:
		mail = get_email(email, sender=e.sender, formatted=formatted, subject=subject,
			text_content=text_content, attachments=attachments, reply_to=reply_to, cc=cc)

		if message_id:
			mail.set_message_id(message_id)

		e.message = mail.as_string()

	except frappe.InvalidEmailAddressError:
		# bad email id - don't add to queue
		return

	e.reference_doctype = reference_doctype
	e.reference_name = reference_name
	e.send_after = send_after
	e.insert(ignore_permissions=True)
	def setUp(self):
		email_html = '''
<div>
	<h3>Hey John Doe!</h3>
	<p>This is embedded image you asked for</p>
	<img embed="assets/frappe/images/favicon.png" />
</div>
'''
		email_text = '''
Hey John Doe!
This is the text version of this email
'''

		img_path = os.path.abspath('assets/frappe/images/favicon.png')
		with open(img_path, 'rb') as f:
			img_content = f.read()
			img_base64 = base64.b64encode(img_content).decode()

		# email body keeps 76 characters on one line
		self.img_base64 = fixed_column_width(img_base64, 76)

		self.email_string = get_email(
			recipients=['*****@*****.**'],
			sender='*****@*****.**',
			subject='Test Subject',
			content=email_html,
			text_content=email_text
		).as_string()
    def setUp(self):
        email_html = '''
<div>
	<h3>Hey John Doe!</h3>
	<p>This is embedded image you asked for</p>
	<img embed="assets/frappe/images/frappe-favicon.svg" />
</div>
'''
        email_text = '''
Hey John Doe!
This is the text version of this email
'''

        img_path = os.path.abspath('assets/frappe/images/frappe-favicon.svg')
        with open(img_path, 'rb') as f:
            img_content = f.read()
            img_base64 = base64.b64encode(img_content).decode()

        # email body keeps 76 characters on one line
        self.img_base64 = fixed_column_width(img_base64, 76)

        self.email_string = get_email(
            recipients=['*****@*****.**'],
            sender='*****@*****.**',
            subject='Test Subject',
            content=email_html,
            text_content=email_text).as_string().replace("\r\n", "\n")
Beispiel #6
0
def get_email_queue(recipients, sender, subject, **kwargs):
	'''Make Email Queue object'''
	e = frappe.new_doc('Email Queue')
	e.priority = kwargs.get('send_priority')
	attachments = kwargs.get('attachments')
	if attachments:
		# store attachments with fid or print format details, to be attached on-demand later
		_attachments = []
		for att in attachments:
			if att.get('fid'):
				_attachments.append(att)
			elif att.get("print_format_attachment") == 1:
				att['lang'] = frappe.local.lang
				_attachments.append(att)
		e.attachments = json.dumps(_attachments)

	try:
		mail = get_email(recipients,
			sender=sender,
			subject=subject,
			formatted=kwargs.get('formatted'),
			text_content=kwargs.get('text_content'),
			attachments=kwargs.get('attachments'),
			reply_to=kwargs.get('reply_to'),
			cc=kwargs.get('cc'),
			bcc=kwargs.get('bcc'),
			email_account=kwargs.get('email_account'),
			expose_recipients=kwargs.get('expose_recipients'),
			inline_images=kwargs.get('inline_images'),
			header=kwargs.get('header'))

		mail.set_message_id(kwargs.get('message_id'),kwargs.get('is_notification'))
		if kwargs.get('read_receipt'):
			mail.msg_root["Disposition-Notification-To"] = sender
		if kwargs.get('in_reply_to'):
			mail.set_in_reply_to(kwargs.get('in_reply_to'))

		e.message_id = mail.msg_root["Message-Id"].strip(" <>")
		e.message = cstr(mail.as_string())
		e.sender = mail.sender

	except frappe.InvalidEmailAddressError:
		# bad Email Address - don't add to queue
		frappe.log_error('Invalid Email ID Sender: {0}, Recipients: {1}'.format(mail.sender,
			', '.join(mail.recipients)), 'Email Not Sent')

	e.set_recipients(recipients + kwargs.get('cc', []) + kwargs.get('bcc', []))
	e.reference_doctype = kwargs.get('reference_doctype')
	e.reference_name = kwargs.get('reference_name')
	e.add_unsubscribe_link = kwargs.get("add_unsubscribe_link")
	e.unsubscribe_method = kwargs.get('unsubscribe_method')
	e.unsubscribe_params = kwargs.get('unsubscribe_params')
	e.expose_recipients = kwargs.get('expose_recipients')
	e.communication = kwargs.get('communication')
	e.send_after = kwargs.get('send_after')
	e.show_as_cc = ",".join(kwargs.get('cc', []))
	e.show_as_bcc = ",".join(kwargs.get('bcc', []))
	e.insert(ignore_permissions=True)

	return e
Beispiel #7
0
	def setUp(self):
		email_html = """
<div>
	<h3>Hey John Doe!</h3>
	<p>This is embedded image you asked for</p>
	<img embed="assets/frappe/images/frappe-favicon.svg" />
</div>
"""
		email_text = """
Hey John Doe!
This is the text version of this email
"""

		img_path = os.path.abspath("assets/frappe/images/frappe-favicon.svg")
		with open(img_path, "rb") as f:
			img_content = f.read()
			img_base64 = base64.b64encode(img_content).decode()

		# email body keeps 76 characters on one line
		self.img_base64 = fixed_column_width(img_base64, 76)

		self.email_string = (
			get_email(
				recipients=["*****@*****.**"],
				sender="*****@*****.**",
				subject="Test Subject",
				content=email_html,
				text_content=email_text,
			)
			.as_string()
			.replace("\r\n", "\n")
		)
Beispiel #8
0
def add(email, sender, subject, formatted, text_content=None,
	reference_doctype=None, reference_name=None, attachments=None, reply_to=None,
	cc=(), message_id=None, in_reply_to=None, send_after=None, send_priority=1, email_account=None, communication=None):
	"""Add to Email Queue"""
	e = frappe.new_doc('Email Queue')
	e.recipient = email
	e.priority = send_priority

	try:
		mail = get_email(email, sender=sender, formatted=formatted, subject=subject,
			text_content=text_content, attachments=attachments, reply_to=reply_to, cc=cc, email_account=email_account)

		if message_id:
			mail.set_message_id(message_id)

		if in_reply_to:
			mail.set_in_reply_to(in_reply_to)

		e.message = cstr(mail.as_string())
		e.sender = mail.sender

	except frappe.InvalidEmailAddressError:
		# bad email id - don't add to queue
		return

	e.reference_doctype = reference_doctype
	e.reference_name = reference_name
	e.communication = communication
	e.send_after = send_after
	e.insert(ignore_permissions=True)
Beispiel #9
0
def get_email_queue(recipients, sender, subject, **kwargs):
	'''Make Email Queue object'''
	e = frappe.new_doc('Email Queue')
	e.priority = kwargs.get('send_priority')
	attachments = kwargs.get('attachments')
	if attachments:
		# store attachments with fid or print format details, to be attached on-demand later
		_attachments = []
		for att in attachments:
			if att.get('fid'):
				_attachments.append(att)
			elif att.get("print_format_attachment") == 1:
				_attachments.append(att)
		e.attachments = json.dumps(_attachments)

	try:
		mail = get_email(recipients,
			sender=sender,
			subject=subject,
			formatted=kwargs.get('formatted'),
			text_content=kwargs.get('text_content'),
			attachments=kwargs.get('attachments'),
			reply_to=kwargs.get('reply_to'),
			cc=kwargs.get('cc'),
			bcc=kwargs.get('bcc'),
			email_account=kwargs.get('email_account'),
			expose_recipients=kwargs.get('expose_recipients'),
			inline_images=kwargs.get('inline_images'),
			header=kwargs.get('header'))

		mail.set_message_id(kwargs.get('message_id'),kwargs.get('is_notification'))
		if kwargs.get('read_receipt'):
			mail.msg_root["Disposition-Notification-To"] = sender
		if kwargs.get('in_reply_to'):
			mail.set_in_reply_to(kwargs.get('in_reply_to'))

		e.message_id = mail.msg_root["Message-Id"].strip(" <>")
		e.message = cstr(mail.as_string())
		e.sender = mail.sender

	except frappe.InvalidEmailAddressError:
		# bad Email Address - don't add to queue
		frappe.log_error('Invalid Email ID Sender: {0}, Recipients: {1}'.format(mail.sender,
			', '.join(mail.recipients)), 'Email Not Sent')

	e.set_recipients(recipients + kwargs.get('cc', []) + kwargs.get('bcc', []))
	e.reference_doctype = kwargs.get('reference_doctype')
	e.reference_name = kwargs.get('reference_name')
	e.add_unsubscribe_link = kwargs.get("add_unsubscribe_link")
	e.unsubscribe_method = kwargs.get('unsubscribe_method')
	e.unsubscribe_params = kwargs.get('unsubscribe_params')
	e.expose_recipients = kwargs.get('expose_recipients')
	e.communication = kwargs.get('communication')
	e.send_after = kwargs.get('send_after')
	e.show_as_cc = ",".join(kwargs.get('cc', []))
	e.show_as_bcc = ",".join(kwargs.get('bcc', []))
	e.insert(ignore_permissions=True)

	return e
Beispiel #10
0
def sendmail(recipients, sender='', msg='', subject='[No Subject]', attachments=None, content=None,
	reply_to=None, cc=(), message_id=None,bcc=()):
	
	"""send an html email as multipart with attachments and all"""
	mail = get_email(recipients, sender, content or msg, subject, attachments=attachments, reply_to=reply_to, cc=cc)
	if message_id:
		mail.set_message_id(message_id)
	send_mail(mail,sender)
Beispiel #11
0
def sendmail(recipients, sender='', msg='', subject='[No Subject]', attachments=None, content=None,
	reply_to=None, cc=(), message_id=None, in_reply_to=None,read_receipt=None):
	"""send an html email as multipart with attachments and all"""
	mail = get_email(recipients, sender, content or msg, subject, attachments=attachments, reply_to=reply_to, cc=cc)
	mail.set_message_id(message_id)
	if in_reply_to:
		mail.set_in_reply_to(in_reply_to)

	if read_receipt:
		mail.msg_root["Disposition-Notification-To"] = sender
	send(mail)
    def test_email_header(self):
        email_html = '''
<h3>Hey John Doe!</h3>
<p>This is embedded image you asked for</p>
'''
        email_string = get_email(recipients=['*****@*****.**'],
                                 sender='*****@*****.**',
                                 subject='Test Subject',
                                 content=email_html,
                                 header=['Email Title', 'green'
                                         ]).as_string().replace("\r\n", "\n")
        # REDESIGN-TODO: Add style for indicators in email
        self.assertTrue('''<span class=3D"indicator indicator-green"></span>'''
                        in email_string)
        self.assertTrue('<span>Email Title</span>' in email_string)
Beispiel #13
0
def sendmail(recipients,
             sender='',
             msg='',
             subject='[No Subject]',
             attachments=None,
             content=None,
             reply_to=None):
    """send an html email as multipart with attachments and all"""
    send(
        get_email(recipients,
                  sender,
                  content or msg,
                  subject,
                  attachments=attachments,
                  reply_to=reply_to))
Beispiel #14
0
def get_email_queue(recipients, sender, subject, **kwargs):
    '''Make Email Queue object'''
    e = frappe.new_doc('Email Queue')
    e.priority = kwargs.get('send_priority')

    try:
        mail = get_email(recipients,
                         sender=sender,
                         subject=subject,
                         formatted=kwargs.get('formatted'),
                         text_content=kwargs.get('text_content'),
                         attachments=kwargs.get('attachments'),
                         reply_to=kwargs.get('reply_to'),
                         cc=kwargs.get('cc'),
                         email_account=kwargs.get('email_account'),
                         expose_recipients=kwargs.get('expose_recipients'))

        mail.set_message_id(kwargs.get('message_id'),
                            kwargs.get('is_notification'))
        if kwargs.get('read_receipt'):
            mail.msg_root["Disposition-Notification-To"] = sender
        if kwargs.get('in_reply_to'):
            mail.set_in_reply_to(kwargs.get('in_reply_to'))

        e.message_id = mail.msg_root["Message-Id"].strip(" <>")
        e.message = cstr(mail.as_string())
        e.sender = mail.sender

    except frappe.InvalidEmailAddressError:
        # bad Email Address - don't add to queue
        frappe.log_error(
            'Invalid Email ID Sender: {0}, Recipients: {1}'.format(
                mail.sender, ', '.join(mail.recipients)), 'Email Not Sent')

    e.set_recipients(recipients + kwargs.get('cc', []))
    e.reference_doctype = kwargs.get('reference_doctype')
    e.reference_name = kwargs.get('reference_name')
    e.add_unsubscribe_link = kwargs.get("add_unsubscribe_link")
    e.unsubscribe_method = kwargs.get('unsubscribe_method')
    e.unsubscribe_params = kwargs.get('unsubscribe_params')
    e.expose_recipients = kwargs.get('expose_recipients')
    e.communication = kwargs.get('communication')
    e.send_after = kwargs.get('send_after')
    e.show_as_cc = ",".join(kwargs.get('cc', []))
    e.insert(ignore_permissions=True)

    return e
Beispiel #15
0
	def test_email_header(self):
		email_html = '''
<h3>Hey John Doe!</h3>
<p>This is embedded image you asked for</p>
'''
		email_string = get_email(
			recipients=['*****@*****.**'],
			sender='*****@*****.**',
			subject='Test Subject',
			content=email_html,
			header=['Email Title', 'green']
		).as_string()

		self.assertTrue('''<span class=3D"indicator indicator-green" style=3D"background-color:#98=
d85b; border-radius:8px; display:inline-block; height:8px; margin-right:5px=
; width:8px" bgcolor=3D"#98d85b" height=3D"8" width=3D"8"></span>''' in email_string)
		self.assertTrue('<span>Email Title</span>' in email_string)
Beispiel #16
0
	def test_email_header(self):
		email_html = '''
<h3>Hey John Doe!</h3>
<p>This is embedded image you asked for</p>
'''
		email_string = get_email(
			recipients=['*****@*****.**'],
			sender='*****@*****.**',
			subject='Test Subject',
			content=email_html,
			header=['Email Title', 'green']
		).as_string()

		self.assertTrue('''<span class=3D"indicator indicator-green" style=3D"background-color:#98=
d85b; border-radius:8px; display:inline-block; height:8px; margin-right:5px=
; width:8px" bgcolor=3D"#98d85b" height=3D"8" width=3D"8"></span>''' in email_string)
		self.assertTrue('<span>Email Title</span>' in email_string)
    def get_email(self, print_html=None, print_format=None, attachments=None):
        """Make multipart MIME Email

		:param print_html: Send given value as HTML attachment.
		:param print_format: Attach print format of parent document."""

        if print_format:
            self.content += self.get_attach_link(print_format)

        default_incoming = frappe.db.get_value("Email Account",
                                               {"default_incoming": 1},
                                               "email_id")
        default_outgoing = frappe.db.get_value("Email Account",
                                               {"default_outgoing": 1},
                                               "email_id")

        if not self.sender:
            self.sender = "{0} <{1}>".format(
                frappe.session.data.full_name or "Notification",
                default_outgoing)

        mail = get_email(self.recipients,
                         sender=self.sender,
                         subject=self.subject,
                         content=self.content,
                         reply_to=default_incoming)

        mail.set_message_id(self.name)

        if print_html or print_format:
            attach_print(mail, self.get_parent_doc(), print_html, print_format)

        if isinstance(attachments, basestring):
            attachments = json.loads(attachments)

        if attachments:
            for a in attachments:
                try:
                    mail.attach_file(a)
                except IOError:
                    frappe.throw(_("Unable to find attachment {0}").format(a))

        return mail
Beispiel #18
0
	def test_email_header(self):
		email_html = """
<h3>Hey John Doe!</h3>
<p>This is embedded image you asked for</p>
"""
		email_string = (
			get_email(
				recipients=["*****@*****.**"],
				sender="*****@*****.**",
				subject="Test Subject",
				content=email_html,
				header=["Email Title", "green"],
			)
			.as_string()
			.replace("\r\n", "\n")
		)
		# REDESIGN-TODO: Add style for indicators in email
		self.assertTrue("""<span class=3D"indicator indicator-green"></span>""" in email_string)
		self.assertTrue("<span>Email Title</span>" in email_string)
Beispiel #19
0
    def prepare_email_content(self):
        mail = get_email(recipients=self.final_recipients(),
                         sender=self.sender,
                         subject=self.subject,
                         formatted=self.email_html_content(),
                         text_content=self.email_text_content(),
                         attachments=self._attachments,
                         reply_to=self.reply_to,
                         cc=self.final_cc(),
                         bcc=self.bcc,
                         email_account=self.get_outgoing_email_account(),
                         expose_recipients=self.expose_recipients,
                         inline_images=self.inline_images,
                         header=self.header)

        mail.set_message_id(self.message_id, self.is_notification)
        if self.read_receipt:
            mail.msg_root["Disposition-Notification-To"] = self.sender
        if self.in_reply_to:
            mail.set_in_reply_to(self.in_reply_to)
        return mail
Beispiel #20
0
def add(recipients, sender, subject, formatted, text_content=None,
	reference_doctype=None, reference_name=None, attachments=None, reply_to=None,
	cc=[], message_id=None, in_reply_to=None, send_after=None, send_priority=1, email_account=None,
	communication=None, unsubscribe_method=None, unsubscribe_params=None, expose_recipients=None, read_receipt=None, is_notification = False):
	"""Add to Email Queue"""
	e = frappe.new_doc('Email Queue')
	e.priority = send_priority

	try:
		mail = get_email(recipients, sender=sender, formatted=formatted, subject=subject,
			text_content=text_content, attachments=attachments, reply_to=reply_to,
			cc=cc, email_account=email_account, expose_recipients=expose_recipients)

		mail.set_message_id(message_id, is_notification)
		if read_receipt:
			mail.msg_root["Disposition-Notification-To"] = sender
		if in_reply_to:
			mail.set_in_reply_to(in_reply_to)

		e.message = cstr(mail.as_string())
		e.sender = mail.sender

	except frappe.InvalidEmailAddressError:
		# bad Email Address - don't add to queue
		return

	e.set("recipient", [])
	for r in recipients + cc:
		e.append("recipient",{"recipient":r})
	e.reference_doctype = reference_doctype
	e.reference_name = reference_name
	e.unsubscribe_method = unsubscribe_method
	e.unsubscribe_params = unsubscribe_params
	e.expose_recipients = expose_recipients
	e.communication = communication
	e.send_after = send_after
	e.show_as_cc = ",".join(cc)
	e.insert(ignore_permissions=True)

	return e
Beispiel #21
0
def sendmail_to_system_managers(subject, content):
	send(get_email(get_system_managers(), None, content, subject))
Beispiel #22
0
def get_email_queue(recipients, sender, subject, **kwargs):
    """Make Email Queue object"""
    e = frappe.new_doc("Email Queue")
    e.priority = kwargs.get("send_priority")
    attachments = kwargs.get("attachments")
    if attachments:
        # store attachments with fid or print format details, to be attached on-demand later
        _attachments = []
        for att in attachments:
            if att.get("fid"):
                _attachments.append(att)
            elif att.get("print_format_attachment") == 1:
                if not att.get("lang", None):
                    att["lang"] = frappe.local.lang
                att["print_letterhead"] = kwargs.get("print_letterhead")
                _attachments.append(att)
        e.attachments = json.dumps(_attachments)

    try:
        mail = get_email(
            recipients,
            sender=sender,
            subject=subject,
            formatted=kwargs.get("formatted"),
            text_content=kwargs.get("text_content"),
            attachments=kwargs.get("attachments"),
            reply_to=kwargs.get("reply_to"),
            cc=kwargs.get("cc"),
            bcc=kwargs.get("bcc"),
            email_account=kwargs.get("email_account"),
            expose_recipients=kwargs.get("expose_recipients"),
            inline_images=kwargs.get("inline_images"),
            header=kwargs.get("header"),
        )

        mail.set_message_id(kwargs.get("message_id"),
                            kwargs.get("is_notification"))
        if kwargs.get("read_receipt"):
            mail.msg_root["Disposition-Notification-To"] = sender
        if kwargs.get("in_reply_to"):
            mail.set_in_reply_to(kwargs.get("in_reply_to"))

        e.message_id = get_string_between("<", mail.msg_root["Message-Id"],
                                          ">")
        e.message = cstr(mail.as_string())
        e.sender = mail.sender

    except frappe.InvalidEmailAddressError:
        # bad Email Address - don't add to queue
        import traceback

        frappe.log_error(
            "Invalid Email ID Sender: {0}, Recipients: {1}, \nTraceback: {2} ".
            format(mail.sender, ", ".join(mail.recipients),
                   traceback.format_exc()),
            "Email Not Sent",
        )

    recipients = list(
        set(recipients + kwargs.get("cc", []) + kwargs.get("bcc", [])))
    e.set_recipients(recipients)
    e.reference_doctype = kwargs.get("reference_doctype")
    e.reference_name = kwargs.get("reference_name")
    e.add_unsubscribe_link = kwargs.get("add_unsubscribe_link")
    e.unsubscribe_method = kwargs.get("unsubscribe_method")
    e.unsubscribe_params = kwargs.get("unsubscribe_params")
    e.expose_recipients = kwargs.get("expose_recipients")
    e.communication = kwargs.get("communication")
    e.send_after = kwargs.get("send_after")
    e.show_as_cc = ",".join(kwargs.get("cc", []))
    e.show_as_bcc = ",".join(kwargs.get("bcc", []))
    e.insert(ignore_permissions=True)

    return e
Beispiel #23
0
def sendmail_to_system_managers(subject, content):
    send(get_email(get_system_managers(), None, content, subject))
Beispiel #24
0
def add(recipients,
        sender,
        subject,
        formatted,
        text_content=None,
        reference_doctype=None,
        reference_name=None,
        attachments=None,
        reply_to=None,
        cc=[],
        message_id=None,
        in_reply_to=None,
        send_after=None,
        send_priority=1,
        email_account=None,
        communication=None,
        unsubscribe_method=None,
        unsubscribe_params=None,
        expose_recipients=None):
    """Add to Email Queue"""
    e = frappe.new_doc('Email Queue')
    e.priority = send_priority

    try:
        mail = get_email(recipients,
                         sender=sender,
                         formatted=formatted,
                         subject=subject,
                         text_content=text_content,
                         attachments=attachments,
                         reply_to=reply_to,
                         cc=cc,
                         email_account=email_account,
                         expose_recipients=expose_recipients)

        mail.set_message_id(message_id)
        if in_reply_to:
            mail.set_in_reply_to(in_reply_to)

        e.message_id = mail.msg_root["Message-Id"].strip(" <>")
        e.message = cstr(mail.as_string())
        e.sender = mail.sender

    except frappe.InvalidEmailAddressError:
        # bad email id - don't add to queue
        return

    e.set("recipient", [])
    for r in recipients + cc:
        e.append("recipient", {"recipient": r})
    e.reference_doctype = reference_doctype
    e.reference_name = reference_name
    e.unsubscribe_method = unsubscribe_method
    e.unsubscribe_params = unsubscribe_params
    e.expose_recipients = expose_recipients
    e.communication = communication
    e.send_after = send_after
    e.show_as_cc = ",".join(cc)
    e.insert(ignore_permissions=True)

    return e