By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

smtp question

P: n/a
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
***************************

Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

Jul 18 '05 #2

P: n/a
"Philippe C. Martin" <ph*************@sbcglobal.net> writes:
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

--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #3

P: n/a
Philippe C. Martin wrote:
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 = 'm***@mxm.dk'
to = 'm***@mxm.dk'
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
Jul 18 '05 #4

P: n/a
"Philippe C. Martin" <ph*************@sbcglobal.net> wrote:
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)
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.