【问题标题】:Python yagmail attachment not workingPython yagmail附件不起作用
【发布时间】:2015-06-20 12:05:36
【问题描述】:

我在 Windows 8.1 上使用yagmail-0.3.78 和 Python 3.3。我可以让 yagmail 从我的控制台发送电子邮件,但附件不起作用。

yagmail.Connect('myemail@gmail.com','password').send('someguy@gmail.com', 'Test', 'This is a test', 'c:\\users\\lenovo\\appdata\\local\\temp\\mydoc.docx')

这会在控制台中返回一个没有错误的空字典。电子邮件显示正确的主题和正文,但没有附件。起初我以为我的防病毒软件可能正在删除附件,但防病毒日志中没有任何内容表明它是。

附言显然没有 yagmail 的标签,我也没有创建它的代表。

Edit-1:一些进展(?)

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)

...导致以下错误。

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:感谢 PascalvKooten,请参阅下面的答案。只是语法上的注释...

yagmail.SMTP('mymail@gmail.com','password').send('someguy@gmail.com','Test1.2','This is a test','C:\\Users\\Lenovo\\AppData\\Local\\Temp\\mydoc.docx')

对我不起作用。但是下面的结构确实有效。

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)

【问题讨论】:

  • 713 到 715 位置有哪些字符?尝试更改 mydoc.docx 的内容。
  • 我可能误读了代码,但它在我看来就像yagmail doesn't support attachments
  • @SamuelTaylor 我同意附件部分似乎没有做任何事情和/或被注释掉。这有点可惜,也与 ReadMe 文件背道而驰,其中有一个附加 mp3 和 png 的示例。
  • @PascalvKooten 你能确认一下吗?谢谢。
  • 啊,我想你有旧版本,用 pip install - U yagmail 升级。 (或 pip3)。请注意,您必须使用 yagmail.SMTP 而不是 yagmail.Connect。

标签: python python-3.x yagmail


【解决方案1】:

作为yagmail包的维护者回答:看这个问题https://github.com/kootenpv/yagmail/issues/5

应该在0.3.81版本解决。

请更新pip3 install -U yagmail

我从未在 Windows 上测试过它,所以这是我唯一的不确定性。

【讨论】:

  • 我已经 pip'd 了,现在在 0.4.84,附件现在可以工作了,谢谢。不过这有点奇怪。语法 yagmail.SMTP('mymail@gmail.com','password').send('someguy@gmail.com','Test1.2','This is a test','C:\\Temp\\mydoc.docx') 不起作用,即使您是明确的并使用 attachment = blah..。您必须先将正文和附件放在一个列表中,然后将该列表放入发送方法中,例如cnts = ['This is a test','C:\\Temp\\mydoc.docx'],然后是yagmail.SMTP('mymail@gmail.com','password').send('someguy@gmail.com','Test1.2',cnts)
  • @I_do_python 实际上,附件没有任何作用。这都是关于contents(使用那里的混合实际文本和附件的列表)。顺便说一句,作为奖励....尝试省略密码并改用密钥环:)(我想它也适用于 Windows)。
  • 现在在我的 Windows 机器上工作得非常好。我正在从安全数据库中提取密码,因此无需惊慌:-p。感谢您的应用!
  • @I_do_python 太棒了,感谢您成为快乐的客户:P
猜你喜欢
  • 1970-01-01
  • 2017-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多