Python google.appengine.api.mail.EmailMessage() Examples

The following are 14 code examples of google.appengine.api.mail.EmailMessage(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module google.appengine.api.mail , or try the search function .
Example #1
Source File: main.py    From pledgeservice with Apache License 2.0 6 votes vote down vote up
def post(self):
    data = json.loads(self.request.body)
    ascii_name = data["name"].encode('ascii', errors='ignore')
    ascii_email = data["email"].encode('ascii', errors='ignore')
    ascii_subject = data["subject"].encode('ascii', errors='ignore')
    ascii_body = data["body"].encode('ascii', errors='ignore')

    replyto = '%s <%s>' % (ascii_name, ascii_email)
    message = mail.EmailMessage(sender=('MayOne no-reply <noreply@%s.appspotmail.com>' %
                                           model.Config.get().app_name),
                                reply_to=replyto,
                                subject=ascii_subject)
    message.to = "info@mayone.us"
    message.body = 'FROM: %s\n\n%s' % (ascii_email, ascii_body)
    message.send()
    util.EnableCors(self)
    self.response.write('Ok.') 
Example #2
Source File: send_message.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def send_approved_mail(sender_address):
    # [START send_message]
    message = mail.EmailMessage(
        sender=sender_address,
        subject="Your account has been approved")

    message.to = "Albert Johnson <Albert.Johnson@example.com>"
    message.body = """Dear Albert:

Your example.com account has been approved.  You can now visit
http://www.example.com/ and sign in using your Google Account to
access new features.

Please let us know if you have any questions.

The example.com Team
"""
    message.send()
    # [END send_message] 
Example #3
Source File: gapps.py    From danforth-east with MIT License 6 votes vote down vote up
def send_email(to_address, to_name, subject, body_html):
    """Sends an email from the configured address.
    Does not check for address validity.
    """

    if config.ALLOWED_EMAIL_TO_ADDRESSES is not None and \
       to_address not in config.ALLOWED_EMAIL_TO_ADDRESSES:
        # Not allowed to send to this address
        logging.info('send_email: not allowed to send to: %s' % to_address)
        return

    full_to_address = '%s <%s>' % (to_name, to_address)

    h2t = html2text.HTML2Text()
    h2t.body_width = 0
    body_text = h2t.handle(body_html)

    message = mail.EmailMessage(sender=config.MASTER_EMAIL_SEND_ADDRESS,
                                subject=subject,
                                to=full_to_address,
                                body=body_text,
                                html=body_html)

    message.send() 
Example #4
Source File: env.py    From pledgeservice with Apache License 2.0 5 votes vote down vote up
def _send_mail(to, subject, text_body, html_body, reply_to=None):
  """Deferred email task"""
  sender = ('Mayday PAC <noreply@%s.appspotmail.com>' %
            model.Config.get().app_name)
  message = mail.EmailMessage(sender=sender, subject=subject)
  message.to = to
  message.body = text_body
  message.html = html_body
  if reply_to:
    message.reply_to = reply_to
  else:
    message.reply_to = 'info@mayday.us'
  message.send() 
Example #5
Source File: models_tests.py    From sndlatr with Apache License 2.0 5 votes vote down vote up
def test_remind(self):
        """
        remind should send remind and mark job as done.
        """
        job = self.create_job(state='queued')
        with mock_instance('sndlatr.gmail.Mailman') as mailman:
            mail = mock.create_autospec(spec=gae_mail.EmailMessage)
            mailman.build_reply.return_value = mail
            job.remind('token')
            mailman.send_mail.assert_called_with(mail)
            mailman.build_reply.assert_called_with(job.thread_id_int,
                                                   mock.ANY)
            mailman.quit.assert_called_with()
        job = job.key.get()
        self.assertEquals(job.state, 'done') 
Example #6
Source File: mailnotify.py    From sndlatr with Apache License 2.0 5 votes vote down vote up
def _send_failure_notice(job, subject_tpl, tpl_basepath, tpl_vars=None):
    message = mail.EmailMessage(sender=_sender,
                                to=job.user_email,
                                subject=subject_tpl.format(
                                    job.subject))
    localtime = _get_localtime(job)
    ctx = {'scheduled_at': localtime,
           'subject': job.subject}
    if tpl_vars is not None:
        ctx.update(tpl_vars)
    message.body = render_template('{}.txt'.format(tpl_basepath), **ctx)
    message.html = render_template('{}.html'.format(tpl_basepath), **ctx)
    logging.info(u'sending failure notice {} to {}'.format(
        tpl_basepath, job.user_email))
    message.send() 
Example #7
Source File: mailnotify.py    From sndlatr with Apache License 2.0 5 votes vote down vote up
def build_remind_message(remind_job):
    """
    Builds a reminder message for given remind_job.
    """
    message = mail.EmailMessage(sender=remind_job.user_email,
                                to=remind_job.user_email)
    ctx = {}
    message.body = render_template('reminder.txt', **ctx)
    message.html = render_template('reminder.html', **ctx)
    return message 
Example #8
Source File: gmail.py    From sndlatr with Apache License 2.0 5 votes vote down vote up
def build_reply(self, thread_id, mail=None):
        """
        Modify mail (gae mail.EmailMessage) to a reply of the given thread_id.
        If mail is None, creates a new, empty EmailMessage object.
        This looks up the last message in the given thread and modifies
        the References and Reply-To and subject headers of mail to be a reply
        to this message.
        """
        if mail is None:
            mail = gae_mail.EmailMessage()
        orig_msg = self.get_thread(thread_id, reverse=True, limit=1)
        if not orig_msg:
            raise MailNotFound('thread not found')
        orig_msg = orig_msg[0]

        # set References and In-Reply-To (see rfc rfc2822 3.6.4)
        orig_msg_id = orig_msg.get('rfc_message_id')
        if not orig_msg_id:
            raise RfcMsgIdMissing('Last mail in thread has no rfc message id')
        orig_references = orig_msg.get('references')
        if not orig_references:
            orig_references = orig_msg.get('in_reply_to')
        if not orig_references:
            orig_references = ''

        mail.headers = {'References': orig_references + '\r\n ' + orig_msg_id,
                        'In-Reply-To': orig_msg_id}
        mail.subject = orig_msg['subject']
        return mail 
Example #9
Source File: gmail.py    From sndlatr with Apache License 2.0 5 votes vote down vote up
def send_mail(self, mail):
        """ Sends gae mail.EmailMessage via SMTP. """
        smtp = self.open_smtp_session()
        smtp.send_rfc822(mail.to_mime_message())
        self.safe_smtp_quit(smtp) 
Example #10
Source File: email.py    From isthislegit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def send(self, **kwargs):
        ''' Sends an email using the App Engine Mail API.
        
        Raises an InvalidEmailError if an invalid email address was specified.
        '''
        message = mail.EmailMessage(**kwargs)
        message.send() 
Example #11
Source File: handle_incoming_email_test.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def test_handle_bounced_email(testbed):
    handler = handle_incoming_email.LogSenderHandler()
    handler.request = 'request'
    message = mail.EmailMessage(
        sender='support@example.com',
        subject='Your account has been approved')
    message.to = 'Albert Johnson <Albert.Johnson@example.com>'
    message.body = 'Dear Albert.'
    handler.receive(message) 
Example #12
Source File: users.py    From rest_gae with Apache License 2.0 5 votes vote down vote up
def _send_verification_email(self, user, email, reset_password=False):
            """Sends a verification email to a specific `user` with specific email details (in `email`). Creates a reset password link if `reset_password` is True."""

            # Prepare the verification URL
            user_id = user.get_id()
            token = self.user_model.create_signup_token(user_id)

            path_url = self.request.path_url
            path_url = path_url[:-len('verify')] if path_url.endswith('reset') else path_url
            path_url = path_url.rstrip('/')
            verification_params = { 'type': ('v' if not reset_password else 'p'), 'user_id': user_id, 'signup_token': token }
            verification_url = path_url + '/verify?' + urlencode(verification_params)

            # Prepare email body
            email['body_text'] = Template(email['body_text']).render(user=user, verification_url=verification_url)
            email['body_html'] = Template(email['body_html']).render(user=user, verification_url=verification_url)

            # Send the email
            if self.send_email_callback:
                # Use the provided function for sending the email
                self.send_email_callback(email)
            else:
                # Use GAE's email services
                message = mail.EmailMessage()
                message.sender = email['sender']
                message.to = user.email
                message.subject = email['subject']
                message.body = email['body_text']
                message.html = email['body_html']
                message.send() 
Example #13
Source File: email.py    From love with MIT License 5 votes vote down vote up
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() 
Example #14
Source File: mail_utils.py    From upvote with Apache License 2.0 4 votes vote down vote up
def Send(subject, body, to=None, cc=None, bcc=None, html=False):
  """Sends an email.

  Args:
    subject: The email subject.
    body: The email body.
    to: The TO address(es). Can be either a string or list of strings, and each
        string can be either a username or email address.
    cc: The CC address(es). Can be either a string or list of strings, and each
        string can be either a username or email address.
    bcc: The BCC address(es). Can be either a string or list of strings, and
        each string can be either a username or email address.
    html: Whether the body contains HTML or plain text.

  Raises:
    NoRecipientsError: if the to, cc, and bcc arguments are all empty.
  """
  message = mail.EmailMessage(
      sender=_SENDER,
      reply_to=_REPLY_TO,
      subject='%s %s' % (_SUBJECT_PREFIX, subject))

  if html:
    message.html = body
  else:
    message.body = body

  to = _SanitizeAddrs(to)
  cc = _SanitizeAddrs(cc)
  bcc = _SanitizeAddrs(bcc)

  if to:
    message.to = to
  if cc:
    message.cc = cc
  if bcc:
    message.bcc = bcc

  # Make sure we're actually sending this to someone.
  recipients = sorted(list(set(to + cc + bcc)))
  if not recipients:
    raise NoRecipientsError

  try:
    logging.info('Sending email to %s', recipients)
    message.check_initialized()
    if env_utils.RunningInProd():
      message.send()

  # If something blows up, log it and move on. Failure to send an email is not
  # something that should take the caller off the rails.
  except Exception:  # pylint: disable=broad-except
    logging.exception('Error encountered while sending email')