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

Steven D'Aprano steve at pearwood.info
Thu Sep 5 04:58:24 EDT 2013


On Thu, 05 Sep 2013 09:31:41 +0300, Ferrous Cranus wrote:

[...]
> UBJECT = u"SuperHost Guest Mail από τον [ %s ]" % FROM
> 			
> MESSAGE = "From: %s\n" + "To: %s\n" + "Subject: %s\n\n%s\n" % (FROM, TO,
> SUBJECT, MESSAGE)
> MESSAGE = MESSAGE.encode('utf-8')
> 
> 
> but i still get the same error messgae

And? What is the error message telling you? Don't just ask for help every 
single time you get an exception. The error says:

TypeError: not all arguments converted during string formatting


What does that mean? The string formatting operator is % and you can, and 
should, experiment on it yourself:

py> "aaaa %s" % 'hello'
'aaaa hello'


Now try to get the error you see:

py> "aaaa%s" % ('hello', "world")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting

You have two strings on the right hand side of the % operator, but only 
one %s target on the left.

Now how about this?


py> "aa%s" + "bb%s" % ("hello", "world")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting


What's the precedence of + and % operators? Which one gets executed 
first? Hint:


py> 8 + 2 % 5
10
py> (8 + 2) % 5
0
py> 8 + (2 % 5)
10

Even though these examples are with ints, not strings, the precedence is 
the same.

Go back to your code. Read your code. Does it look closer to this:

8 + 2 % 5

or this?

(8 + 2) % 5


Can you solve this problem now?



-- 
Steven



More information about the Python-list mailing list