def send_email(subject, message, recipients, html=False, attachments=None, order=None): """Send emails Parameters ---------- subject: str The subject of the email message: str Body of the email recipients: list An iterable with django users representing the recipients of the email html: bool, optional Whether the e-mail should be sent in HTML or plain text """ already_emailed = [] custom_recipient_handler = oseo_settings.get_mail_recipient_handler() if custom_recipient_handler is not None: logger.debug("Calling custom recipient handler code...") handler = utilities.import_callable(custom_recipient_handler) final_recipients = handler(subject, message, current_recipients=recipients, order=order) else: final_recipients = [r.email for r in recipients] logger.debug("email recipients: {}".format(final_recipients)) for address in final_recipients: if address != "" and address not in already_emailed: msg = MailerMessage(subject=subject, to_address=address, from_address=django_settings.EMAIL_HOST_USER, app="oseoserver") if html: text_content = html2text(message) msg.content = text_content msg.html_content = message else: msg.content = message if attachments is not None: for a in attachments: msg.add_attachment(a) msg.save() already_emailed.append(address)
def send(msg): tos = msg.to + msg.bcc for to in tos: new_message = MailerMessage() new_message.subject = msg.subject new_message.to_address = to new_message.from_address = msg.from_email new_message.content = msg.body if len(msg.alternatives) > 0: new_message.html_content = msg.alternatives[0][0] new_message.reply_to = next(iter(msg.reply_to or []),None) for a in msg.attachments: with open(a[0], 'wb') as f: f.write(a[1]) at = File(open(a[0], 'rb')) new_message.add_attachment(at) new_message.save()
def send_email(to_address, subject, content, html_content, bcc_address=None, attachment=None, attachment2=None, attachment3=None): new_message = MailerMessage() new_message.subject = subject new_message.to_address = to_address if bcc_address: new_message.bcc_address = bcc_address new_message.from_address = settings.DEFAULT_FROM_EMAIL new_message.content = content new_message.html_content = html_content new_message.cc_address = settings.DEFAULT_CC_EMAIL if attachment: new_message.add_attachment(attachment) if attachment2: new_message.add_attachment(attachment2) if attachment3: new_message.add_attachment(attachment3) new_message.app = "our app name" new_message.save()