我正在尝试使用python发送电子邮件。我在防火墙后面工作。我不太确定我错过了什么,但似乎我必须用服务器来表明自己。我不太清楚如何做到这一点。我玩过SMTP.helo()和SMTP.ehlo(),但无法进行身份验证。任何建议将不胜感激。我的代码如下。
谢谢!
from smtplib import SMTP
import socks
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, 'xxx.xx.xxx.xxx', xxxx)
socks.wrapmodule(smtplib)
msg = "Testing"
SMTP.send_message(msg, 'john@email.com', 'jane@email.com')
# Returns the error 'str' object has no attribute ehlo_or_helo_if_needed'
SMTP.sendmail('john@email.com', 'jane@email.com', msg)
#Returns the error sendmail() missing 1 required positional argument: 'msg'
答案 0 :(得分:0)
我认为它应该更容易使用email.mime
https://docs.python.org/2/library/email.mime.html
下面是一个从Gmail电子邮件地址发送电子邮件的示例
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_email():
msg = MIMEMultipart('alternative')
recipients = '{},{}'.format(
'hello1@mail.co.uk',
'hello2@mail.co.uk'
)
msg['Subject'] = 'Subject'
msg['From'] = 'Me'
msg['To'] = recipients
msg_html = """
<body>
Your html code
</body>
"""
body = MIMEText(msg_html, 'html')
msg.attach(body)
smtp = smtplib.SMTP("smtp.gmail.com", 587, timeout=120)
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login('<email>', '<password>')
smtp.sendmail(
'<email>',
recipients.split(','),
msg.as_string()
)