Here's my script:
#!/usr/bin/python import smtplib msg = 'Hello world.' server = smtplib.SMTP('smtp.gmail.com',587) #port 465 or 587 server.ehlo() server.starttls() server.ehlo() server.login('[email protected]','mypass') server.sendmail('[email protected]','[email protected]',msg) server.close()
I'm just trying to send an email from my gmail account. The script uses starttls because of gmail's requirement. I've tried this on two web hosts, 1and1 and webfaction. 1and1 gives me a 'connection refused' error and webfaction reports no error but just doesn't send the email. I can't see anything wrong with the script, so I'm thinking it might be related to the web hosts. Any thoughts and comments would be much appreciated.
EDIT: I turned on debug mode. From the output, it looks like it sent the message successfully...I just never receive it.
send: 'ehlo web65.webfaction.com\r\n' reply: '250-mx.google.com at your service, [174.133.21.84]\r\n' reply: '250-SIZE 35651584\r\n' reply: '250-8BITMIME\r\n' reply: '250-STARTTLS\r\n' reply: '250-ENHANCEDSTATUSCODES\r\n' reply: '250 PIPELINING\r\n' reply: retcode (250); Msg: mx.google.com at your service, [174.133.21.84] SIZE 35651584 8BITMIME STARTTLS ENHANCEDSTATUSCODES PIPELINING send: 'STARTTLS\r\n' reply: '220 2.0.0 Ready to start TLS\r\n' reply: retcode (220); Msg: 2.0.0 Ready to start TLS send: 'ehlo web65.webfaction.com\r\n' reply: '250-mx.google.com at your service, [174.133.21.84]\r\n' reply: '250-SIZE 35651584\r\n' reply: '250-8BITMIME\r\n' reply: '250-AUTH LOGIN PLAIN\r\n' reply: '250-ENHANCEDSTATUSCODES\r\n' reply: '250 PIPELINING\r\n' reply: retcode (250); Msg: mx.google.com at your service, [174.133.21.84] SIZE 35651584 8BITMIME AUTH LOGIN PLAIN ENHANCEDSTATUSCODES PIPELINING send: 'AUTH PLAIN *****\r\n' reply: '235 2.7.0 Accepted\r\n' reply: retcode (235); Msg: 2.7.0 Accepted send: 'mail FROM:<[email protected]> size=12\r\n' reply: '250 2.1.0 OK 4sm652580yxq.48\r\n' reply: retcode (250); Msg: 2.1.0 OK 4sm652580yxq.48 send: 'rcpt TO:<[email protected]>\r\n' reply: '250 2.1.5 OK 4sm652580yxq.48\r\n' reply: retcode (250); Msg: 2.1.5 OK 4sm652580yxq.48 send: 'data\r\n' reply: '354 Go ahead 4sm652580yxq.48\r\n' reply: retcode (354); Msg: Go ahead 4sm652580yxq.48 data: (354, 'Go ahead 4sm652580yxq.48') send: 'Hello world.\r\n.\r\n' reply: '250 2.0.0 OK 1240421143 4sm652580yxq.48\r\n' reply: retcode (250); Msg: 2.0.0 OK 1240421143 4sm652580yxq.48 data: (250, '2.0.0 OK 1240421143 4sm652580yxq.48')
Python offers a ` library to send emails- “SMTP lib”. “smtplib” creates a Simple Mail Transfer Protocol client session object which is used to send emails to any valid email id on the internet. The Port number used here is '587'. And if you want to send mail using a website other than Gmail.
Your code looks correct but sometimes google blocks an IP when you try to send a email from an unusual location. You can try to unblock it by visiting https://accounts.google.com/DisplayUnlockCaptcha from the IP and following the prompts.
The SMTP Protocol This may not come as a surprise, but of course Python already has a library that lets you connect to an SMTP server, like the one Gmail uses. This library is called, predictably, smtplib and comes included with Python.
The smtplib module defines an SMTP client session object that can be used to send mail to any internet machine with an SMTP or ESMTP listener daemon. For details of SMTP and ESMTP operation, consult RFC 821 (Simple Mail Transfer Protocol) and RFC 1869 (SMTP Service Extensions). Availability: not Emscripten, not WASI.
Some self-promotion here, but I feel on a valid ground.
You would literally only need this code to do exactly what you wrote:
import yagmail yag = yagmail.SMTP('[email protected]') yag.send('[email protected]', subject = None, contents = 'Hello')
Or a one liner:
yagmail.SMTP('[email protected]').send('[email protected]', None, 'Hello world.')
What is nice is that I propose to use keyring to store your password, so you never have a risk of people seeing your password in your script.
You can set this up by running once in your interpreter:
import yagmail yagmail.register("[email protected]", "mypassword")
and exit. Then you can just use:
import yagmail yagmail.SMTP("[email protected]") # without password
If you add .yagmail with "[email protected]" in your home dir, then you can just do: yagmail.SMTP()
, but that's rather pointless by now.
Warning: If you get serious about sending a lot of messages, better set up OAuth2, yagmail can help with that.
yagmail.SMTP("[email protected]", oauth2_file="/path/to/save/creds.json")
The first time ran, it will guide you through the process of getting OAuth2 credentials and store them in the file so that next time you don't need to do anything with it. Do you suspect someone found your credentials? They'll have limited permissions, but you better invalidate their credentials through gmail.
For the package/installation please look at git or readthedocs, available for both Python 2 and 3.
Have you tried constructing a valid message?
from email.MIMEText import MIMEText msg = MIMEText('body') msg['Subject'] = 'subject' msg['From'] = "..." msg['Reply-to'] = "..." msg['To'] = "..."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With