0

I have a script that sends a .png file with SMTP. When I use a hotmail account;

smtplib.SMTP('smtp.live.com', 587)

It works without any problem. But when I use a gmail account;

smtplib.SMTP('smtp.gmail.com', 587)

An error raises: SMTPServerDisconnected: Connection unexpectedly closed

I've changed smtplib.SMTP('smtp.gmail.com', 587) to smtplib.SMTP('localhost') but didn't work. How can I fix this gmail problem?

GLHF
  • 3,835
  • 10
  • 38
  • 83
  • You should call `smtplib.starttls()`. See this as a reference: http://www.pythonforbeginners.com/google/sending-emails-using-google – DeepSpace May 12 '16 at 08:35
  • I don't know the lib you are using but when I connect to Gmail, I use TLS with the port 465. – foobar443 May 12 '16 at 08:37
  • Now I have new problem. It seems like there is a problem when try to login at password. `Raise SMTPAuthenticationError(code, resp)` It's about password applying I understand that. There is a really long traceback that tells me some weird smybols after then : `Please log in via your web browser and\n5.7.14 then try again.\n5.7.14` – GLHF May 12 '16 at 09:11
  • this could help you http://stackoverflow.com/questions/10147455/how-to-send-an-email-with-gmail-as-provider-using-python/12424439#12424439 http://stackoverflow.com/questions/26852128/smtpauthenticationerror-when-sending-mail-using-gmail-and-python – Suraj May 12 '16 at 09:13

2 Answers2

0

Try this code,its working fine for me,

import smtplib

## email sending function
def email_sender(input_message, email_to, client):
    ''' function to send email '''
    to = email_to
    gmail_user = '' ## email of sender account
    gmail_pwd = '' ## password of sender account
    smtpserver = smtplib.SMTP("smtp.gmail.com",587)
    smtpserver.ehlo()
    smtpserver.starttls()
    smtpserver.ehlo
    smtpserver.login(gmail_user, gmail_pwd)
    header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' +'Subject:site down! \n'
    input_message = input_message + client
    msg = header + input_message
    smtpserver.sendmail(gmail_user, to, msg)
    smtpserver.close()
  • Sign in to Gmail. Click the gear in the top right . Select Settings. Click Forwarding and POP/IMAP. Select Enable IMAP. Click Save Changes. –  May 12 '16 at 09:04
0

you can user smtplib and email for sending emails, this code working for me after i follow this steps.

steps are

  1. Sign in to Gmail.
  2. Click the gear in the top right .
  3. Select Settings.
  4. Click Forwarding and POP/IMAP.
  5. Select Enable IMAP. 6.Click Save Changes

 

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

me = "your email"
my_password = r"your password"
you = "to email id"

msg = MIMEMultipart('alternative')
msg['Subject'] = "Alert"
msg['From'] = me
msg['To'] = you

html = '<html><body><p>Hi, I have the following alerts for you!</p></body></html>'
part2 = MIMEText(html, 'html')

msg.attach(part2)
s = smtplib.SMTP_SSL('smtp.gmail.com')
s.login(me, my_password)

s.sendmail(me, you, msg.as_string())

print s

s.quit()
Kevin
  • 74,910
  • 12
  • 133
  • 166