rok
rok

Reputation: 597

Sending multiple emails in a row in GAE

I have a GAE based app (python) and I'm trying to implement automatic email sending. When only one email is sent it works fine (both local and deployed). However, when I try to send two emails in a row the script only works on my local dev server. If the application is deployed to Google a strange mixup occurs. It seems like the body of the first email is sent to the recipient of the second one and then the application throws an internal server error.

Here's my code:

class MailSender(webapp.RequestHandler):
def get(self):
        firstname=self.request.get('firstname')
        lastname=self.request.get('lastname')
        email=self.request.get('email')
        city=self.request.get('city')
        state=self.request.get('state')
        country=self.request.get('country')        

        sender_address = firstname+" "+lastname+" <"+users.get_current_user().email()+">"
        subject = "Subject 1"
        body = "Name: "+firstname+"\nLast name: "+lastname+"\nEmail: "+email+"\nCity: "+city+"\nState: "+state+"\nCountry: "+country
        mail.send_mail(sender_address, "[email protected]", subject, body)

        sender_address1="[email protected]"
        subject1="Subject 2"
        body2="message"    
        mail.send_mail(sender_address1, email, subject1, body1)

        self.response.out.write('{"status":"true"}')

Upvotes: 1

Views: 367

Answers (2)

HayatoY
HayatoY

Reputation: 527

It seems you didn't set a valid sender address in second mail.

https://cloud.google.com/appengine/docs/python/mail/sendingmail

The sender address must be one of the following types:

  • The address of a registered administrator for the application. You can add administrators to an application using the Administration Console.
  • The address of the user for the current request signed in with a Google Account. You can determine the current user's email address with the Users API. The user's account must be a Gmail account, or be on a domain managed by Google Apps.
  • Any valid email receiving address for the app (such as [email protected]).
  • Any valid email receiving address of a domain account, such as [email protected]. Domain accounts are accounts outside of the Google domain with email addresses that do not end in @gmail.com or @APP-ID.appspotmail.com.

Upvotes: 1

Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26647

You might need to use the EmailMessage object the way you see here. Did you try using a loop to send emails? The following works for me.

for detail in details:
    subject = detail.title or 'Reminder'
    message = mail.EmailMessage(sender='Business <[email protected]>', subject=subject)
    message.body = """
    Hello!
    """ 
    message.to = detail.email
    message.send()

Upvotes: 0

Related Questions