473,472 Members | 1,728 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1919
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: news | last post by:
Before I begin, I realise there's a big war regarding HTML in e-mails. Personally, I don't like it. Unfortunately, I'm being paid by my company to create an automated newsletter for our customers....
2
by: john.lehmann | last post by:
Attacked is a piece of code which first hits the login page successfully and receives back login cookies. But then when I attempt to hit a page which is restricted to logged in users only, I fail....
3
by: Ryan Riehle | last post by:
Hi All! Trying to upgrade to Apache 2.0.49 and getting compile errors related to mod_auth_pgsql, any clue?: make: Entering directory `/usr/src/httpd-2.0.49'...
1
by: PangFromChina | last post by:
Platform:window 2000 professional, VC++6.0 +SP5 STLport: STLport 5.0.2 While test STLport following "STLport README for Microsoft Visual C++ compilers." of README.msvc(readme.txt), there is a...
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
8
by: Spartacus | last post by:
Oops, I meant to write: function send_mail($to_name, $to_email, $from_name, $from_email, $subject, $message) { $thedate = date("r", time()); $headers = "X-Mailer: PHPMailProgram/1.0\n";...
5
by: Dave | last post by:
Hi All, I'm experiencing problems with sending mail using mail() to forwarded email accounts. The problem seems to revolve around the optional 4th argument of mail(), namely 'additional headers'....
3
by: Veli | last post by:
Good Day I have published a website and everything works expect for my forms. I can't seem to send the attachments to the email address according to the uploader. I don't know what i am doing...
3
by: tonyb68 | last post by:
Hi - Not sure if this is exactly the right forum to enter this is - but need to start somewhere as I don't know where it's not working. I have a contact form on a flash website, when the submit...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.