Cannot form correctly the FORM part of the header when sending mail

Piet van Oostrum piet at vanoostrum.org
Tue Sep 3 22:14:31 EDT 2013


Ferrous Cranus <nikos at superhost.gr> writes:

> Hello,
> i have written the following snipper of code to help me send mail:
>

[snip]

>             # prepare mail data
>             TO = "nikos at superhost.gr"
>
>             SUBJECT = u"Mail από τον επισκέπτη: ( %s )" % FROM
>
>             MESSAGE = "From: %s\r\n" + "To: %s\r\n" + "Subject: %s\r\n"
> + MESSAGE + "\r\n"
>             MESSAGE = MESSAGE % ( FROM, TO, SUBJECT )
>             MESSAGE = MESSAGE.encode('utf-8')

First a couple of remarks:
1. You should add an empty line between the headers and the message (I
suppose the message does not start with an empty line).
2. It is better to do the % substitution only on the headers, not
including the message, just in case the message contains a % sign.
That makes it:
             MESSAGE = "From: %s\r\n" + "To: %s\r\n" + "Subject: %s\r\n\r\n" % 
                              ( FROM, TO, SUBJECT ) + MESSAGE + "\r\n"
             MESSAGE = MESSAGE.encode('utf-8')
3. It is bad coding style in Python to use all-caps variables. Better
use message instead of MESSAGE etc.

[snip]

> It works as expected, but the the problem is that it display the FROM
> part as being send from ,my personal GMail account when it supposed to
> be shown the format variable field that was passed by index.html to the
> mail.py script.

Where does it display that?
Do you happen to read that mail in a Microsoft program?
If yes, then it is the fault of that program. Read the mail in some
other program and you will probably see that the proper From address is
there.

The problem is that Gmail inserts a "Sender" header with your account
(email address) and certain Microsoft programs use that to display as
the From address instead of the real From address. It's against the
rules, but then, Microsoft makes its own rules and who is going to stop
them?

And maybe there are other mail programs that do the same. AFAIK there is
no way to get rid of that Sender line.
-- 
Piet van Oostrum <piet at vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]



More information about the Python-list mailing list