def send(self, to): """ Sends this email to the list of the users in TO, with additional BCC and CC arguments. """ to = [to] if isinstance(to, User) else to connection = get_connection() for user in to: email = EmailMessage(template_name='api/email.html', context={ 'subject': Email._parse_text(self.subject, user), 'content': Email._parse_text(self.content, user), 'footer': Email._parse_text(self.footer, user) }, from_email=self.from_email, to=[user.email], connection=connection, reply_to=self.reply_to) for attachment in self.attachments.all(): email.attach(attachment.name, attachment.datafile, attachment.mimetype) email.send() self.times_sent += 1 self.save()
def solicita(request): if request.method == 'POST': form = UserCreationForm(request.POST) form1 = New_Solic_cad(request.POST) print(form.errors) if form.is_valid(): post1 = form.save(commit=False) post1.first_name = 'first_access' post1.save() if request.POST.get('Contato'): if form1.is_valid(): Contato = request.POST.get('Contato') Obs = request.POST.get('Obs') post = form1.save(commit=False) post.Solicitante = request.user post.save() print(request.POST.get('username')) context = { 'contato': Contato, 'emp': 'rrrr', 'solicitante': request.user.username, 'obs': Obs, 'rec': request.POST.get('username') } message = EmailMessage('SOLICTA_EMP_MAIL.html', context, settings.EMAIL_HOST_USER, [ '*****@*****.**', '*****@*****.**', ], render=True) f = '/SIG_1.png' fp = open(os.path.join(os.path.dirname(__file__), f), 'rb') msg_img = MIMEImage(fp.read()) fp.close() msg_img.add_header('Content-ID', '<{}>'.format(f)) message.attach(msg_img) message.send() return render(request, 'success.html', { 'Usmail': request.user.email, 'Solmail': post1.username }) else: form = UserCreationForm() form1 = New_Solic_cad() return render(request, 'Nsuccess.html') else: form = UserCreationForm() form1 = New_Solic_cad() return render(request, 'Nsuccess.html') else: form = UserCreationForm() form1 = New_Solic_cad() return render(request, 'solicita.html', {'form': form, 'form1': form1})
def send_upn(request, event_name=False): event_instance = get_event_by_name_no_rights(event_name) profile = get_object_or_404(EventProfile, event=event_instance, user=request.user) context = { 'event': event_instance, 'profile': profile, 'date': datetime.now(), 'date_to': date.today() + timedelta(days=10) } for payment in event_instance.payment.all(): t_payment = payment.get_payments() if t_payment[0] == 'UPN': context['payment'] = t_payment[1] break print(context) return_file = "tmp/" + 'upn_' + event_instance.name + '_' + str( profile.id) + '.pdf' t = loader.get_template('Visitor/email/upn.html') #DEBUG FOR UPN #return render(request, 'Visitor/email/upn.html', context) response = render_pdf_from_template( request=request, context=context, input_template=t, header_template='', footer_template='', ) message = EmailMessage( 'Visitor/email/base_email.html', {'user': profile}, '*****@*****.**', #event_instance.sent_mail, to=[profile.user.email]) message.attach('invoice.pdf', response, 'application/pdf') message.send() return render(request, 'Visitor/mail_redirect.html', {})
class Owl(): """ Owl is an email agent. It utilizes default send_mail() as a message-backend, hoping you have configured it properly. On the production host it tries to queue your message via the Celery daemon. For usage examples please see tests. """ timezone = None def __init__(self, template, ctx, from_email=None, timezone=None, to=[]): if from_email is None: from_email = settings.EMAIL_NOTIFICATIONS_FROM self.template = template self.ctx = ctx self.to = to self.from_email = from_email if timezone is not None: if isinstance(timezone, str): self.timezone = pytz.timezone(timezone) else: self.timezone = timezone self.headers = { 'X-ELK-Timezone': str(self.timezone), } self.EmailMessage() @user_tz @disable_i18n def EmailMessage(self): """ This method preventively renders a message to catch possible errors in the main flow. """ self.msg = EmailMessage( self.template, self.ctx, self.from_email, self.to, headers=self.headers, reply_to=[settings.REPLY_TO], ) self.msg.render() @user_tz @disable_i18n def send(self): """ Send message On the production host uses celery, on dev — django configured backend. """ if not self.clean(): logger.warning('Trying to send invalid message!') return if not settings.EMAIL_ASYNC: self.msg.send() else: self.queue() @user_tz @disable_i18n def queue(self): self.headers['X-ELK-Queued'] = 'True' send_email.delay(owl=self) def attach(self, filename=None, content=None, mimetype=None): """ Add an attachment to the message See http://django-mail-templated.readthedocs.io/en/master/api.html?highlight=attach#mail_templated.EmailMessage.attach """ return self.msg.attach(filename, content, mimetype) def clean(self): if not self.to or not self.to[0]: return False return True
class Owl(): """ Owl is an email agent. It utilizes default send_mail() as a message-backend, hoping you have configured it properly. On the production host it tries to queue your message via the Celery daemon. For usage examples please see tests. """ timezone = None def __init__(self, template, ctx, from_email=None, reply_to=None, timezone=None, to=None): if to is None: to = [] if from_email is None: from_email = settings.EMAIL_NOTIFICATIONS_FROM if reply_to is None: reply_to = settings.REPLY_TO self.template = template self.ctx = ctx self.to = to self.reply_to = reply_to self.from_email = from_email if timezone is not None: if isinstance(timezone, str): self.timezone = pytz.timezone(timezone) else: self.timezone = timezone self.headers = { 'X-GM-Timezone': str(self.timezone), } self.EmailMessage() def EmailMessage(self): """ This method preventively renders a message to catch possible errors in the main flow. """ self.msg = EmailMessage( self.template, self.ctx, self.from_email, self.to, headers=self.headers, reply_to=[self.reply_to], ) self.msg.render() def send(self): """ Send message """ if not self.clean(): return if not settings.DISABLE_NOTIFICATIONS and settings.EMAIL_ENABLED: self.msg.send() def add_cc(self, *args): """ Add a list of recipients """ self.msg.cc += args def attach(self, filename=None, content=None, mimetype=None): """ Add an attachment to the message See http://django-mail-templated.readthedocs.io/en/master/api.html?highlight=attach#mail_templated.EmailMessage.attach """ return self.msg.attach(filename, content, mimetype) def clean(self) -> bool: if not self.to or not self.to[0]: return False return True