smtp question

  • Thread starter Philippe C. Martin
  • Start date
P

Philippe C. Martin

Hi,

I am testing the smtp module and have the following question:

in the code below (taken from net sample) prior to adding the "Subject:"
field, the email client found the "From" and the "To". Without the
"Subject:" field on I get this:

Email client = Evolution: the "From" field is blank
Email client = KMail: the "To" field is blank

Any clue ?

Thanks and regards,

Philippe
**********************

import smtplib

def prompt(prompt):
return raw_input(prompt).strip()


fromaddr = prompt("From: ")
toaddrs = prompt("To: ").split()
print "Enter message, end with ^D (Unix) or ^Z (Windows):"

# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\n\r\n" % (fromaddr, ", ".join(toaddrs)))
while 1:
try:
line = raw_input()
except EOFError:
break
if not line:
break
msg = msg + line

print "Message length is " + repr(len(msg))

server = smtplib.SMTP('smtp.sbcglobal.yahoo.com')
server.set_debuglevel(1)
server.login ('xxxxx','yyyyyyy')
server.sendmail(fromaddr, toaddrs, 'Subject:from python\n\n'+msg)
server.quit()









--
***************************
Philippe C. Martin
SnakeCard LLC
www.snakecard.com
***************************
 
K

Kartic

Philippe,

Looks like the problem lies where you have added the Subject header.
You have terminated it with a \n\n and then your From and To headers.
You may want to rewrite it as:

server.sendmail(fromaddr, toaddrs, 'Subject:from python\r\n'+msg)

Why dont you consider using the email module -
http://python.org/doc/2.4/lib/module-email.html ? It is elegant and
easy to use.

Thank you,
--Kartic
 
M

Mike Meyer

Philippe C. Martin said:
Hi,

I am testing the smtp module and have the following question:

in the code below (taken from net sample) prior to adding the "Subject:"
field, the email client found the "From" and the "To". Without the
"Subject:" field on I get this:

Email client = Evolution: the "From" field is blank
Email client = KMail: the "To" field is blank

Any clue ?

Thanks and regards,

Philippe
**********************

import smtplib

def prompt(prompt):
return raw_input(prompt).strip()


fromaddr = prompt("From: ")
toaddrs = prompt("To: ").split()
print "Enter message, end with ^D (Unix) or ^Z (Windows):"

# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\n\r\n" % (fromaddr, ", ".join(toaddrs)))
while 1:
try:
line = raw_input()
except EOFError:
break
if not line:
break
msg = msg + line

print "Message length is " + repr(len(msg))

server = smtplib.SMTP('smtp.sbcglobal.yahoo.com')
server.set_debuglevel(1)
server.login ('xxxxx','yyyyyyy')
server.sendmail(fromaddr, toaddrs, 'Subject:from python\n\n'+msg)

You've got \n\n after the Subject line, not \r\n. That is being treated
as two newlines, thus ending the headers.

<mike
 
M

Max M

Philippe said:
Hi,

I am testing the smtp module and have the following question:

in the code below (taken from net sample) prior to adding the "Subject:"
field, the email client found the "From" and the "To". Without the
"Subject:" field on I get this:

Email client = Evolution: the "From" field is blank
Email client = KMail: the "To" field is blank

Any clue ?

It' very easy to create email messages with the email module.
>>> from email.Message import Message
>>> fromaddr = '(e-mail address removed)'
>>> to = '(e-mail address removed)'
>>> msg = Message()
>>> msg['Subject'] = 'From Python'
>>> msg['From'] = fromaddr
>>> msg['To'] = to
>>> body = "This is the message content"
>>> msg.set_payload(body)

Thats all. Though some smtp servers needs a Date header too, to work.
>>> from time import gmtime, strftime
>>> msg['Date'] = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())

Sending the message via the smtp module is even simpler.
>>> import smtplib
>>> server = smtplib.SMTP('localhost')
>>> server.sendmail(fromaddr, [to], msg.as_string())
>>> server.quit()


--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
 
T

Tim Roberts

Philippe C. Martin said:
Hi,

I am testing the smtp module and have the following question:

in the code below (taken from net sample) prior to adding the "Subject:"
field, the email client found the "From" and the "To". Without the
"Subject:" field on I get this:

Email client = Evolution: the "From" field is blank
Email client = KMail: the "To" field is blank

Any clue ?
Absolutely.

server = smtplib.SMTP('smtp.sbcglobal.yahoo.com')
server.set_debuglevel(1)
server.login ('xxxxx','yyyyyyy')
server.sendmail(fromaddr, toaddrs, 'Subject:from python\n\n'+msg)
server.quit()

You have two newlines following the Subject: line. That inserts a blank
line, which terminates the headers. Everything else, including the From:
and To: lines, will be taken as part of the message body. I assume you
meant to use \r\n, but \n will work just as well and is less error prone.
print "Message length is " + repr(len(msg))

Easier is:
print "Message length is", len(msg)
More efficient is:
print "Message length is %d" % len(msg)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top