This mail never gets delivered. Any ideas why?

Thomas Rachel nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de
Mon Mar 11 06:25:56 CET 2013


Am 09.03.2013 22:20 schrieb Νίκος Γκρ33κ:
> 		SENDMAIL = '/usr/sbin/sendmail'
> 			
> 		FROM = mail
> 		TO = ['support at superhost.gr']
> 		SUBJECT = "Επικοινωνία πιθανού πελάτη!"
> 		TEXT = comment
>
> 		message = """\
> 				  From: %s
> 				  To: %s
> 				  Subject: %s
> 					
> 				  %s
> 				  """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
>
> 		p = os.popen("%s -t -i" % SENDMAIL, "w")
> 		p.write(message)
> 		status = p.close()
> 		if status != 256:
> 			print( "<h2><font color=lime>Ευχαριστώ πολύ για το ενδιαφέρον! Θα επικοινωνήσω μαζί σου άμεσα :-)</font></h2>" )
> 		else:
> 			print( "<h2><font color=red>Δυστυχώς δεν μπόρεσε να αποσταλεί το e-mail :-(" )
>
> ===============
>
> Do you see somehtign wrong in the above code?
>

I see some things here:

1. Your subject is not properly encoded.

All characters outside the ASCII area must be encoded in an appropriate 
way if you send an email. It MIGHT be the case that sendmail handles 
this for you, but probably not every version.

But that should not prevent sending the mail at all; probably some 
garbage would result.

2. You failed to tell us what "never gets delivered" means: do you get 
an error message?

3. You failed to give us a SSCCE <http://sscce.org/>: in order to test 
the code, I had to add several variable definitions and imports.

4., an I thik this is the point: you are indenting your message string.
If you put a print(message) somewhere into the code, you'll see that the 
email header lines don't start with the header names, but with spaces. 
That is something which sendmail cannot and won't handle. Likewise, the 
header-body-separator is probably broken as well.

Just write, even if your code is indented in some way:

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

%s""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

or maybe, just to be sure,

   		message = "From: %s\nTo: %s\nSubject: %s\n\n%s" % \
                     (FROM, ", ".join(TO), SUBJECT, TEXT)

in order to get a useful result.


And, as you are working on it, don't use os.popen - it is deprecated. 
Better use subprocess:

replace

p = os.popen("%s -t -i" % SENDMAIL, "w")
p.write(message)
status = p.close()

with

import subprocess
sp = subprocess.Popen([SENDMAIL, '-t', '-i'], stdin=subprocess.PIPE)
sp.communicate(message)
status = sp.wait()

giving more flexibility.


HTH,

Thomas



More information about the Python-list mailing list