help with sending mail in Program

Kent Johnson kent37 at tds.net
Thu Jun 9 06:07:54 EDT 2005


Tim Roberts wrote:
> Kent Johnson <kent37 at tds.net> wrote:
> 
>>Ivan Shevanski wrote:
>>
>>>Could someone send me a good tutorial for sending mail then? The one I 
>>>found is not what I'm looking for.  Also please dont send me the stmp 
>>>definition cause i've looked at that enough.
>>
>>Here is a module I use to send mail to myself using smtplib:
> 
> 
> Not exactly like this, you didn't.  The list of destination addresses in
> SMTP.sendmail must be a sequence, not a string:

Yes, exactly like that. This is a working module, the only thing I changed to post it was the email address and the name of the SMTP server. SMTP.sendmail() allows a single string as a to address.

>From the comment in smtplib.py:
            - to_addrs     : A list of addresses to send this mail to.  A bare
                             string will be treated as a list with 1 address.

I admit, I may have figured this out by accident, but it *does* work! 

Kent

> 
> 
>>#!/usr/local/bin/python
>>
>>''' Send mail to me '''
>>
> 
>>from smtplib import SMTP
> 
>>def sendToMe(subject, body):
>>   me = '"Kent Johnson" <me at mycompany.com>'
>>   send(me, me, subject, body)
>>   
>>   
>>def send(frm, to, subject, body):
>>   s = SMTP()
>>#    s.set_debuglevel(1)
>>   s.connect('mail.mycompany.com')
>>   s.ehlo('10.0.3.160') # IP address of my computer, I don't remember why I needed this
>>   
>>   msg = '''From: %s
>>Subject: %s
>>To: %s
>>
>>%s
>>''' % (frm, subject, to, body)
>>
>>   s.sendmail(frm, to, msg)
> 
> 
>     s.sendmail(frm, [to], msg)
> 
> 
>>   s.quit()
>>   
>>
>>if __name__ == '__main__':
>>   sendToMe('Testing', 'This is a test')



More information about the Python-list mailing list