Sending Email in Google AppEngine for Python

Google AppEngine provides an API services for sending emails. I’ve setup a contact form to use such feature. I used a couple of validations, security tokens via custom sessions and the mail service to implement this feature in my site.

The config

Below is my configuration the the contact form in YAML format.

--- 
sender_name: 'Lysender'
sender_email: 'lysender@email'
receiver_name: 'Lysender'
receiver_email: 'lysender@email'
subject_line: 'Somebody contacted you via www.lysender.com'
message_body: |
  %s contacted you via www.lysender.com

  Name: %s
  Email: %s
  Message:

  %s

  --
  Contact form

The code

And below is my code for actually sending the email.

contact_config = dclab.get_yaml_config('contact.yaml')
self.validate_post(name, email, message)

sender_line = '%s <%s>' % (contact_config['sender_name'], contact_config['sender_email'])
receiver_line = '%s <%s>' % (contact_config['receiver_name'], contact_config['receiver_email'])

# Send the email now
msg = mail.EmailMessage(sender=sender_line,
                        subject=contact_config['subject_line'])
msg.to = receiver_line
msg.body = contact_config['message_body'] % (name, name, email, message)
msg.send()

In development sever, it doesn’t send an email by default. It just logs the email on the terminal. In production, it works flawlessly. The service will send an email in behalf of the admin email.

This entry was posted in Python and tagged , , , , . Bookmark the permalink.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *