Python yagmail attachment not working

3.4k views Asked by At

I'm using yagmail-0.3.78 with Python 3.3 on Windows 8.1. I can get yagmail to send emails from my console but attachments just don't work.

yagmail.Connect('[email protected]','password').send('[email protected]', 'Test', 'This is a test', 'c:\\users\\lenovo\\appdata\\local\\temp\\mydoc.docx')

This returns an empty dict with no errors in the console. The email shows up with the right subject and body but no attachment. At first I thought my antivirus might be removing the attachment but there's nothing in the antivirus logs to suggest that it is.

p.s. Apparently there is no tag for yagmail, and I don't have the rep to create it.

Edit-1: Some progress(?)

contents = ['This is a test', 'c:\\users\\lenovo\\appdata\\local\\temp\\mydoc.docx']
yagmail.Connect('[email protected]','password').send('[email protected]', 'Test',contents)

...results in the below error.

Traceback (most recent call last):
  File "<string>", line 301, in runcode
  File "<interactive input>", line 1, in <module>
  File "C:\Python33\lib\site-packages\yagmail-0.3.78-py3.3.egg\yagmail\yagmail.py", line 73, in send
    return self._attempt_send(addresses['recipients'], msg.as_string())
  File "C:\Python33\lib\site-packages\yagmail-0.3.78-py3.3.egg\yagmail\yagmail.py", line 79, in _attempt_send
    result = self.smtp.sendmail(self.user, recipients, msg_string)
  File "C:\Python33\lib\smtplib.py", line 749, in sendmail
    msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 713-715: ordinal not in range(128)

Edit-2: Thanks to PascalvKooten, see his answer below. Just a note on syntax...

yagmail.SMTP('[email protected]','password').send('[email protected]','Test1.2','This is a test','C:\\Users\\Lenovo\\AppData\\Local\\Temp\\mydoc.docx')

Does not work for me. However the below structure does work.

contents = ['This is a test', 'C:\\Users\\Lenovo\\AppData\\Local\\Temp\\mydoc.docx']
yagmail.SMTP('[email protected]','password').send('[email protected]','Test1.2',contents)
1

There are 1 answers

4
PascalVKooten On BEST ANSWER

Answer as the maintainer of the yagmail package: see this issue https://github.com/kootenpv/yagmail/issues/5

It should be solved as of version 0.3.81.

Please update with pip3 install -U yagmail

I never tested this to work on windows, so that's my only uncertainty.