def task_send_proposal(proposal, old_status=None): urlsafe_token = proposal.generate_token() subject = "Presupuesto" from_email = settings.ADMIN_EMAIL to = proposal.event.customer.email template = loader.get_template("base/email/proposal.html") domain = settings.CATERFULL_BASE_URL context = Context({ 'token': urlsafe_token, 'domain': domain, 'proposal': proposal, 'pidb64': urlsafe_base64_encode(force_bytes(proposal.id)) }) html_content = template.render(context) msg = EmailMultiAlternatives(subject, html_content, from_email, [to]) msg.content_subtype = "html" # msg.attach_alternative(html_content, "text/html") proposal_pdf = generate_proposal_pdf(proposal=proposal) msg.attach_file(proposal_pdf.name, 'application/pdf') try: msg.send(fail_silently=False) # proposal.has_been_sent() return OK except SMTPException as e: proposal.reset_token(restart_status=True, old_status=old_status) return ERROR
def send_raw_mail(self, to_addrs, subject, html, attachfile_paths=None): """ send mail with attachments. charset depend on CHARSET in settings.py. if not set, default is utf8. Args: to_addrs (list): email address list subject (string): subject string html (string): html mail content attachfile_paths (list): a list contains all the attachments path want to send. if this arg is None, will use self.send_mail to send an email """ if not attachfile_paths: logger.warn('use send_raw_mail method but with no attachment. to %s, subject: %s' % ( to_addrs, subject )) self.send_html_mail(to_addrs, subject, html) return logger.info('start to send raw mail to %s, subject: %s' % (to_addrs, subject)) email = EmailMultiAlternatives(subject, html, self.from_addr, to_addrs) email.content_subtype = 'html' for attachment_path in attachfile_paths: email.attach_file(attachment_path) if not email.send(): error_msg = 'mail send to nobody. subject: %s, toAddrs: %s' % (subject, to_addrs) logger.error(error_msg) raise Exception(error_msg) logger.info('successfully send raw mail to %s, subject: %s' % (to_addrs, subject))
def send_email(self, emailobj, attachment): if emailobj: email_multimedia = EmailMultimedia.objects.filter( dealer=emailobj.dealer) email_body, emailbodyhtml = self.get_email_body(emailobj) bcc = self.get_bcc(emailobj) cc = self.get_cc(emailobj) mail_to = self.get_to(emailobj) msg = EmailMultiAlternatives(emailobj.subject, emailbodyhtml, emailobj.mail_from, mail_to, bcc=bcc, cc=cc) msg.content_subtype = "html" for obj in email_multimedia: fp = open( os.path.join(settings.MEDIA_ROOT, 'email_media/' + obj.multimedia_file), 'rb') msg_img = MIMEImage(fp.read()) fp.close() msg_img.add_header('Content-ID', '<{}>'.format(obj.multimedia_file)) msg.attach(msg_img) if attachment: msg.attach("CheckinReview.pdf", attachment, "application/pdf") msg.send() print "mail sent"
def task_send_invoice(invoice): urlsafe_token = invoice.generate_token() subject = "Presupuesto" from_email = settings.ADMIN_EMAIL to = invoice.proposal.event.customer.email template = loader.get_template("base/email/invoice.html") domain = settings.CATERFULL_BASE_URL context = Context({'token':urlsafe_token,'domain':domain, 'invoice':invoice, 'iidb64':urlsafe_base64_encode(force_bytes(invoice.id))}) text_content = template.render(context) msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) invoice_pdf = generate_invoice_pdf(invoice=invoice) msg.content_subtype = "html" msg.attach_file(invoice_pdf.name,'application/pdf') response = OK try: with transaction.atomic(): if not invoice.has_payment_order(): payment = create_payment(invoice.proposal.get_total()) invoice.set_payment(payment=payment) msg.send(fail_silently=False) invoice.has_been_sent() except SMTPException as e: print(e) invoice.reset_token() response = ERROR except Exception as e: invoice.reset_token() response = ERROR print(e) return response
def send_html_mail_task(subject, text_message, html_message, from_email, recipient_list, template='email/email_base.html'): if template is not None: html_message = render_to_string(template, {'content': mark_safe(html_message)}) # render html into an email template message = EmailMultiAlternatives("Discover Special Value - {0}".format(subject), html_message, from_email, recipient_list) message.content_subtype = "html" message.attach_alternative(text_message, "text/plain") message.send()
def send_restore(self, request=None, skip=False): stamp, crypt = self.get_restore_credentials() if not skip and appsettings.RESTORE_SEND_MAIL: html = None if appsettings.RESTORE_TEMPLATE_HTML: try: html = get_template(appsettings.RESTORE_TEMPLATE_HTML) except TemplateDoesNotExist: pass plain = None if appsettings.RESTORE_TEMPLATE_PLAIN: try: plain = get_template(appsettings.RESTORE_TEMPLATE_PLAIN) except TemplateDoesNotExist: pass try: subject = get_template(appsettings.RESTORE_TEMPLATE_SUBJECT) except TemplateDoesNotExist: subject = None if subject and (html or plain): context = { 'user': self.user, 'email': self.email, 'stamp': stamp, 'crypt': crypt, 'viewname': appsettings.RESOLVE_PASSWORD_RESTORE, 'timeout': appsettings.RESTORE_TIMEOUT, 'request': request, } message = EmailMultiAlternatives(subject.render(context).strip(), to=[self.email]) if plain and html: message.body = plain.render(context).strip() message.attach_alternative(html.render(context).strip(), "text/html") elif html: message.body = html.render(context).strip() message.content_subtype = "html" else: message.body = plain.render(context).strip() message.send() else: logger.critical( "No subject or text provided for password restore. Sending validation-mail to %s aborted", self.email, ) password_restore_send.send(sender=self.__class__, user=self.user, email=self.email, stamp=stamp, crypt=crypt, skip=skip) logger.info("%s has requested a password restore for %s", self.user, self.email) return (stamp, crypt)
def send_email(title, text, emails: iter = ('*****@*****.**', ), as_html: bool = False): mail = EmailMultiAlternatives(title, text, settings.EMAIL_HOST_USER, emails) if as_html: mail.content_subtype = "html" try: mail.send(fail_silently=False) except Exception as Ex: return Ex
def email_two_factor_success(self, user: get_user_model(), token): """ Sends email containing one-time token """ subject = "Two-Factor Authentication Successful" msg = EmailMultiAlternatives( subject=subject, body=self.build_html_content(user, token), from_email=settings.DEFAULT_FROM_EMAIL_SES, to=[user.email], ) msg.content_subtype = 'html' msg.mixed_subtype = 'related' msg.send()
def email_two_factor_token(self, user: get_user_model(), token): """ Sends email containing current token """ subject = "Your One Time Token" msg = EmailMultiAlternatives( subject=subject, body=self.build_html_content(user, token), from_email=settings.DEFAULT_FROM_EMAIL_SES, to=[user.email], ) msg.content_subtype = 'html' msg.mixed_subtype = 'related' msg.send()
def send_restore(self, request=None, skip=False): stamp, crypt = self.get_restore_credentials() if not skip and appsettings.RESTORE_SEND_MAIL: html = None if appsettings.RESTORE_TEMPLATE_HTML: try: html = get_template(appsettings.RESTORE_TEMPLATE_HTML) except TemplateDoesNotExist: pass plain = None if appsettings.RESTORE_TEMPLATE_PLAIN: try: plain = get_template(appsettings.RESTORE_TEMPLATE_PLAIN) except TemplateDoesNotExist: pass try: subject = get_template(appsettings.RESTORE_TEMPLATE_SUBJECT) except TemplateDoesNotExist: subject = None if subject and (html or plain): context = { 'user': self.user, 'email': self.email, 'stamp': stamp, 'crypt': crypt, 'viewname': appsettings.RESOLVE_PASSWORD_RESTORE, 'timeout': appsettings.RESTORE_TIMEOUT, 'request': request, } message = EmailMultiAlternatives(subject.render(context).strip(), to=[self.email]) if plain and html: message.body = plain.render(context).strip() message.attach_alternative(html.render(context).strip(), "text/html") elif html: message.body = html.render(context).strip() message.content_subtype = "html" else: message.body = plain.render(context).strip() message.send() else: logger.critical("No subject or text provided for password restore. Sending validation-mail to %s aborted", self.email) password_restore_send.send(sender=self.__class__, user=self.user, email=self.email, stamp=stamp, crypt=crypt, skip=skip) logger.info("%s has requested a password restore for %s", self.user, self.email) return (stamp, crypt)
def prepare_email(self, recipient_list, from_email=None): """From the recipient list, create an email""" from_email = from_email or settings.DEFAULT_FROM_EMAIL subject = self.get_subject() html_message = self.render_html() text_message = self.render_text() if text_message is not None: message = EmailMultiAlternatives(subject, text_message, from_email, recipient_list) if html_message is not None: message.attach_alternative(html_message, "text/html") else: message = EmailMultiAlternatives(subject, html_message, from_email, recipient_list) message.content_subtype = "html" return message
def prepare_email(self, recipient_list, from_email=None): """From the recipient list, create an email""" from_email = from_email or settings.DEFAULT_FROM_EMAIL subject = self.get_subject() html_message = self.render_html() text_message = self.render_text() if text_message is not None: message = EmailMultiAlternatives(subject, text_message, from_email, recipient_list) if html_message is not None: message.attach_alternative(html_message, "text/html") else: message = EmailMultiAlternatives(subject, html_message, from_email, recipient_list) message.content_subtype = "html" return message
def create_message(self, send_text, send_html): """ Create a message object and return it, or return None if no recipients """ # We need at least one recipient to = self._validate_list(self.to) if not len(to): return None if not send_text and not send_html: return None text = self.get_rendered_text() if send_text else None html = self.get_rendered_html() if send_html else None message = EmailMultiAlternatives( subject=self.subject, from_email=self.get_from_address(), to=to, cc=self._validate_list(self.cc), bcc=self._validate_list(self.bcc), headers=self.headers ) if text: message.body = text if send_html: # Text plus html message.attach_alternative(html, 'text/html') else: # There is no text version, so send it as pure html only message.body = html message.content_subtype = 'html' if self.attachments: for attachment in self.attachments: message.attach_file(attachment) return message
def render_mail(subject, template_prefix, to_emails, context, bcc=None, cc=None, **kwargs): from_email = DEFAULT_FROM_EMAIL bodies = {} for ext in ['html', 'txt']: try: template_name = '{0}.{1}'.format(template_prefix, ext) bodies[ext] = render_to_string(template_name, context).strip() except TemplateDoesNotExist: if ext == 'txt' and not bodies: # We need at least one body raise if 'txt' in bodies: msg = EmailMultiAlternatives(subject, bodies['txt'], from_email, to_emails, bcc=bcc, cc=cc) if 'html' in bodies: msg.attach_alternative(bodies['html'], 'text/html') else: msg = EmailMessage(subject, bodies['html'], from_email, to_emails, bcc=bcc, cc=cc) msg.content_subtype = 'html' # Main content is now text/html return msg
def task_send_booking_nt_to_business(event): business = event.customer.business subject = "Reserva" from_email = settings.ADMIN_EMAIL to = business.contact_email() template = loader.get_template("base/booking/email/business_nt.html") domain = settings.CATERFULL_BASE_URL context = Context({'domain': domain, 'business': business, 'event': event}) html_content = template.render(context) msg = EmailMultiAlternatives(subject, html_content, from_email, [to]) msg.content_subtype = "html" # msg.attach_alternative(html_content, "text/html") try: msg.send(fail_silently=False) return OK except SMTPException as e: return ERROR
def render_mail(subject, template_prefix, to_emails, context, bcc=None, cc=None, **kwargs): from_email = DEFAULT_FROM_EMAIL bodies = {} for ext in ['html', 'txt']: try: template_name = '{0}.{1}'.format(template_prefix, ext) bodies[ext] = render_to_string(template_name, context).strip() except TemplateDoesNotExist: if ext == 'txt' and not bodies: # We need at least one body raise if 'txt' in bodies: msg = EmailMultiAlternatives(subject, bodies['txt'], from_email, to_emails, bcc=bcc, cc=cc) if 'html' in bodies: msg.attach_alternative(bodies['html'], 'text/html') else: msg = EmailMessage(subject, bodies['html'], from_email, to_emails, bcc=bcc, cc=cc) msg.content_subtype = 'html' # Main content is now text/html return msg
def send_mail(to, subject, template, context={}, html=False): #if settings.DEBUG: # return email_template = get_template(template) c = Context(context) from_email, subject_prefix = settings.DEFAULT_FROM_EMAIL, settings.EMAIL_SUBJECT_PREFIX connection = get_connection(fail_silently=True) message = EmailMultiAlternatives(subject=subject_prefix + subject, body=email_template.render(c), from_email=from_email, to=[to], connection=connection, headers={'Reply-To': 'StartupForMe <*****@*****.**>'}) if html: message.attach_alternative(email_template.render(c), 'text/html') message.html = email_template.render(c) message.auto_html = True message.content_subtype = "html" message.send()
def run (self): msg = EmailMultiAlternatives(self.subject, self.body, self.from_email, self.recipient_list) if self.html: msg.attach_alternative(self.html, "text/html") msg.content_subtype = 'html' msg.send(self.fail_silently)