1

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('myemail@gmail.com','password').send('someguy@gmail.com', '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('myemail@gmail.com','password').send('someguy@gmail.com', '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('mymail@gmail.com','password').send('someguy@gmail.com','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('mymail@gmail.com','password').send('someguy@gmail.com','Test1.2',contents)
I_do_python
  • 1,366
  • 4
  • 16
  • 31
  • What characters are at positions 713 to 715? Try changing the contents of mydoc.docx. – gary Jun 20 '15 at 12:17
  • 2
    I may be misreading the code, but it looks to me like [yagmail doesn't support attachments](https://github.com/kootenpv/yagmail/blob/master/yagmail/yagmail.py#L216). – Samuel Taylor Jun 20 '15 at 12:24
  • @SamuelTaylor I agree it looks like the attachment section doesn't do anything and/or is commented out. That's a bit of a shame, and also counter to the ReadMe file that has an example attaching an mp3 and png. – I_do_python Jun 20 '15 at 12:52
  • @PascalvKooten Are you able to confirm? Thanks. – I_do_python Jun 20 '15 at 12:52
  • Sorry I had not noticed. The argument attachments is unused, however: you can send the file path in `contents` and it will work! Only limitation is that images will be displayed inline and cannot be attached. The rest will be attached. I'll have a look in a couple of hours, to see what's wrong if anything. – PascalVKooten Jun 20 '15 at 13:52
  • 1
    Ah, I think you have the old version, upgrade with pip install - U yagmail. (or pip3). Note that you'll have to use yagmail.SMTP instead of yagmail.Connect. – PascalVKooten Jun 20 '15 at 14:05
  • @SamuelTaylor My apologies. It is there when I thought I needed to do explicit attachments. At this point, only images would need to be explicitly attached (didn't implement it yet): at this point images will be embedded. Any other file paths will be attached. – PascalVKooten Jun 20 '15 at 16:54
  • @PascalvKooten no worries! I just gave the library a cursory glance; it's my bad. I should have looked more thoroughly. Thanks for making it! – Samuel Taylor Jun 21 '15 at 01:50

1 Answers1

1

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.

PascalVKooten
  • 20,643
  • 17
  • 103
  • 160
  • I've pip'd it, now on 0.4.84, and the attachment is working now, thank you. It is a bit strange though. The syntax `yagmail.SMTP('mymail@gmail.com','password').send('someguy@gmail.com','Test1.2','This is a test','C:\\Temp\\mydoc.docx')` does not work, even if you're explicit and use `attachment = blah..`. You have to put the body and attachment in a list first, and then put that list in the send method, like `cnts = ['This is a test','C:\\Temp\\mydoc.docx']` and then `yagmail.SMTP('mymail@gmail.com','password').send('someguy@gmail.com','Test1.2',cnts)`. – I_do_python Jun 20 '15 at 17:18
  • @I_do_python Indeed, attachments does not do anything. It's all about `contents` (using a list there of mixing actual text and attachments). Btw, as a bonus.... try omitting the password and use the keyring instead :) (I suppose it also works on windows). – PascalVKooten Jun 20 '15 at 18:12
  • Working perfectly fine on my windows machine now. I'm pulling the password from a secure database so no need to panic :-p. Thanks for the App! – I_do_python Jun 21 '15 at 03:51
  • @I_do_python Awesome, thanks for being a happy customer :P – PascalVKooten Jun 21 '15 at 07:06