473,804 Members | 3,469 Online
Bytes | Software Development & Data Engineering Community
+ 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....essent ially, 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.c om>'
m['To'] = 'm******@google groups.com'
m['Subject'] = 'A Test Message'
m.set_payload(' This is a test email. Please ignore')
s = smtplib.SMTP('s mtp.myemail.com ')
failed =
s.sendmail('us* *@myemail.com', 'm******@google groups.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 1933
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.c om>'
m['To'] = 'm******@google groups.com'
m['Subject'] = 'A Test Message'
m.set_payload(' This is a test email. Please ignore')
s = smtplib.SMTP('s mtp.myemail.com ')
failed =
s.sendmail('us* *@myemail.com', 'm******@google groups.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...@uniqsy s.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.c om>'
m['To'] = 'm******@google groups.com'
m['Subject'] = 'A Test Message'
m.set_payload(' This is a test email. Please ignore')
s = smtplib.SMTP('s mtp.myemail.com ')
failed =
s.sendmail('u.. .@myemail.com', 'm******@google groups.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.sour ceforge.net

Aug 4 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2214
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. I did all I could just to get them to allow me to have it sent in both plaintext and HTML so that the e-mail clients have a choice. Anyway. I've looked around the RFC's and the source of similar e-mails, and I'm certain I figured out how to do...
2
2255
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. That seems to be because I am not successfully re-attaching the cookies to the header portion of the this request. I have tried 2 methods which should both work I think. The first was to use install_opener to attach the cookie handler back...
3
2340
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' /usr/src/httpd-2.0.49/srclib/apr/libtool --silent --mode=link gcc -pthread -I/ =500 -D_BSD_SOURCE -D_SVID_SOURCE -D_GNU_SOURCE -DAP_HAVE_DESIGNATED_INITIALIZER I. -I/usr/src/httpd-2.0.49/os/unix -I/usr/src/httpd-2.0.49/server/mpm/prefork -I .49/modules/proxy
1
6328
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 error:NMAKE: fatal error U1077: 'cl': return code '0x2'stop. I can build STLport without any error but a warning, after buiding I test it, a error is appear.
71
19145
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 problem? I cannot see why using malloc instead of new does not give the same result.
8
1595
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"; $headers .= "From: \"".$from_name."\" <".$from_email.">\n";
5
3950
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'. It seems that when I populate this 4th argument of mail(), any emails sent to an email forwarding service email address (ukreg/fasthosts actually) fail to reach their destination. Direct email addresses seem to work fine.
3
1616
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 wrong please help ....................i really need serious help the field picture2, picture4 and video are uploaders <?php $firstname = $_POST; $lastname = $_POST; $email = $_POST; $number = $_POST; $NameOfAdvertisedItem = $_POST;
3
1478
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 button is pressed, another window opens to say the mail has been sent - when it hasn't (Ihave changed this to read there was a problem). I can't see anything in the logfiles to say what hasn't worked - but it's driving me crazy. The file called upon is...
0
10589
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10327
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6857
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
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 we have to send another system
2
3828
muto222
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.