1

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','from_me@gmail.com','to_som1@gmail.com','my      subject','mymessage','my_existing_email@gmail.com','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?

Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92
flexxxit
  • 2,440
  • 5
  • 42
  • 69
  • 1
    Your question is tagged "Django"; is there some reason you're not using Django's [built-in e-mail sending](https://docs.djangoproject.com/en/dev/topics/email/#quick-example) (which prevents header injection among other conveniences). – supervacuo Aug 11 '12 at 18:57
  • yeah I tried but was giving me errors! – flexxxit Aug 11 '12 at 19:05
  • Can you help me to get it working using django ? – flexxxit Aug 11 '12 at 19:41
  • Certainly. Show us the code you were using with `send_mail`, and the error message (copy and paste of traceback) you get when you try. Probably best to edit it into your existing question. – supervacuo Aug 11 '12 at 21:17

4 Answers4

1

You want to use this:

FROMADDR = "%s <%s>" % (your_name, your_email)
ldiqual
  • 15,015
  • 6
  • 52
  • 90
  • This code does not work when I upload to heroku. Do you know why? 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 https://support.google.com/mail/bin/answer.py?answer=78754 et6sm2577249qab.8')" – flexxxit Aug 11 '12 at 19:24
  • It seems to be an issue with gmail authentification. Something to do with login/password, not about email headers. Double check your `LOGIN` and `PASSWORD` variables. Note that `LOGIN` is just your email address, not `Name `. – ldiqual Aug 11 '12 at 20:19
0

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@example.com>. See RFC 5322 for details.

Community
  • 1
  • 1
Jim
  • 72,985
  • 14
  • 101
  • 108
  • I uploaded to heroku and then when I try sending the mail I get the error "https://support.google.com/mail/bin/answer.py?answer=78754 z9sm2583649qae.15 – flexxxit Aug 11 '12 at 20:44
0

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

supervacuo
  • 9,072
  • 2
  • 44
  • 61
0

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.

finspin
  • 4,021
  • 6
  • 38
  • 66