Baama
Baama

Reputation: 2642

Sending email not working on heroku

I have this function in forms.py. There is currently no email specifications in my settings.py.

def send_email(FROM_NAME,FROM,TO,SUB,MSG,EXISTING_EMAIL,EXISTING_PASSWORD):

    FROMADDR = "%s <%s>" % (FROM_NAME, FROM)
    LOGIN    = EXISTING_EMAIL
    PASSWORD = EXISTING_PASSWORD
    TOADDRS  = [TO]
    SUBJECT  = SUB

    msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"  % (FROMADDR, ", ".join(TOADDRS), SUBJECT) )
    msg +=  MSG+"\r\n"

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.set_debuglevel(1)
    server.ehlo()
    server.starttls()
    server.login(LOGIN, PASSWORD)
    server.sendmail(FROMADDR, TOADDRS, msg)
    server.quit()

I call it my views.py like so

send_email('my_name','[email protected]','[email protected]','my      subject','mymessage','[email protected]','password_to_existing_email')

This works locally. I have tested it with yahoomail and gmail. But when I upload to heroku it gives the error "(535, '5.7.1 Please log in with your web browser and then try again. Learn more at\n5.7.1 support.google.com/mail/bin/answer.py?answer=78754 et6sm2577249qab.8')"

Can anyone help?

Upvotes: 1

Views: 2655

Answers (4)

finspin
finspin

Reputation: 4061

Here is what worked for me. After getting the error Please log in with your web browser and then try again. Learn more etc. when trying to send email from my web application, I logged in to the email via browser from my local computer.

After I logged in, there was a yellow notification bar on top which asking me if I want to allow external application access my mail. I confirmed this and Google asked me to log in to the account from the application within the next 10 mins. This will white-list the application.

Upvotes: 0

supervacuo
supervacuo

Reputation: 9242

Have you read the page linked to in the error message?

If you're repeatedly prompted for your username and password, or if you're getting an 'invalid credentials' or 'web login required' error, make sure your password is correct. Keep in mind that password are case-sensitive.

If you’re sure your password is correct, sign in to your account from the web version of Gmail instead at http://mail.google.com

In most cases signing in from the web should resolve the issue

Upvotes: 0

Jim
Jim

Reputation: 73936

You shouldn't be building emails with string interpolation, that's a good way to get your site used to send spam via header injections. See my answer here for details on how to construct emails securely.

Generally speaking, when formatting from addresses, you should use the format Display Name <[email protected]>. See RFC 5322 for details.

Upvotes: 0

ldiqual
ldiqual

Reputation: 15365

You want to use this:

FROMADDR = "%s <%s>" % (your_name, your_email)

Upvotes: 1

Related Questions