kootenpv / yagmail

Send email in Python conveniently for gmail using yagmail
MIT License
2.66k stars 265 forks source link

yagmail problem on python2.6 #75

Closed yvanz closed 7 years ago

yvanz commented 7 years ago

I want to send email with yagmail on python2.6,and this is my test demo code

user = "xxx@yyyy.com"
password = "xxxx.Wn"
smtp_port = 25
smtp_host = "xxxx"

yag = yagmail.SMTP(user=user, password=password, 
                                  host=smtp_host, port=smtp_port, smtp_starttls=False)
yag.send("yvan@yyyy.com", "an attachment", "this is the body")

But then I got a ValueError

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/site-packages/yagmail/sender.py", line 104, in send
    msg = self._prepare_message(addresses, subject, contents, attachments, headers)
  File "/usr/lib/python2.6/site-packages/yagmail/sender.py", line 248, in _prepare_message
    self._add_recipients_headers(msg, addresses)
  File "/usr/lib/python2.6/site-packages/yagmail/sender.py", line 314, in _add_recipients_headers
    '\\', '\\\\').replace('"', '\\"'), self.user)
ValueError: zero length field name in format

I have tested on Python2.7 with the same code ,and it works well. So,what's the problem I got?And how to fix it?

yvanz commented 7 years ago

I find the reason now. In python2(<2.7), I need indices in the format specs,so I change the code like this

msg['From'] = '"{0}" <{1}>'.format(self.useralias.replace(
            '\\', '\\\\').replace('"', '\\"'), self.user)
kootenpv commented 7 years ago

Afterwards it worked? Maybe you could make a pull request, I would gladly accept it!

yvanz commented 7 years ago

Yeah,it worked after I change the code.And I made a pull request just now