I'm having a strange problem where the last 10-20 characters of my email are being truncated.
The code that sends the email is as follows:
#Get the runtime arguments.
subject = args[0]
content = str(args[1]).replace("\\n","<br/>") #Python automatically escapes the backslash in runtime arguments, so "\n" turns into "\\n".
#Create the email message.
msg = MIMEText(content, 'html')
msg['From']=sender
msg['To']=",".join(recipients)
msg['Subject']=subject
print "Sending email with subject \""+subject+"\" to: " + ",".join(recipients)
print "Content: \n" + content;
print "\n\n"
print "msg.as_string(): \n" + msg.as_string()
#Set the SMPTP server, and send the email.
s = smtplib.SMTP(server)
s.sendmail(sender,recipients,msg.as_string())
s.quit()
As you can see in the code, I print both the content and the final message to the screen, both of which print correctly. But when the email is received by the recipient, it is truncated. I'm not 100% sure if it's being truncated by a certain amount of characters, or after a certain amount characters, but I suspect it's the later.
Oddly, the emails are sent just fine if they are sent in plain-text rather than HTML format. But unfortunately, most of the recipients use Outlook, which thinks it knows better than me where to put new lines in plain-text emails...
Any insight will be appreciated.
Edit: I should also mention that this is not well-formed HTML. Basically, I'm just replacing new lines with
<br/>
I'm not sure if that will make a difference. Aside from the brake tags, there is nothing that even remotely resembles an HTML tag, so the problem is not with an unexpected tag messing up the formatting.
If you are removing all newlines from the email message, you are in for trouble. SMTP servers typically do not accept lines longer than some 1,000 characters. If you want to send free-form data, encapsulate it in something like Quoted-Printable (where you can put in "invisible" line breaks which will be removed by the client -- take care to correctly QP-encode the message itself, though).
In quoted printable (RFC 2045), you can hex-encode any =22special=22 chara=
cter, like this (or=20in=20fact,=20any=20character=20at=all), and add line=
breaks where you see fit by prefixing them with an equals sign. Of cours=
e, you also have to encode any literal equals sign, like this: =3D. Bette=
r use a library which understands the details of this format than write yo=
ur own encoder, though.
If you specify Content-Transfer-Encoding: binary
you can in theory pass in lines of arbitrary length, but it's better and safer to stick to what 7bit
allows, and use a suitable Content-Transfer-Encoding
like quoted-printable
or (if you want to go really wild) base64
.