def send_mail_as_noreply(address, subject, msg=None, html=None, attachments=None): logging.info(u"Sending email to %s: [%s] %s" % (address, subject, msg)) try: mail = EmailMessage() mail.sender = NO_REPLY_SENDER mail.to = address mail.subject = subject if attachments: mail.attachments = attachments if html: mail.html = html elif msg: mail.body = msg else: mail.body = "---" # mail.body cannot be empty string, raises exception mail.check_initialized() mail.send() except Exception, e: logging.error("Email sending failed: %s" % e.message)
def sendMail(emailId, subject, body): emailMessage = EmailMessage() emailMessage.sender = "Support <*****@*****.**>" emailMessage.to = emailId emailMessage.subject = subject emailMessage.html = body return emailMessage.send()
def sendMail(emailId,subject,body): emailMessage = EmailMessage() emailMessage.sender = "Support <*****@*****.**>" emailMessage.to = emailId emailMessage.subject = subject emailMessage.html = body return emailMessage.send()
def send_appengine_email(sender, recipient, subject, body_html, body_text): email = EmailMessage() email.sender = sender email.to = recipient email.subject = subject email.body = body_text email.html = body_html email.send()
def translate_message(self, msg): sender = msg.get_unixfrom() or msg['From'] if not sender: if self.fix_sender: sender = self.default_sender else: raise BadMessageError("No sender specified") to = msg['To'] if not to: raise BadMessageError("No destination addresses specified") message = EmailMessage(sender=sender or msg['From'], to=to) # Go through all the headers which Google will let us use cc = msg['Cc'] if cc: message.cc = cc bcc = msg['Bcc'] if bcc: message.bcc = cc reply_to = msg['Reply-To'] if reply_to: message.reply_to = reply_to subject = msg['Subject'] if subject: message.subject = subject # If there's just a plain text body, use that, otherwise # iterate over all the attachments payload = msg.get_payload(decode=True) if isinstance(payload, basestring): message.body = payload else: body = '' html = '' attachments = [] # GAE demands we specify the body explicitly - we use the first text/plain attachment we find. # Similarly, we pull in the first html we find and use that for message.html # We pull in any other attachments we find; but we ignore the multipart structure, # because GAE doesn't give us enough control there. for part in msg.walk(): if part.get_content_type() == 'text/plain' and not body: body = part.get_payload(decode=True) elif part.get_content_type() == 'text/html' and not html: html = part.get_payload(decode=True) elif not part.get_content_type().startswith('multipart'): attachments.append( (get_filename(part), part.get_payload(decode=True))) if not body: raise BadMessageError("No message body specified") message.body = body if html: message.html = html if attachments: message.attachments = attachments return message
def _gae_send(self, sender, to, subject, body, html): from google.appengine.api.mail import EmailMessage message = EmailMessage() message.sender = sender message.subject = subject message.body = body message.html = html message.to = to try: message.send() except Exception as ex: logging.warning(ex, exc_info=ex)
def translate_message(self, msg): sender = msg.get_unixfrom() or msg["From"] if not sender: if self.fix_sender: sender = self.default_sender else: raise BadMessageError("No sender specified") to = msg["To"] if not to: raise BadMessageError("No destination addresses specified") message = EmailMessage(sender=sender or msg["From"], to=to) # Go through all the headers which Google will let us use cc = msg["Cc"] if cc: message.cc = cc bcc = msg["Bcc"] if bcc: message.bcc = cc reply_to = msg["Reply-To"] if reply_to: message.reply_to = reply_to subject = msg["Subject"] if subject: message.subject = subject # If there's just a plain text body, use that, otherwise # iterate over all the attachments payload = msg.get_payload(decode=True) if isinstance(payload, basestring): message.body = payload else: body = "" html = "" attachments = [] # GAE demands we specify the body explicitly - we use the first text/plain attachment we find. # Similarly, we pull in the first html we find and use that for message.html # We pull in any other attachments we find; but we ignore the multipart structure, # because GAE doesn't give us enough control there. for part in msg.walk(): if part.get_content_type() == "text/plain" and not body: body = part.get_payload(decode=True) elif part.get_content_type() == "text/html" and not html: html = part.get_payload(decode=True) elif not part.get_content_type().startswith("multipart"): attachments.append((get_filename(part), part.get_payload(decode=True))) if not body: raise BadMessageError("No message body specified") message.body = body if html: message.html = html if attachments: message.attachments = attachments return message
def get(self): config = memcache.get('config') or Configuration.all().get() if not config: # We haven't set up our configuration yet, so don't try to do anything return elink_api = elink_appengine.AppEngineAPI(api_key=(config.key_id, config.vcode)) elink_char = evelink.char.Char(config.rcpt_char, api=elink_api) elink_eve = evelink.eve.EVE(api=elink_api) recips = set([config.rcpt_org]) if config.rcpt_org2: recips.add(config.rcpt_org2) headers = elink_char.messages() message_ids = set(h['id'] for h in headers if h['to']['org_id'] in recips) headers = dict((h['id'], h) for h in headers) message_ids_to_relay = set() sender_ids = set() for m_id in message_ids: seen = memcache.get('seen-%s' % m_id) or SeenMail.gql("WHERE mail_id = :1", m_id).get() if not seen: message_ids_to_relay.add(m_id) sender_ids.add(headers[m_id]['sender_id']) else: memcache.set('seen-%s' % m_id, True) if not message_ids_to_relay: self.response.out.write("No pending messages.") return bodies = elink_char.message_bodies(message_ids_to_relay) senders = elink_eve.character_names_from_ids(sender_ids) e = EmailMessage() e.to = config.dest_email e.sender = '*****@*****.**' for m_id in message_ids_to_relay: sender = senders[headers[m_id]['sender_id']] timestamp = headers[m_id]['timestamp'] e.subject = '[EVEMail] %s' % headers[m_id]['title'] e.html = self.format_message(bodies[m_id] or '', timestamp, sender) e.send() SeenMail(mail_id=m_id).put() memcache.set('seen-%s' % m_id, True) self.response.out.write("Processed message ID %s.<br/>\n" % m_id) return
def send_applicant_notification_email(self): # Super hacky to ask for request_handler but it works for now message = EmailMessage() if self.House == "Men's Christian Campus House": message.sender = "CCH Housing Application <*****@*****.**>" message.to = self.EmailAddress message.subject = "Thank you for applying to the Rolla CCH!" else: message.sender = "WCCH Housing Application <*****@*****.**>" message.to = self.EmailAddress message.subject = "Thank you for applying to the Rolla WCCH!" message.html = self._generate_applicant_notification_email_html() message.body = self._generate_applicant_notification_email_text() message.send()
def send_staff_notification_email(self, request_handler): # Super hacky to ask for request_handler but it works for now message = EmailMessage() if self.House == "Men's Christian Campus House": message.sender = "CCH Housing Application <*****@*****.**>" message.to = request_handler.settings.HousingApplicationCch_CompletionEmail message.subject = "CCH Housing Application (%s)" % self.FullName else: message.sender = "WCCH Housing Application <*****@*****.**>" message.to = request_handler.settings.HousingApplicationWcch_CompletionEmail message.subject = "WCCH Housing Application (%s)" % self.FullName message.html = self._generate_staff_notification_email_html() message.body = self._generate_staff_notification_email_text() message.send()
def _gae_send_mail(sender, to, cc=None, bcc=None, reply_to=None, subject='', body='', html='', attachments=[], headers={}): email = EmailMessage() email.sender = sender email.to = to email.subject = subject email.body = body if cc: email.cc = cc if bcc: email.bcc = bcc if reply_to: email.reply_to = reply_to if html: email.html = html if attachments: email.attachments = attachments if headers: email.headers = headers return email.send()
def _gae_send_mail(sender, to, cc=None, bcc=None, reply_to=None, subject='', body='', html='', attachments=[], headers={}): email = EmailMessage() email.sender = sender email.to = to email.subject = subject email.body = body if cc: email.cc = cc if bcc: email.bcc = bcc if reply_to: email.reply_to = reply_to if html: email.html = html if attachments: email.attachments = attachments if headers: email.headers = headers return email.send()
def send_staff_ref_notification_email(self, request_handler, ref_type): if ref_type not in ['c', 'o']: raise ValueError # Super hacky to ask for request_handler but it works for now message = EmailMessage() if self.House == "Men's Christian Campus House": message.sender = "CCH Housing Application <*****@*****.**>" message.to = request_handler.settings.HousingApplicationCch_CompletionEmail message.subject = "CCH Housing Reference (%s)" % self.FullName else: message.sender = "WCCH Housing Application <*****@*****.**>" message.to = request_handler.settings.HousingApplicationWcch_CompletionEmail message.subject = "WCCH Housing Reference (%s)" % self.FullName message.html = self._generate_staff_ref_notification_html(ref_type) message.body = self._generate_staff_ref_notification_text(ref_type) message.send()
def send_notifications(self, config, elink_api, elink_char, elink_eve, notify_descriptions): headers = elink_char.notifications() message_ids = set(headers[h]['id'] for h in headers if headers[h]['type_id'] in config.notify_types ) headers = dict((headers[h]['id'], headers[h]) for h in headers) message_ids_to_relay = set() sender_ids = set() for m_id in message_ids: seen = (memcache.get('nseen-%s' % m_id) or SeenNotification.gql("WHERE notify_id = :1", m_id).get()) if not seen: message_ids_to_relay.add(m_id) sender_ids.add(headers[m_id]['sender_id']) else: memcache.set('nseen-%s' % m_id, True) if not message_ids_to_relay: self.response.out.write("No pending notifications.<br/>") return bodies = elink_char.notification_texts(message_ids_to_relay) senders = elink_eve.character_names_from_ids(sender_ids) e = EmailMessage() e.to = config.dest_email e.sender = '*****@*****.**' for m_id in message_ids_to_relay: sender = senders[headers[m_id]['sender_id']] timestamp = headers[m_id]['timestamp'] e.subject = ('[EVE Notify] %s' % notify_descriptions.filter( "type_id = ", headers[m_id]['type_id']).get().description) e.html = self.format_notification(bodies[m_id], timestamp, sender, elink_eve) e.send() SeenNotification(notification_id=m_id).put() memcache.set('nseen-%s' % m_id, True) self.response.out.write("Processed notification ID %s.<br/>\n" % m_id) return
def send_emails(self, config, elink_api, elink_char, elink_eve): recips = set([config.rcpt_org]) if config.rcpt_org2: recips.add(config.rcpt_org2) headers = elink_char.messages() message_ids = set(h['id'] for h in headers if h['to']['org_id'] in recips) headers = dict((h['id'], h) for h in headers) message_ids_to_relay = set() sender_ids = set() for m_id in message_ids: seen = memcache.get('seen-%s' % m_id) or SeenMail.gql("WHERE mail_id = :1", m_id).get() if not seen: message_ids_to_relay.add(m_id) sender_ids.add(headers[m_id]['sender_id']) else: memcache.set('seen-%s' % m_id, True) if not message_ids_to_relay: self.response.out.write("No pending messages.<br/>") return bodies = elink_char.message_bodies(message_ids_to_relay) senders = elink_eve.character_names_from_ids(sender_ids) e = EmailMessage() e.to = config.dest_email e.sender = '*****@*****.**' for m_id in message_ids_to_relay: sender = senders[headers[m_id]['sender_id']] timestamp = headers[m_id]['timestamp'] e.subject = '[EVEMail] %s' % headers[m_id]['title'] e.html = self.format_message(bodies[m_id], timestamp, sender) e.send() SeenMail(mail_id=m_id).put() memcache.set('seen-%s' % m_id, True) self.response.out.write("Processed message ID %s.<br/>\n" % m_id) return
def send(self): """ Build and send an email message from this model instance """ # create new email message new_email = EmailMessage() # set sender address new_email.sender = self.get_sender() # set to addresses new_email.to = self.to # set cc addresses if self.cc: new_email.cc = self.cc # set bcc addresses if self.bcc: new_email.bcc = self.bcc # set reply to address if self.reply_to: new_email.reply_to = self.reply_to else: new_email.reply_to = self.get_sender() # set email subject new_email.subject = self.subject # set plaintext body if self.body_plain: new_email.body = self.body_plain # set html body if self.body_html: new_email.html = self.body_html # check that the email has been properly initialized and send new_email.check_initialized() new_email.send()
def get(self): time_delay = datetime.timedelta(days=self.settings.HousingApplication_ReminderEmailDelayDays) time_offset = datetime.datetime.utcnow() - time_delay unacknowledged_apps = HousingApplication.query( HousingApplication.Stage == 0, HousingApplication.TimeSubmitted < time_offset ).fetch(20) message_html = self.generate_html(unacknowledged_apps) self.response.out.write(message_html) if len(unacknowledged_apps) > 0: message = EmailMessage() message.sender = "CCF Housing Application Reminder <*****@*****.**>" message.to = [self.settings.HousingApplicationCch_CompletionEmail, self.settings.HousingApplicationWcch_CompletionEmail] message.subject = "CCF Housing Application Reminder ({date})".format( date=datetime.datetime.now(tz=Central).strftime('%b-%d-%Y'), ) message.html = message_html message.send()
def send_appengine_email(**kwargs): receiver = kwargs.get("receiver") sender = kwargs.get("sender", EmailManager.default_sender) subject = kwargs.get("subject") # email content text_template = kwargs.get("text_template") html_template = kwargs.get("html_template", "") # we must format the email address in this way receiver_address = "{0} <{1}>".format(receiver["name"], receiver["email"]) sender_address = "{0} <{1}>".format(sender["name"], sender["email"]) try: # create a new email object message = EmailMessage(sender=sender_address, to=receiver_address, subject=subject) # add the text body message.body = text_template if html_template: message.html = html_template if DEV: info(message.body) # send the email # on dev the email is not actually sent just logged in the terminal message.send() return True except Exception, e: warn(e) return False
def get(self): time_delay = datetime.timedelta( days=self.settings.HousingApplication_ReminderEmailDelayDays) time_offset = datetime.datetime.utcnow() - time_delay unacknowledged_apps = HousingApplication.query( HousingApplication.Stage == 0, HousingApplication.TimeSubmitted < time_offset).fetch(20) message_html = self.generate_html(unacknowledged_apps) self.response.out.write(message_html) if len(unacknowledged_apps) > 0: message = EmailMessage() message.sender = "CCF Housing Application Reminder <*****@*****.**>" message.to = [ self.settings.HousingApplicationCch_CompletionEmail, self.settings.HousingApplicationWcch_CompletionEmail ] message.subject = "CCF Housing Application Reminder ({date})".format( date=datetime.datetime.now(tz=Central).strftime('%b-%d-%Y'), ) message.html = message_html message.send()
def send_mail_as_noreply(address, subject, msg=None, html=None, attachments=None): logging.info(u"Sending email to %s: [%s] %s" % (address, subject, msg)) try: mail = EmailMessage() mail.sender = NO_REPLY_SENDER mail.to = address mail.subject = subject if attachments: mail.attachments = attachments if html: mail.html = html elif msg: mail.body = msg else: mail.body = "---" # mail.body cannot be empty string, raises exception mail.check_initialized() mail.send() except Exception, e: logging.error("Email sending failed: %s" % e.message)
def send_reference_email(self, ref_type): # disable automatic references until we are ready return if ref_type not in ['c', 'o']: raise ValueError if ref_type == 'c': to_email = self.HomeChurchEmail else: to_email = self.OtherReferenceEmail if not to_email: return message = EmailMessage() message.sender = "Housing Application <*****@*****.**>" message.to = to_email if self.House == "Men's Christian Campus House": message.subject = "CCH Housing Application Reference for {name}".format(name=self.FullName) else: message.subject = "WCCH Housing Application Reference for {name}".format(name=self.FullName) message.html = self._generate_reference_email_html(ref_type) message.body = self._generate_reference_email_text(ref_type) message.send()
def send_email(l): """Send an email notifying the recipient of l about their love.""" sender_future = l.sender_key.get_async() recipient_future = l.recipient_key.get_async() # Remove this love from recent_love if present (datastore is funny sometimes) recent_love = recent_received_love(l.recipient_key, limit=4).get_result() index_to_remove = None for i, love in enumerate(recent_love): if l.sender_key == love.sender_key and l.recipient_key == love.recipient_key and l.message == love.message: index_to_remove = i break if index_to_remove is not None: del recent_love[index_to_remove] sender = sender_future.get_result() recipient = recipient_future.get_result() email = EmailMessage() email.sender = config.LOVE_SENDER_EMAIL email.to = recipient.user.email() email.subject = u'Love from {}'.format(sender.full_name) email.body = u'"{}"\n\n{}'.format(l.message, '(Sent secretly)' if l.secret else '') email.html = render_template('email.html', love=l, sender=sender, recipient=recipient, recent_love_and_lovers=[ (love, love.sender_key.get()) for love in recent_love[:3] ]) email.send()