2015-06-20 34 views
1

我在Windows 8.1上使用yagmail -0.3.78和Python 3.3。我可以讓yagmail從我的控制檯發送電子郵件,但附件不起作用。Python yagmail附件不起作用

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

這將在控制檯中返回一個沒有錯誤的空白字典。電子郵件顯示正確的主題和正文,但沒有附件。起初我以爲我的防病毒軟件可能會刪除附件,但防病毒日誌中沒有任何內容可以表明它的存在。

p.s.顯然沒有標籤爲yagmail,我沒有代表創建它。

編輯-1:(?)一些進展

contents = ['This is a test', 'c:\\users\\lenovo\\appdata\\local\\temp\\mydoc.docx'] 
yagmail.Connect('[email protected]','password').send('[email protected]', '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) 

編輯-2:感謝PascalvKooten,見下面他的回答。只是注意語法...

yagmail.SMTP('[email protected]','password').send('[email protected]','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('[email protected]','password').send('[email protected]','Test1.2',contents) 
+0

什麼字符在位置713到715?嘗試更改mydoc.docx的內容。 – gary

+2

我可能會誤解代碼,但是在我看來,[yagmail不支持附件](https://github.com/kootenpv/yagmail/blob/master/yagmail/yagmail.py#L216)。 –

+0

@SamuelTaylor我同意它看起來像附件部分沒有做任何事情和/或被註釋掉。這有點令人慚愧,並且還與附有mp3和png的示例的自述文件相反。 –

回答

1

答案爲yagmail包的維護者:看到這個問題https://github.com/kootenpv/yagmail/issues/5

應解決爲0.3.81版本。

請更新與pip3 install -U yagmail

我從來沒有測試這個在Windows上工作,所以這是我唯一的不確定性。

+0

我已經打了它,現在在0.4.84,現在附件正在工作,謝謝。這有點奇怪。語法'yagmail.SMTP('[email protected]','password')。send('[email protected]','Test1.2','This is a test','C:\\ Temp \ \ mydoc.docx')'不起作用,即使你是明確的並且使用'attachment = blah..'。您必須先將主體和附件放入列表中,然後將該列表放入send方法中,例如'cnts = ['This is a test','C:\\ Temp \\ mydoc.docx']'並且然後'yagmail.SMTP('[email protected]','password')。send('[email protected]','Test1.2',cnts)'。 –

+0

@I_do_python事實上,附件不會做任何事情。這完全是關於「內容」(使用混合實際文本和附件的列表)。順便說一句,作爲獎金....嘗試省略密碼,並使用鑰匙圈,而不是:)(我想它也適用於Windows)。 – PascalVKooten

+0

現在在我的Windows機器上工作得很好。我從一個安全的數據庫中提取密碼,所以不需要恐慌:-p。感謝您的應用程序! –