[英]Sending an alias(spoof) for the 'from' e-mail using python smtplib
我正在使用 python 3.4.3 发送电子邮件,此时我需要使用别名发送电子邮件。 该帐户是 gmail 帐户,但我需要能够将我想要的任何内容作为恶搞(别名)“发件人”电子邮件。 我已经非常努力地研究如何做到这一点并且运气很少。 鉴于我查看的线程数量以及我没有得到可行答案的事实表明缺乏对这个特定主题的讨论。 我希望这不仅仅是因为这很容易,以至于除了我之外的每个人都知道如何去做。
我应该提到我在 windows 10 机器上,但也可以使用 Ubuntu 和 Windows 7 机器。
import smtplib
fromreal = 'realmail@gmail.com'
fromshow = 'fakemail@gmail.com'
toaddy = ['rec01@gmail.com', 'rec02@gmail.com']
subject = ' test'
body = 'This is the body test'
content = '''\
From: %s
To: %s
Subject: %s
%s
''' % (fromshow, ', '.join(toaddy), subject, body)
server = 'smtp.gmail.com'
port = 587
mail = smtplib.SMTP(server, port)
mail.ehlo()
mail.starttls()
mail.login(fromreal, 'password')
try:
mail.sendmail(fromshow, toaddy, content)
print('E-mail sent.')
except:
print('E-mail not sent.')
mail.close()
您可以使用yagmail发送别名(不会更改为假电子邮件,但至少是别名):
import yagmail
# first is "from" arg; using a dictionary you can give an alias as value
yag=yagmail.SMTP({fromreal:'fakealias'}, 'password')
yag.send(toaddy, subject, body)
有 3 行而不是 30 行有多好;)
使用pip install yagmail
。
在github页面上阅读有关许多其他功能的更多信息。
除此之外,您可以使用“无密码”脚本(脚本中不需要密码),非常容易发送 HTML、内嵌图像和附件!
完全披露:我是yagmail
的开发者/维护者。
你的代码很好,
Google 会阻止您设置不属于您的别名 email。 这就是为什么您需要在您的 gmail 帐户中设置别名的原因。 为此,go 到https://mail.google.com/ -> 设置 ->(查看所有设置)-> 帐户 -> 将邮件发送为: -> 添加另一个 Z0C83F57C786A0B4A39EFAB237317C 地址。
验证 email 地址,然后您可以设置代码中使用的别名。
如果您收到 SMTPAuthenticationError (534, b'5.7.9 Application-specific password required. ...),您应该按照链接设置应用程序密码而不是您的真实密码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.