[Tutor] How to send an email using Python

Stefan Lesicnik stefan at lsd.co.za
Wed Mar 11 07:42:45 CET 2009


Hi.

I use the following to send through gmail.


  message = headers + text
        mailserver = smtplib.SMTP('smtp.gmail.com')
        if debug == 1: mailserver.set_debuglevel(1)
        mailserver.ehlo()
        mailserver.starttls()
        mailserver.ehlo()

        #Define username / password if using SMTP Auth
        username = 'email at gmail.com'
        password = getpass.getpass("%s's password: " % username)

        mailserver.login(username,password)
        mailserver.sendmail(sender, to, message)
        mailserver.close()


I believe for gmail smtp you need the ehlo() and then the starttls()
and then another ehlo().


My first post, been lurking a while.  :)




On Wed, Mar 11, 2009 at 6:28 AM, Senthil Kumaran <orsenthil at gmail.com> wrote:
> 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
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list