def get_context_data(self, **kwargs): context = super(DocView, self).get_context_data(**kwargs) context['login_js_url'] = static('widget/js/login.min.js') context['Message_ID'] = make_msgid() context['SORTED_DISCIPLINES'] = SORTED_DISCIPLINES context['DEGREES'] = DEGREES context['HOSTELS'] = HOSTELS context['SEXES'] = SEXES context['USER_TYPES'] = UserProfile.objects.values_list( 'type').distinct() # Mark all tabs as inactive for tab_ in self.tabs: tab_.is_active = False tab = context.get('tab', '') for tab_ in self.tabs: if tab == tab_.tab_name: tab = tab_ break else: tab = self.tabs[0] tab.is_active = True context['tabs'] = self.tabs context['active_tab'] = tab return context
def get_context_data(self, **kwargs): context = super(DocView, self).get_context_data(**kwargs) context['login_js_url'] = static('widget/js/login.min.js') context['Message_ID'] = make_msgid() context['SORTED_DISCIPLINES'] = SORTED_DISCIPLINES context['DEGREES'] = DEGREES context['HOSTELS'] = HOSTELS context['SEXES'] = SEXES context['USER_TYPES'] = UserProfile.objects.values_list('type').distinct() # Mark all tabs as inactive for tab_ in self.tabs: tab_.is_active = False tab = context.get('tab', '') for tab_ in self.tabs: if tab == tab_.tab_name: tab = tab_ break else: tab = self.tabs[0] tab.is_active = True context['tabs'] = self.tabs context['active_tab'] = tab return context
def send_email(self, reasons=[u"Demande spécifique", ]): """ Send an email for this particuliar ticket """ dests = set() if self.client: dests = dests.union(set(self.client.get_emails())) # La personne qui a ouvert if self.opened_by.email: dests.add(self.opened_by.email) # La personne sur qui le ticket est assignée if self.assigned_to and self.assigned_to.email: dests.add(self.assigned_to.email) # Les project watchers ? # LC: TODO watchers # Ceux qui ont participé (comments) dests = dests.union(set([c.user.email for c in django.contrib.comments.get_model().objects.filter(content_type__model="ticket", object_pk=str(self.id)) if c.user and c.user.email])) #print "Envoi d'email à %s. Raisons: %s" % (dests, reasons) if not dests: # print "Aucun destinataire email ?!" return # Application du template email template = get_template("email/ticket.txt") context = Context({"ticket": self, 'childs': self.child.order_by('date_open'), 'reasons': reasons}) data = template.render(context) template = Template("{% autoescape off %}[Ticket {{ ticket.id }} ({{ ticket.state }})]: {{ ticket.title|striptags|truncatewords:64 }}{% endautoescape %}") subject = template.render(context) # Send the email mail = EmailMessage(subject, data, settings.DEFAULT_FROM_EMAIL, dests) if self.message_id: mail.extra_headers['In-Reply-To'] = self.message_id mail.extra_headers['References'] = self.message_id else: self.message_id = make_msgid() self.save() mail.extra_headers['Message-ID'] = self.message_id if self.keywords: mail.extra_headers['X-CLARITICK-KEYWORDS'] = self.keywords self.ticketmailtrace_set.create(email=mail) mail.send()
def send(self, commit=True, only_email=False): self.case.update_email() msg_id = make_msgid(domain=self.case.email.split("@", 2)[1]) message = self._construct_message(msg_id=msg_id) text = message.message().as_bytes() self.email = self.case.institution.email self.message_id_header = normalize_msg_id(msg_id) self.eml.save("%s.eml" % uuid.uuid4(), ContentFile(text), save=False) self.is_draft = False if commit: self.save(update_fields=["eml", "email"] if only_email else None) return message.send()
def prepare_reply(self, message, ): """ Prepares a email reply """ if self.mailbox.from_email: message.from_email = self.mailbox.from_email else: message.from_email = settings.DEFAULT_FROM_EMAIL message.extra_headers['Message-ID'] = make_msgid() message.extra_headers['Date'] = formatdate() message.extra_headers['In-Reply-To'] = self.message_id message.extra_headers['References'] = self.message_id return message
def message(self): encoding = self.encoding or settings.DEFAULT_CHARSET msg = MIMEUTF8QPText(self.body, encoding) msg = self._create_message(msg) msg['Subject'] = self.subject msg['From'] = self.extra_headers.get('From', self.from_email) msg['To'] = self.extra_headers.get('To', ', '.join(self.to)) if self.cc: msg['Cc'] = ', '.join(self.cc) header_names = [key.lower() for key in self.extra_headers] if 'date' not in header_names: msg['Date'] = formatdate() if 'message-id' not in header_names: msg['Message-ID'] = make_msgid() for name, value in self.extra_headers.items(): if name.lower() in ('from', 'to'): # From and To are already handled continue msg[name] = value del msg['MIME-Version'] wrapper = SafeMIMEMultipart( 'signed', protocol='application/pgp-signature', micalg='pgp-sha512') wrapper.preamble = ( "This is an OpenPGP/MIME signed message (RFC 4880 and 3156)" ) # copy headers from original message to PGP/MIME envelope for header in msg.keys(): if header.lower() not in ( 'content-disposition', 'content-type', 'mime-version' ): for value in msg.get_all(header): wrapper.add_header(header, value) del msg[header] for part in msg.walk(): del part['MIME-Version'] signature = self._sign(msg) wrapper['Content-Disposition'] = 'inline' wrapper.attach(msg) wrapper.attach(signature) return wrapper
def encrypt(self, sender_address, encrypting_identities, message): """ Encrypts the given message for all the supplied recipients. """ if not encrypting_identities: raise ValueError('Encrypting recipient identities not supplied.') if not message: raise ValueError('Valid Message not supplied.') s = SMIME.SMIME() cipher = getattr(settings, 'DJEMBE_CIPHER', 'aes_256_cbc') s.set_cipher(SMIME.Cipher(cipher)) self.logger.debug("Encrypting message for %s" % encrypting_identities) # Gather all the recipient certificates sk = X509.X509_Stack() for identity in encrypting_identities: sk.push(identity.x509) s.set_x509_stack(sk) # prepare the payload for encryption payload_msg = self.extract_payload(message) # encrypt the payload payload = BIO.MemoryBuffer(payload_msg.as_string()) pkcs7_encrypted_data = s.encrypt(payload) payload.close() # get the PKCS7 object into a string payload = BIO.MemoryBuffer() s.write(payload, pkcs7_encrypted_data) pkcs7_string = payload.read() payload.close() encrypted_message = email.message_from_string(pkcs7_string) message.set_payload(encrypted_message.get_payload()) for header, value in encrypted_message.items(): del message[header] message[header] = value del message['Message-ID'] message['Message-ID'] = make_msgid() return message
def encrypt(self, sender_address, encrypting_identities, message): """ Encrypts the given message for all the supplied recipients. """ if not encrypting_identities: raise ValueError("Encrypting recipient identities not supplied.") if not message: raise ValueError("Valid Message not supplied.") s = SMIME.SMIME() cipher = getattr(settings, "DJEMBE_CIPHER", "aes_256_cbc") s.set_cipher(SMIME.Cipher(cipher)) self.logger.debug("Encrypting message for %s" % encrypting_identities) # Gather all the recipient certificates sk = X509.X509_Stack() for identity in encrypting_identities: sk.push(identity.x509) s.set_x509_stack(sk) # prepare the payload for encryption payload_msg = self.extract_payload(message) # encrypt the payload payload = BIO.MemoryBuffer(payload_msg.as_bytes()) pkcs7_encrypted_data = s.encrypt(payload) payload.close() # get the PKCS7 object into a string payload = BIO.MemoryBuffer() s.write(payload, pkcs7_encrypted_data) pkcs7_string = payload.read() payload.close() encrypted_message = email.message_from_string(pkcs7_string.decode("UTF-8")) message.set_payload(encrypted_message.get_payload()) for header, value in encrypted_message.items(): del message[header] message[header] = value del message["Message-ID"] message["Message-ID"] = make_msgid() return message
def sendemail(self, destList, alertmatch): """Send this alert by main to destList emails.""" # Email Body template_context = Context({'alert': self, 'alertmatch': alertmatch}) template = get_template("alerts/email.txt") data = template.render(template_context) # Email Subject template = get_template("alerts/email_subject.txt") subject = template.render(template_context) mail = EmailMessage(subject, data, settings.DEFAULT_FROM_EMAIL, destList) mail.extra_headers['Message-ID'] = make_msgid() mail.extra_headers["X-MINIALERTS-ID"] = self.pk mail.send() self.email_sent = True self.save()
def send_email(self): over_target = setting('MISSIVE_EMAIL', False) self.missive.target = over_target if over_target else self.missive.target if setting('MISSIVE_SERVICE', False): self.missive.msg_id = make_msgid() text_content = str(self.missive.txt) html_content = self.missive.html self.email = EmailMultiAlternatives( self.missive.subject, html_content, conf.sender_email, [self.missive.target], headers={'Message-Id': self.missive.msg_id}) self.email.attach_alternative(html_content, "text/html") self.email_attachments() self.missive.to_sent() self.missive.save() return self.missive.status
def send_email(request): match_id = request.data['match_id'] # Get the recipients email based on the if request.user.is_employer: # We can use get() because there is only one instance/row recipient = Match.objects.get(id=match_id).seeker.user else: recipient = Match.objects.get(id=match_id).employer.user # Set the emails fields: subject = 'You have a match from Seek Geek!' reply_to = request.user.email # to_email = recipient.email to_email = '*****@*****.**' email_message = f"Hello {recipient.first_name}, " \ f"\nYour match on Seek Geek, {request.user.first_name}, " \ f"is interested in connecting with you, please reach out to them. " \ f"\nHere's their email address: {reply_to}" if 'message' in request.data: email_message += f"\nHere's their message: {request.data['message']}" email = EmailMessage( subject=subject, body=email_message, from_email=settings.DEFAULT_FROM_EMAIL, to=[to_email], reply_to=[reply_to], headers={"Message - ID": make_msgid(domain='seekgeek.app')}, ) try: email.send(fail_silently=False) except Exception as e: return Response( { "message": 'Error while sending email', 'error': str(e) }, status=500) return Response({"message": 'Email was sent'})
def message(self): msg=super(EmailPGP, self).message() encoding = self.encoding or settings.DEFAULT_CHARSET del msg['From'] del msg['Subject'] del msg['To'] del msg['Date'] if self.signed: tmp = SafeMIMEMultipart(_subtype=self.signed_subtype, encoding=encoding) tmp.attach(msg) attachment = MIMEBase('application', 'pgp-signature') #We don't want base64 enconding attachment.set_payload(detach_sign(msg.as_string(), self.from_email)) attachment.add_header('Content-Disposition', 'attachment', filename='signature.asc') tmp.attach(attachment) msg=tmp if self.encrypted: tmp = SafeMIMEMultipart(_subtype=self.encrypted_subtype, encoding=encoding) tmp.attach(self._create_attachment('', '', 'application/pgp-encrypted')) attachment = MIMEBase('application', 'octet-stream') #We don't want base64 enconding attachment.set_payload(encrypt(msg.as_string(), self.to)) attachment.add_header('Content-Disposition', 'inline', filename='msg.asc') tmp.attach(attachment) msg=tmp msg['Subject'] = self.subject msg['From'] = self.extra_headers.get('From', self.from_email) msg['To'] = self.extra_headers.get('To', ', '.join(self.to)) if self.cc: msg['Cc'] = ', '.join(self.cc) # Email header names are case-insensitive (RFC 2045), so we have to # accommodate that when doing comparisons. header_names = [key.lower() for key in self.extra_headers] if 'date' not in header_names: msg['Date'] = formatdate() if 'message-id' not in header_names: msg['Message-ID'] = make_msgid() for name, value in self.extra_headers.items(): if name.lower() in ('from', 'to'): # From and To are already handled continue msg[name] = value return msg
def reply(self, message): """Sends a message as a reply to this message instance. Although Django's e-mail processing will set both Message-ID and Date upon generating the e-mail message, we will not be able to retrieve that information through normal channels, so we must pre-set it. """ if self.mailbox.from_email: message.from_email = self.mailbox.from_email else: message.from_email = settings.DEFAULT_FROM_EMAIL message.extra_headers['Message-ID'] = make_msgid() message.extra_headers['Date'] = formatdate() message.extra_headers['In-Reply-To'] = self.message_id message.send() return self.mailbox.record_outgoing_message( email.message_from_string(message.message().as_string()))
def reply(self, message): """Sends a message as a reply to this message instance. Although Django's e-mail processing will set both Message-ID and Date upon generating the e-mail message, we will not be able to retrieve that information through normal channels, so we must pre-set it. """ if self.mailbox.from_email: message.from_email = self.mailbox.from_email else: message.from_email = settings.DEFAULT_FROM_EMAIL message.extra_headers['Message-ID'] = make_msgid() message.extra_headers['Date'] = formatdate() message.extra_headers['In-Reply-To'] = self.message_id message.send() return self.mailbox.record_outgoing_message( email.message_from_string( message.message().as_string() ) )
def message(self): encoding = self.encoding or settings.DEFAULT_CHARSET msg = SafeMIMEText(smart_str(self.body, encoding), self.content_subtype, encoding) msg = self._create_message(msg) msg['Subject'] = self.subject msg['From'] = self.extra_headers.get('From', self.from_email) msg['To'] = self.extra_headers.get("To", ', '.join(self.to)) if self.cc: msg['Cc'] = ', '.join(self.cc) # Email header names are case-insensitive (RFC 2045), so we have to # accommodate that when doing comparisons. header_names = [key.lower() for key in self.extra_headers] if 'date' not in header_names: msg['Date'] = formatdate() if 'message-id' not in header_names: msg['Message-ID'] = make_msgid() for name, value in self.extra_headers.items(): if name.lower() in ('from', 'to') : # From and To are already handled continue msg[name] = value return msg
def sanitize_headers(msg): """ Set and modify headers on an :class:`email.Message` that need to be there no matter which list the message ends up being delivered to. Also remove headers that need to *not* be there. """ if 'message-id' not in msg: msg['Message-ID'] = make_msgid() if 'date' not in msg: msg['Date'] = formatdate() if 'subject' not in msg: msg['Subject'] = NO_SUBJECT if 'precedence' not in msg: msg['Precedence'] = 'list' del msg['domainkey-signature'] del msg['dkim-signature'] del msg['authentication-results'] del msg['list-id'] del msg['x-recipient'] del msg['x-subscriber'] # Preserve reply-to, but smoosh them together since only one reply-to header # is allowed. reply_to = set([address.lower() for real_name, address in getaddresses(msg.get_all('reply-to', []))]) del msg['reply-to'] if reply_to: msg['Reply-To'] = ", ".join(reply_to) # Remove various headers a la Mailman's cleansing. del msg['approved'] del msg['approve'] del msg['urgent'] del msg['return-receipt-to'] del msg['disposition-notification-to'] del msg['x-confirm-reading-to'] del msg['x-pmrqc'] del msg['archived-at']
def _set_headers(self, msg): msg['Subject'] = self.subject msg['From'] = self.extra_headers.get('From', self.from_email) msg['To'] = self.extra_headers.get('To', ', '.join(map(force_text, self.to))) if self.cc: msg['Cc'] = ', '.join(map(force_text, self.cc)) if self.reply_to: msg['Reply-To'] = self.extra_headers.get( 'Reply-To', ', '.join(map(force_text, self.reply_to))) # Email header names are case-insensitive (RFC 2045), so we have to # accommodate that when doing comparisons. header_names = [key.lower() for key in self.extra_headers] if 'date' not in header_names: msg['Date'] = formatdate() if 'message-id' not in header_names: # Use cached DNS_NAME for performance msg['Message-ID'] = make_msgid() for name, value in self.extra_headers.items(): if name.lower() in ('from', 'to'): # From and To are already handled continue msg[name] = value
def message(self): encoding = self.encoding or settings.DEFAULT_CHARSET msg = SafeMIMEText(smart_str(self.body, encoding), self.content_subtype, encoding) msg = self._create_message(msg) msg['Subject'] = self.subject msg['From'] = self.extra_headers.get('From', self.from_email) msg['To'] = self.extra_headers.get("To", ', '.join(self.to)) if self.cc: msg['Cc'] = ', '.join(self.cc) # Email header names are case-insensitive (RFC 2045), so we have to # accommodate that when doing comparisons. header_names = [key.lower() for key in self.extra_headers] if 'date' not in header_names: msg['Date'] = formatdate() if 'message-id' not in header_names: msg['Message-ID'] = make_msgid() for name, value in self.extra_headers.items(): if name.lower() in ('from', 'to'): # From and To are already handled continue msg[name] = value return msg
def ensure_message_id(msg): if get_message_id(msg) is None: msg.extra_headers['Message-ID'] = make_msgid()
def send_messages(self, messages): for message in messages: if 'message-id' not in message.extra_headers: message.extra_headers['Message-ID'] = make_msgid( domain='example.com') return super().send_messages(messages)
def message(self): self._msg['Date'] = formatdate(localtime=settings.EMAIL_USE_LOCALTIME) self._msg['Message-ID'] = make_msgid(domain=DNS_NAME) # Used for linking resent emails to the message self._msg[app_settings.MESSAGE_ID_HEADER_FIELD_NAME] = self._message_id return self._msg
def get_reply_headers(self, headers=None): headers = headers or {} headers['Message-ID'] = make_msgid() headers['Date'] = formatdate() headers['In-Reply-To'] = self.message_id.strip() return headers
def ensure_message_id(msg): if get_message_id(msg) is None: # Use cached DNS_NAME for performance msg.extra_headers['Message-ID'] = make_msgid(domain=DNS_NAME)