[Tutor] sender name in smtplib

Kent Johnson kent37 at tds.net
Tue Apr 14 02:28:36 CEST 2009


On Mon, Apr 13, 2009 at 6:55 PM, George Wahid <geoterium at gmail.com> wrote:
> I want to send an email from a google mail account so that the sender
> name will be 'User Name' instead of 'username at gmail.com' in the inbox.
> this is the code I use:         (I'm using python 2.5)
>>>>import smtplib
>>>>g=smtplib.SMTP('smtp.googlemail.com')
>>>>g.ehlo();g.starttls();g.ehlo()
>>>>g.login('username at gmail.com','somepassword')
>>>>g.sendmail('User Name','username at gmail.com','From: User Name\r\nTo: username at gmail.com\r\nSubject: how r u ?\r\nfoobar')
> {}
>>>>g.quit()
>
> but this doesn't work, what should I do ?

The first two arguments to sendmail() are the from address and the to
address. Also the From: part of the body should be a valid address. I
think you want something like

g.sendmail('George Wahid <geoterium at gmail.com>',
'User Name <username at gmail.com>',
'From: George Wahid <geoterium at gmail.com>\r\nTo: User Name
<username at gmail.com>\r\nSubject: how r u ?\r\nfoobar')

Kent


More information about the Tutor mailing list