help with sending mail in Program

Tim Williams listserver at tdw.net
Sat Jun 11 07:46:30 EDT 2005


----- Original Message ----- 
From: "Ivan Shevanski" <darkpaladin79 at hotmail.com>
To: <listserver at tdw.net>
Sent: Saturday, June 11, 2005 3:32 AM
Subject: Re: help with sending mail in Program
>
> >>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')

> >From: "Tim Williams" <listserver at tdw.net>
> >To: "Ivan Shevanski" <darkpaladin79 at hotmail.com>
> >Subject: Re: help with sending mail in Program
> >Date: Thu, 9 Jun 2005 22:48:38 +0100
> >
> >Hi Ivan,   did you have any luck with the email source I sent you ?
> >
> >The hotmail server would have been overly sensitive to "fake" emails,  if
> >you were using a different server your original code would probably work.
> >
> >HTH
> >
> >Tim
> >-----

> Nope no luck. . .What server do you suggest I use? Yahoo doesnt have one,
> and I don't feel like downloading gmail. . .What do you think?
>
> -Ivan

Ivan,  the MIMEText module should create a properly constructed email from
your own text,    try something like this (untested)

----------------------------
from email.MIMEText import MIMEText
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')

    msg = MIMEText(body)
    msg['To'] = to #  '"Text Name" <address>'
    msg['Subject'] = subject
    msg['From'] = frm # '"Kent Johnson" <me at mycompany.com>'

    to2 = to.split()[-1]  # = <address>
    frm2 = frm.split()[-1]  # can use rsplit() in 2.4

    s.sendmail(frm2, [to2], msg.as_string())
    s.quit()

if __name__ == '__main__':
   sendToMe('Testing', 'This is a test')






More information about the Python-list mailing list