SMTP电子邮件脚本在Mac上不起作用,但适用于Windows

时间:2011-09-14 09:55:54

标签: python macos email smtplib

我有一些用于发送电子邮件的代码。它适用于Windows,但是当我尝试在Mac上运行它(与Windows机器上的用户登录相同)时,它不会发送电子邮件或返回任何错误。

任何人都有这方面的经验,或者示例,提示,解决方案?

import smtplib
import mimetypes
from email import encoders
from email.message import Message
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


import platform

SERVER = "mail.myCompany.co.za"
COMMASPACE = ', '


SUBJECT = "testing"
TEXT = "mac dev test\n\nSent from: " + platform.platform()
TO = [ 'jared.glass@myCompany.co.za' ]
FROM = "jared.glass@myCompany.co.za"


# connect to the server
smtp = smtplib.SMTP( SERVER )

smtp.ehlo()


# if attachments are too big, try gain and just add them as links
# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = SUBJECT
msg['From'] = FROM
msg['To'] = COMMASPACE.join(TO)      
msg.attach( MIMEText( TEXT , 'plain') )

smtp.sendmail( FROM , TO , msg.as_string() )


smtp.close()               
print(" ---- SUCCESS ---- mail sent ---- ")

1 个答案:

答案 0 :(得分:0)

sendmail调用什么都没有返回 - 只是{}。最后我不知道为什么,但出于纯粹的烦恼我试过了

smtp.starttls()
smtp.login('username', 'password')

似乎解决了这个问题。

对我来说,你需要这样做才能让脚本在Mac上工作,因为它在Windows机器上运行得很好而没有登录... Grrrrr mac。

感谢您的建议:)