我正在使用 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()
Run Code Online (Sandbox Code Playgroud) 我对此进行了相当多的搜索,但找不到任何令人满意的结果。
我一直在尝试编写一个 python 程序来监听电子邮件退回报告,并根据退回的原因以不同的时间间隔重新发送它们。
import smtplib
from smtplib import *
sender = 'foo@bar.com'
receivers = ['42@life.com']
message = """From: From Arthur <foo@bar.com>
To: To Deep Thought <42@life.com>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
try:
smtpObj = smtplib.SMTP('smtp.gmail.com',587)
smtpObj.starttls()
smtpObj.login(sender,'foo@bar.com')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPResponseException:
error_code = SMTPResponseException.smtp_code
error_message = SMTPResponseException.smtp_error
print "Error code:"+error_code
print "Message:"+error_message
if (error_code==422):
print "Recipient Mailbox Full"
elif(error_code==431):
print "Server out of space"
elif(error_code==447):
print "Timeout. Try …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下代码从 iCloud 电子邮件地址发送一些电子邮件:
import smtplib
def main():
smtp_obj = smtplib.SMTP('smtp.mail.me.com', 587)
smtp_obj.starttls()
smtp_obj.login('user@icloud.com', 'password')
pairs = {'name_1': 'email_1@gmail.com', 'name_2': 'email_2@gmail.com'}
try:
for name in pairs.keys():
body = 'hi {}'.format(name)
print('Sending email to {}...'.format(name))
send_status = smtp_obj.sendmail(smtp_obj.user, pairs.get(name), body)
if send_status != {}:
print('There was a problem sending mail to {}.\n{}'.format(name, send_status))
except smtplib.SMTPDataError:
print('Sending email to {} failed.'.format(name))
finally:
smtp_obj.quit()
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
然而,当我运行这个时,我只得到一个 SMTPDataError,说:
smtplib.SMTPDataError: (550, b'5.7.0 From address is not one of your addresses')
Run Code Online (Sandbox Code Playgroud)
我尝试过对不同的地址进行硬编码。当我使用我的地址时,我得到了这个。当我使用一个我知道是错误的地址时,错误消息还会打印出无效的电子邮件(该帐户无法访问该电子邮件 - …
当用户输入电子邮件进行密码重置时,会出现与服务器故障错误相关的连接失败错误。
回程:
内部34中的文件“C:\ProgramData\Anaconda3\lib\site-packages\django-2.2b1-py3.7.egg\django\core\handlers\exception.py”。response = get_response(request)
_get_response 115 中的文件“C:\ProgramData\Anaconda3\lib\site-packages\django-2.2b1-py3.7.egg\django\core\handlers\base.py”。response = self.process_exception_by_middleware(e, request)
_get_response 113 中的文件“C:\ProgramData\Anaconda3\lib\site-packages\django-2.2b1-py3.7.egg\django\core\handlers\base.py”。response =wrapped_callback(request, *callback_args, * *回调_kwargs)
视图 71 中的文件“C:\ProgramData\Anaconda3\lib\site-packages\django-2.2b1-py3.7.egg\django\views\generic\base.py”。 return self.dispatch(request, *args, **夸格斯)
文件“C:\ProgramData\Anaconda3\lib\site-packages\django-2.2b1-py3.7.egg\django\utils\decorators.py”在_wrapper 45中。返回bound_method(*args, **kwargs)
文件“C:\ProgramData\Anaconda3\lib\site-packages\django-2.2b1-py3.7.egg\django\views\decorators\debug.py”在sensitive_post_parameters_wrapper 76中。 return view(request, *args, **夸格斯)
调度 53 中的文件“C:\ProgramData\Anaconda3\lib\site-packages\registration\views.py”。返回 super(RegistrationView, self).dispatch(request, *args, **kwargs)
调度 97 中的文件“C:\ProgramData\Anaconda3\lib\site-packages\django-2.2b1-py3.7.egg\django\views\generic\base.py”。 return handler(request, *args, **夸格斯)
文件“C:\ProgramData\Anaconda3\lib\site-packages\django-2.2b1-py3.7.egg\django\views\generic\edit.py”在帖子142中。返回self.form_valid(form)
form_valid 中的文件“C:\ProgramData\Anaconda3\lib\site-packages\registration\views.py” 56. new_user = self.register(form)
寄存器 100 中的文件“C:\ProgramData\Anaconda3\lib\site-packages\registration\backends\default\views.py”。request=self.request,
create_inactive_user 193 中的文件“C:\ProgramData\Anaconda3\lib\site-packages\registration\models.py”。 lambda: Registration_profile.send_activation_email(
退出284中的文件“C:\ProgramData\Anaconda3\lib\site-packages\django-2.2b1-py3.7.egg\django\db\transaction.py” 。connection.set_autocommit(True)
set_autocommit 409 中的文件“C:\ProgramData\Anaconda3\lib\site-packages\django-2.2b1-py3.7.egg\django\db\backends\base\base.py”。 self.run_and_clear_commit_hooks()
文件“C:\ProgramData\Anaconda3\lib\site-packages\django-2.2b1-py3.7.egg\django\db\backends\base\base.py”在 run_and_clear_commit_hooks 624.func()
194.站点中的文件“C:\ProgramData\Anaconda3\lib\site-packages\registration\models.py”,请求)
send_activation_email 451 中的文件“C:\ProgramData\Anaconda3\lib\site-packages\registration\models.py”。 email_message.send()
发送 291 …
我是 Django 新手,我想使用我的 Gmail 帐户通过 Django 服务器发送电子邮件,但不幸的是,即使我已正确输入所有凭据,我在发送电子邮件时仍遇到错误,但我的 Gmail 中还有一件事帐户是“不太安全的应用程序”,必须启用,但此功能在 Gmail 中不再可用,那么现在我如何在 Django 中发送电子邮件?
error while sending the email
b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials f14-20020a05600c4e8e00b0039c5642e430sm4688852wmq.20 - gsmtp'
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Ubuntu 12.04 上安装libsmtp,编译时出现错误,这是我遵循的步骤(它们与 Install.txt 文件中写入的完全相同)
cd libsmtp
./configure
Run Code Online (Sandbox Code Playgroud)
[上述命令的输出是]
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ANSI …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Python发送电子邮件并使用以下代码:
import smtplib
import datetime
SERVER = "localhost"
PORT = 1025
FROM = "me@mydevice.com"
TO = ["myemailaddress@something.com"]
SUBJECT = "test"
dt = datetime.datetime.now()
TEXT = "blabla bla @ " + str(dt)
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ",".join(TO), SUBJECT, TEXT)
server = smtplib.SMTP(SERVER,PORT)
server.sendmail(FROM,TO,message)
server.quit()
Run Code Online (Sandbox Code Playgroud)
没有安装/设置任何STMP服务器,我只是使用这个:
python -m smtpd -n -c DebuggingServer localhost:1025
Run Code Online (Sandbox Code Playgroud)
代码似乎运行正常,没有错误,服务器甚至通知我:
---------- MESSAGE FOLLOWS ----------
From: me@mydevice.com
To: myemailaddress@something.com
Subject: test
X-Peer: 127.0.0.1
blabla bla @ 2014-01-29 14:44:37.219724
------------ END …Run Code Online (Sandbox Code Playgroud) 对不起,如果问题没有发现,我很难正确地表达这个问题.
我正在尝试使用电子邮件模块创建一个简单的纯文本电子邮件,并在满足某些条件时发送它.我正在遇到各种异常行为,并希望了解它.我开始从官方示例(https://docs.python.org/3.4/library/email-examples.html)中提取一个简单的测试,它工作正常.当我开始尝试在我的项目中实现这一点时,我开始得到各种各样的"'module' object has no attribute 'something'".我可以运行这样的东西,它工作正常
import email
import smtplib
# Create message object
msg = email.message.EmailMessage()
# Create the from and to Addresses
from_address = email.headerregistry.Address("Stack of Pancakes", "pancakes@gmail.com")
to_address = email.headerregistry.Address("Joe", "pythontest@mailinator.com")
# Create email headers
msg['Subject'] = "subject of email"
msg['From'] = from_address
msg['To'] = to_address
#
email_content = "This is a plain text email"
msg.set_content(email_content)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("pancakes@gmail.com", "password")
server.send_message(msg)
server.quit()
Run Code Online (Sandbox Code Playgroud)
这非常有效.但是,如果我以不同的方式订购东西,事情就会开始破碎,我不明白为什 例如,如果我将from_address和to_address上面的行放在EmailMessage被调用的地方,就像这样
import …Run Code Online (Sandbox Code Playgroud) 我一直在使用 Jupyter 制作 html 报告,我希望能够使用 smtplib 通过电子邮件发送这些报告。我已经能够成功发送电子邮件,但无法将 html 报告附加到或嵌入到电子邮件中。
我一直在使用的代码看起来像这样......
fromaddr = "myemail@domain.com.au"
toaddr = "myfriendsemail@domain.com.au"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Automatic Weekly Report"
html = open("WeeklyReport.html")
part2 = MIMEText(html.read(), 'text/html')
msg.attach(part2)
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login("myemail@domain.com.au", "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
Run Code Online (Sandbox Code Playgroud)
我认为这个问题主要是我从https://docs.python.org/3.4/library/email-examples.html 获取和修改的中间部分
html = open("WeeklyPnlReport.html")
part2 = MIMEText(html.read(), 'text/html')
msg.attach(part2)
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我收到一封电子邮件,我在 Gmail 中打开该电子邮件,该电子邮件是空白的,并且有一个名为“noname”的附件。预览它不起作用并下载它导致我的电脑不知道用什么文件打开它。
如果我将中间位更改为:
part2 = MIMEText(html.read(), 'html')
Run Code Online (Sandbox Code Playgroud)
我收到一封电子邮件,上面写着“消息已剪辑”,然后当我单击“查看整个消息”时,会弹出一个包含报告的 html 文本的新选项卡。
如果我运行:
part2 = MIMEText(html, 'html')
Run Code Online (Sandbox Code Playgroud)
我收到错误“'_io.TextIOWrapper' …
我正在尝试发送电子邮件,但遇到此错误:
smtplib.SMTPAuthenticationError: (534, b'5.7.9 Application-specific password required. Learn more at\n5.7.9 https://support.google.com/mail/?p=InvalidSecondFactor d2sm13023190qkl.98 - gsmtp')
在网址中,我没有看到任何非常有用的东西,有人有任何提示吗?出于 SO 目的,我将电子邮件帐户密码保留为test与共享我的个人信息相比。
import smtplib
import ssl
# User configuration
sender_email = 'test@gmail.com'
receiver_email = 'test@gmail.com'
password = 'test'
# Email text
email_body = '''
This is a test email sent by Python. Isn't that cool?
'''
# Creating a SMTP session | use 587 with TLS, 465 SSL and 25
server = smtplib.SMTP('smtp.gmail.com', 587)
# Encrypts the email
context = ssl.create_default_context()
server.starttls(context=context)
# …Run Code Online (Sandbox Code Playgroud) smtplib ×10
python ×8
email ×4
smtp ×3
django ×2
attributes ×1
error-code ×1
gmail ×1
html ×1
icloud ×1
import ×1
linux ×1
python-2.7 ×1
python-3.x ×1
sendmail ×1
smtpclient ×1
smtpd ×1
spoofing ×1
ubuntu-12.04 ×1