0

There are several examples of similar functionality, and I have been following those guidelines, but this is still not working. I am trying to get this test script to run:

# -*- coding: utf-8 -*-

import smtplib
from   email.header         import Header
from   email.mime.multipart import MIMEMultipart
from   email.mime.text      import MIMEText

USERNAME   = 'you@gmail.com'
PASSWORD   = 'yourpass'
COMMASPACE = ', '
SERVER     = 'smtp.gmail.com:587'

def isallascii(str):
    '''Checks if all characters in string are ascii (useful for unicode related processing)'''
    return all(ord(c) < 128 for c in str)

def correct_headervalue(headervalue):
    return headervalue if isallascii(headervalue) else Header(headervalue, 'utf-8')

def addheader(msg, headername, headervalue):
    msg[headername] = correct_headervalue(headervalue)

def buildmsg(sender, receivers, subject, plainbody):
    receivers_str = COMMASPACE.join(receivers) if receivers else None
    msg = MIMEMultipart('alternative')
    addheader(msg, 'Subject', subject)
    addheader(msg, 'From',    sender)
    addheader(msg, 'To',      receivers_str)
    plaintext = MIMEText(plainbody,'plain') if isallascii(plainbody) else MIMEText(plainbody.encode('utf-8'), 'plain', 'utf-8')
    msg.attach(plaintext)
    return msg

smtpObj = smtplib.SMTP(SERVER)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.ehlo()
smtpObj.login(USERNAME, PASSWORD)
sender = 'Juan Pérez Castaña <juan.pérez.castaña@example.com>'
receivers = ['test1@example.com']
subject = 'A nice subject'
body = 'who cares'
message = buildmsg(sender, receivers, subject, body)
smtpObj.sendmail(sender, receivers, message.as_string())

But it is throwing:

Traceback (most recent call last):
  File "sendit.py", line 50, in <module>
    smtpObj.sendmail(sender, receivers, message)
  File "/usr/lib/python2.7/smtplib.py", line 722, in sendmail
    raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (555, '5.5.2 Syntax error. q17sm7358699wiv.10 - gsmtp', 'Juan P\xc3\xa9rez Casta\xc3\xb1a <juan.p\xc3\xa9rez.casta\xc3\xb1a@example.com>')

What I do not get is that I have properly encoded the headers, but it still refuses them. I wonder if I also need to encode the parameters passed to smtpObj.sendmail (sender and receivers). If so, how can I do this?

Community
  • 1
  • 1
blueFast
  • 41,341
  • 63
  • 198
  • 344
  • 1
    You can RFC2047-encode the real name Juan Pérez Castaña, but `` is simply not a valid address. – tripleee Oct 16 '13 at 16:54
  • It seems you are right. Removing those invalid characters from the email address itself (`Juan Pérez Castaña `) allows me to send the email, but the from field is garbled in my email client. – blueFast Oct 17 '13 at 07:53
  • So what do you see? In "View Source", you should see something like `From: =?utf-8?q?Juan_P=C3=A9rez_Casta=C3=B1a?= ` or perhaps `From: =?utf-8?b?SnVhbiBQw6lyZXogQ2FzdGHDsWE=?= ` – tripleee Oct 17 '13 at 08:30
  • Oh, the envelope sender should basically be only the actual email terminus. You should have `juan.perez.castana@example.com` in a separate string, and then building the RFC5322 `From:` header with `'%s <%s>' % (Header(sendername), senderaddress)` should be smooth sailing. – tripleee Oct 17 '13 at 08:34
  • What I see is ("show original" in gmail) `From: "=?iso-8859-1?q?Juan_P=E9rez_Casta=F1a_=3Cjuan=2Eperez=2Ecastana=40example?= =?iso-8859-1?q?=2Ecom=3E?=" ` (I have encoded now with `iso-8859-1`; `utf-8` also produces a garbled `from`). I wonder where does my real address (`gonvaled@xxxxxx.com`) come from? I guess that somebody is adding that automatically because the sender is garbled? Who is adding this? – blueFast Oct 17 '13 at 08:40
  • And interesting that you are splitting sendername and senderaddress when building the from. Is this really needed? Can't I just pass the whole thing to `Header`? – blueFast Oct 17 '13 at 08:42
  • Ok, I verified and you are right. Splitting name and email solves the problem. Also, regarding my question related to the from, there is a reply about [that](http://stackoverflow.com/a/3872880/647991). Maybe you can add a reply so that I can upvote / accept? – blueFast Oct 17 '13 at 09:01
  • For the record, the MTA you are connecting to will probably add an address to the `From:` header if it doesn't contain one. Not all MTAs do this, but there is a strong precedent from Sendmail. – tripleee Oct 17 '13 at 11:19
  • That could be, but it is not the case in my test, since the MTA has no idea of what my email address is. It is indeed google overwriting the from address to the one of the SSL authorized user. – blueFast Oct 17 '13 at 13:37

0 Answers0