[Tutor] How to send an email using Python

Senthil Kumaran orsenthil at gmail.com
Wed Mar 11 05:28:53 CET 2009


Hello Samuel,

When sending through smtp.gmail.com you might need to starttls() and
do a login() before sending.

>>> import smtplib
>>> headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" %(from,to,subject)
>>> message = headers + "Hello, How are you?"
>>> mailserver = smtplib.SMTP('smtp.gmail.com')
>>> mailserver.ehlo()
>>> mailserver.starttls()
>>> mailserver.login('username','password')
>>> mailserver.sendmail(from,to,message)

As I try it now, startls() was giving me a message " reply: '454 TLS
not available due to temporary reason\r\n'"
- As the error message says, it could be temporary.

Try it at your end and report if you succeed.

Thanks,
Senthil

On Tue, Mar 10, 2009 at 4:41 PM, Samuel Avinash <samuelavinash at in.com> wrote:
> Hi,
> Can anybody tell me how to send an email to a recipient using Python
> I am trying to send an email using Gmail
> I create an instance of  smtplib and use :
> x=smtplib.SMTP(sever,port)
> and then
> x.login(user,pwd)
> I try to send the email using
> x..sendmail(SENDER, RECIPIENTS, msg)
> But I get the following error
> Traceback (most recent call last):
> File "C:UsersAvisDesktopMail.py", line 13, in <module>
> session = smtplib.SMTP(smtpserver,port)
> File "C:Python26libsmtplib.py", line 239, in __init__
> (code, msg) = self.connect(host, port)
> File "C:Python26libsmtplib.py", line 295, in connect
> self.sock = self._get_socket(host, port, self.timeout)
> File "C:Python26libsmtplib.py", line 273, in _get_socket
> return socket.create_connection((port, host), timeout)
> File "C:Python26libsocket.py", line 498, in create_connection
> for res in getaddrinfo(host, port, 0, SOCK_STREAM):
> socket.gaierror: [Errno 11001] getaddrinfo failed
>
> This is in Windows Vista and I am trying to connect to "smtp.gmail.com" with
> port 465
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>



-- 
-- 
Senthil


More information about the Tutor mailing list