472,119 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Problems with headers in email.message

Hi, python newbie, struggling to learn here. I'm trying to write a
simple program which posts messages to my google group (via email).
I'm using smtplib, email and email.message to build and send a
message, but all the header attributes are appearing in the message
body, so my messages are arriving with loads of junk in the body, no
subject line and only the skinniest of headers. I know I'm doing
something wrong, but I don't know what...

Here's a simplification of what I'm doing....essentially, I've taken
from what I've learned at
http://codeidol.com/python/python3/C...-Email-Client/

-------------Snip Here-------------Snip Here-------------Snip
Here-------------
import smtplib, email
from email.message import Message
m = Message( )
m['From'] = 'Slippy <us**@myemail.com>'
m['To'] = 'm******@googlegroups.com'
m['Subject'] = 'A Test Message'
m.set_payload('This is a test email. Please ignore')
s = smtplib.SMTP('smtp.myemail.com')
failed =
s.sendmail('us**@myemail.com','m******@googlegroup s.com',str(m))
if failed:
print 'Message sending failed.'
else:
print 'Message sent.'
print 'Bye.'
-------------Snip Here-------------Snip Here-------------Snip
Here-------------

Now, I get all the right responses, and the message sends ok. I go and
check my inbox, and the message is there, but the From, To and Subject
lines I created (as well as a preceding blank line and a "From nobody"
line) are in the message body, followed by the body text.

How do I assign values to the header?

I'd appreciate any help anyone can give me with this.

Aug 4 '07 #1
2 1842
On Sat, 2007-08-04 at 15:38 +0000, Slippy wrote:
[...]
import smtplib, email
from email.message import Message
m = Message( )
m['From'] = 'Slippy <us**@myemail.com>'
m['To'] = 'm******@googlegroups.com'
m['Subject'] = 'A Test Message'
m.set_payload('This is a test email. Please ignore')
s = smtplib.SMTP('smtp.myemail.com')
failed =
s.sendmail('us**@myemail.com','m******@googlegroup s.com',str(m))
if failed:
print 'Message sending failed.'
else:
print 'Message sent.'
print 'Bye.'
-------------Snip Here-------------Snip Here-------------Snip
Here-------------

Now, I get all the right responses, and the message sends ok. I go and
check my inbox, and the message is there, but the From, To and Subject
lines I created (as well as a preceding blank line and a "From nobody"
line) are in the message body, followed by the body text.

How do I assign values to the header?
The way you're doing it is fine as far as assigning headers goes. My
semi-educated guess is that either your mail-transfer agent or Google
Groups' MTA is being confused by the "Unix From" envelope (that's the
"From nobody..." you're seeing) that is included by str(m).

Try m.as_string() instead, which doesn't include the Unix From envelope.

I'd also like to point out that email.message is overkill for your
simple use case. An email message is simply a series of headers followed
by a blank line followed by the message body, which you can easily build
manually if the headers aren't too long and the body is plain text:

email_message = """\
From: %s
To: %s
Subject: %s

%s""" % (email_from, email_to, email_subject, email_body)

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net
Aug 4 '07 #2
Carsten, changing to m.as_string() worked perfectly - Thanks for the
help.

The actual project I'm working on is a lot more complex than the
simple case I've shown you, and does warrant the use of the message
parser, honest! :-)

On Aug 4, 5:14 pm, Carsten Haese <cars...@uniqsys.comwrote:
On Sat, 2007-08-04 at 15:38 +0000, Slippy wrote:
[...]
import smtplib, email
from email.message import Message
m = Message( )
m['From'] = 'Slippy <u...@myemail.com>'
m['To'] = 'm******@googlegroups.com'
m['Subject'] = 'A Test Message'
m.set_payload('This is a test email. Please ignore')
s = smtplib.SMTP('smtp.myemail.com')
failed =
s.sendmail('u...@myemail.com','m******@googlegroup s.com',str(m))
if failed:
print 'Message sending failed.'
else:
print 'Message sent.'
print 'Bye.'
-------------Snip Here-------------Snip Here-------------Snip
Here-------------
Now, I get all the right responses, and the message sends ok. I go and
check my inbox, and the message is there, but the From, To and Subject
lines I created (as well as a preceding blank line and a "From nobody"
line) are in the message body, followed by the body text.
How do I assign values to the header?

The way you're doing it is fine as far as assigning headers goes. My
semi-educated guess is that either your mail-transfer agent or Google
Groups' MTA is being confused by the "Unix From" envelope (that's the
"From nobody..." you're seeing) that is included by str(m).

Try m.as_string() instead, which doesn't include the Unix From envelope.

I'd also like to point out that email.message is overkill for your
simple use case. An email message is simply a series of headers followed
by a blank line followed by the message body, which you can easily build
manually if the headers aren't too long and the body is plain text:

email_message = """\
From: %s
To: %s
Subject: %s

%s""" % (email_from, email_to, email_subject, email_body)

HTH,

--
Carsten Haesehttp://informixdb.sourceforge.net

Aug 4 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by john.lehmann | last post: by
1 post views Thread by PangFromChina | last post: by
71 posts views Thread by desktop | last post: by
8 posts views Thread by Spartacus | last post: by
5 posts views Thread by Dave | last post: by
3 posts views Thread by Veli | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.