0

So I was trying to send a basic email with python with the folllowing code but it gives me an error sugesting I sign in using a browser . I did it and it stil spit out the same error. The code:

import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("projtig6@gmail.com", "redacted")

msg = "Your security system has detected an intrusion of some kind please refere to http://projetotig6.orgfree.com/ for an image of the current situation"
server.sendmail("projtig6@gmail.com", "grenskul@gmail.com", msg)
server.quit()

And it gives me this when I run it

user@ubuntu:~/Desktop$ python3 tent.py
Traceback (most recent call last):
  File "tent.py", line 5, in <module>
    server.login("projtig6@gmail.com", "redacted")
  File "/usr/lib/python3.4/smtplib.py", line 639, in login
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtT\n5.7.14 NJKUGchcZYhLMqcl9utRyvWa6jwkzOCuhBtdP3URW63HNIVrSlue8To8yKxxbDIwvlnO2g\n5.7.14 V2GooN21jsn0X8_d6W_CxGwuOXmBrkoDzFCqqbB72xz3MbY0Kj3Z7ZzdXlQCc8sjdavwes\n5.7.14 bUlIhA8GIqRKJAyBbildXtsSqF8Fh_7AYPI0W3SKlidhoUOK7o4hI0IIUmgVdqOCpFpxyU\n5.7.14 fvShqxoTn6W_bAWqXfS5akND40-gQ> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14  Learn more at\n5.7.14  https://support.google.com/mail/answer/78754 c142sm10923092wme.18 - gsmtp')
danidee
  • 9,298
  • 2
  • 35
  • 55
grenskul
  • 31
  • 2
  • 1
    https://support.google.com/mail/answer/78775?hl=en – danidee Jun 12 '16 at 21:51
  • Forgot to mention I already followed everything on that page . I activated POP and IMAP . Can connect to the SMTP server not sending attatchments . The only thing I tought might be causing the error is the ssl/TSL thing but I didn't understand it so I posted here . – grenskul Jun 12 '16 at 22:03
  • you do realize that you're showing your password in public... – danidee Jun 12 '16 at 22:05
  • it's not the actual password . Even if it was it's a throwaway mail . – grenskul Jun 12 '16 at 22:08

1 Answers1

0

First, make sure you "allow less secure apps to authenticate to your gmail": https://support.google.com/accounts/answer/6010255?hl=en

If that's not the issue, try using this wrapper I wrote around sending emails in python; it makes sending emails super easy: https://github.com/krystofl/krystof-utils/blob/master/python/krystof_email_wrapper.py

Use it like so:

emailer = Krystof_email_wrapper()

email_body = 'Hello, <br /><br />' \
             'This is the body of the email.'

emailer.create_email('sender@example.com', 'Sender Name',
                     ['recipient@example.com'],
                     email_body,
                     'Email subject!',
                     attachments = ['filename.txt'])

cred = { 'email': 'sender@example.com', 'password': 'VERYSECURE' }

emailer.send_email(login_dict = cred, force_send = True)

You can also look at the source code to find how it works. The relevant bits:

import email
import smtplib   # sending of the emails
from email.mime.multipart   import MIMEMultipart
from email.mime.text        import MIMEText

self.msg = MIMEMultipart()

self.msg.preamble   = "This is a multi-part message in MIME format."

# set the sender
author = email.utils.formataddr((from_name, from_email))
self.msg['From'] = author

# set recipients (from list of email address strings)
self.msg['To' ] = ', '.join(to_emails)
self.msg['Cc' ] = ', '.join(cc_emails)
self.msg['Bcc'] = ', '.join(bcc_emails)

# set the subject
self.msg['Subject'] = subject

# set the body
msg_text = MIMEText(body.encode('utf-8'), 'html', _charset='utf-8')
self.msg.attach(msg_text)

# send the email
session = smtplib.SMTP('smtp.gmail.com', 587)
session.ehlo()
session.starttls()
session.login(login_dict['email'], login_dict['password'])
session.send_message(self.msg)
session.quit()
StaringFrog
  • 561
  • 7
  • 21