我有一个包含很多行的 file.txt 文件。我希望随机选择任何一行并通过电子邮件发送。这个循环了三次。
import smtplib,datetime,random
def mail(em):
#print "yo"
fromAddress = "atadmorecouth@gmail.com"
toAddress = "atadmorecouth@gmail.com"
subject = "WOW " + str(datetime.datetime.now())
#print subject
msg = em
server=smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
password = "mypassword"
server.login(fromAddress, password)
server.sendmail(fromAddress, toAddress, msg)
#print server.sendmail(fromAddress, toAddress, msg)
for i in range(0,3):
em=str(random.choice(list(open('file.txt'))))
mail(em)
Run Code Online (Sandbox Code Playgroud)
这段代码不起作用。登录一切都很好。我注意到的唯一问题是 server.sendmail(fromAddress, toAddress, msg) 返回一个空的 {} 对此可能有什么解决办法?首先问题出在哪里?我已经在终端中运行了 mail() 函数,看起来工作正常。所有文件都托管在这里
文档指定您可以包含mail_options=[],rcpt_options=[]作为sendmail或 的参数send_message。我一直无法在任何地方找到这些选项的确切含义。我假设在那里的某个地方我可以设置 DSN。我已经构建了一个运行良好的电子邮件脚本;不需要关于如何在 Python 中使用 smtp 的帮助。
有谁知道描述这些选项以及如何使用它们的文档?
https://docs.python.org/3.5/library/smtplib.html
https://docs.python.org/3.5/library/email-examples.html#email-examples
我想使用 smtplib 使用 cron 作业每天发送一次状态邮件。
邮件发送效果很好,但是发送时间和日期似乎总是我阅读邮件时的时间和日期,而不是发送邮件时的时间和日期。这可能是 6 小时后。
我还没有找到有关向 smtplib 提供发送时间以及消息数据的提示。我是否遗漏了任何东西,还是我的邮件服务器配置有问题?但是,其他通过 Thunderbird 上交的邮件在此帐户下不会显示此效果。
我的python程序(删除了登录数据)如下所示:
import smtplib
sender = 'abc@def.com'
receivers = ['z@def.com']
message = """From: Sender <abc@def.com>
To: Receiver<z@def.com>
Subject: Testmail
Hello World.
"""
try:
smtpObj = smtplib.SMTP('mailprovider.mailprovider.com')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
Run Code Online (Sandbox Code Playgroud)
[编辑]
按照建议使用电子邮件包进行编码,但我的收件箱中显示的时间仍然是阅读时间而不是发送时间。
import smtplib
from email.mime.text import MIMEText
sender = ..
receiver = ..
message = "Hello World"
msg = MIMEText(message)
msg['Subject'] = 'Testmessage'
msg['From'] = sender …Run Code Online (Sandbox Code Playgroud) 我试图使用smtplib在python 2.7中发送邮件.以下代码非常简单:
import smtplib
def main(argv=None):
sender = 'abc@gmail.com'
receivers = ['xyz@gmail.com']
message = """
This is a test e-mail message.
"""
smtpObj = smtplib.SMTP('xyz@gmail.com',25)
smtpObj.login('abc', 'pwd')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
现在,当我执行下面的代码时,我不断收到此异常:
smtplib.SMTPAuthenticationError:(535,'5.7.3身份验证失败').
好心提醒.
谢谢,
我正在尝试使用 python 和库在我的计算机上运行我自己的 stmp 服务器aiosmtpd。
我运行这个例子,一切看起来都很好,但我从未收到对方的电子邮件。
不知道是否有日志可以看到。
我使用的是 Visual Studio 2015、Python 3.5 和 Windows 8.1
我看到过类似的帖子,但没有帮助。
重要提示:
在客户端代码中,我也尝试过不使用日期标头
服务器.py:
import asyncio import logging
from aiosmtpd.controller import Controller
from aiosmtpd.handlers import Sink
from smtplib import SMTP
async def amain(loop):
cont = Controller(Sink(), hostname='::0', port=8025)
cont.start()
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
loop = asyncio.get_event_loop()
loop.create_task(amain(loop=loop))
try:
loop.run_forever()
except KeyboardInterrupt:
pass
Run Code Online (Sandbox Code Playgroud)
客户端.py:
from smtplib import SMTP import smtplib
s = SMTP('localhost', 8025) try:
s.set_debuglevel(True)
s.sendmail('andy@love.com', ['bob@hate.com'], """\
Date:17/05/2017,2:18
From: andy@love.com
To: …Run Code Online (Sandbox Code Playgroud) 此问题与 smtplib 的 SMTP_SSL 连接有关。
虽然与 SMTP 连接(没有 ssl),但它正在工作。
在 SMTP_SSL 中尝试相同的主机和端口时,出现错误。该错误仅基于主机,gmail 设置也可以正常工作。
请查看下面的示例,让我知道 Outlook 和 office365 是否需要进行任何更改。
In [1]: import smtplib
In [10]: smtplib.SMTP_SSL('smtp.gmail.com', 465, timeout=100)
Out[10]: <smtplib.SMTP_SSL instance at 0x109ccaf38>
In [2]: smtplib.SMTP('smtp-mail.outlook.com', 587, timeout=100)
Out[1]: <smtplib.SMTP instance at 0x10a00bb90>
In [3]: smtplib.SMTP_SSL('smtp-mail.outlook.com', 587, timeout=100)
---------------------------------------------------------------------------
SSLError Traceback (most recent call last)
<ipython-input-9-f0c5b0de4e24> in <module>()
----> 1 smtplib.SMTP_SSL('smtp-mail.outlook.com', 587, timeout=100)
/python2.7/smtplib.pyc in __init__(self, host, port, local_hostname, keyfile, certfile, timeout)
794 self.keyfile = keyfile
795 self.certfile = certfile
--> …Run Code Online (Sandbox Code Playgroud) 谁能帮我解决这个错误:AttributeError: SMTP_SSL instance has no attribute ' exit '。
我正在开发一个使用 Python 发送多封电子邮件的小模块。Python 版本:2.7.15 操作系统:MacOS X
import smtplib
import ssl
port = 465 # For SSL
smtp_server = "smtp.gmail.com"
sender_email = "abc@gmail.com" # type: str # Enter your address
receiver_email = "xyz@gmail.com" # Enter receiver address
password = 'xyz@0101'
message = """\
Subject: Hi there
This message is sent from Python."""
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context ) as server:
server.login(sender_email, password)
server.sendmail(sender_email, message, receiver_email)
Run Code Online (Sandbox Code Playgroud) 我正在 python 中使用“smtplib”发送带有 html 内容的邮件,我想向该 html 添加动态内容。
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
message = MIMEMultipart("alternative")
message["Subject"] = "Error Notification"
message["From"] = sender
message["To"] = sender
# Create the plain-text and HTML version of your message
html = """\
<html>
<body>
<p>Hi,<br>
<span>Something went wrong !</span><br>
</p>
</body>
</html>
"""
part1 = MIMEText(html, "html")
# Add HTML/plain-text parts to MIMEMultipart message
message.attach(part1)
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message.as_string())
print "Successfully sent email"
except smtplib.SMTPException:
print …Run Code Online (Sandbox Code Playgroud) 我编写了一个非常简单的 Python 脚本来自动发送电子邮件。这是它的代码:
import smtplib
From = "LorenzoTheGabenzo@gmx.com"
To = ["LorenzoTheGabenzo@gmx.com"]
with smtplib.SMTP('smtp.gmx.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login("LorenzoTheGabenzo@gmx.com", Password)
Subject = "Test"
Body = "TestingTheBesting"
Message = f"{Subject}\n\n{Body}"
smtp.sendmail(From, To, Message)
Run Code Online (Sandbox Code Playgroud)
每当我运行此代码时,我都会收到一个非常奇怪的错误,告诉我此发件人是“未经授权的发件人”。这是完整的错误
File "test.py", line 17, in <module> smtp.sendmail(From, To, Message)
File "C:\Users\James\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 888, in sendmail888, in sendmail raise SMTPDataError(code, resp)smtplib.SMTPDataError: (554, b'Transaction failed\nUnauthorized sender address.')
Run Code Online (Sandbox Code Playgroud)
我已经在 GMX 设置中启用了 SMTP 访问,但不确定现在还需要做什么来解决此问题。
注意:我知道变量密码尚未定义。这是因为我在发布之前故意删除了它,它是在我的原始代码中定义的。
我已经浏览了很多 SO 帖子和 smtplib 文档,一切似乎都是正确的,但邮件仅发送给列表中的第一个收件人
注意:我使用的是Python3.7,我也尝试过Python 2.6,在下面的情况下,邮件仅发送给接收器中的第一个收件人
下面是我的代码:
import smtplib
from email.mime.text import MIMEText
sender='from@domain.com'
receiver=['email1@domain.com', 'email2@domain.com', 'email3@domain.com']
msg = MIMEText("message")
msg['Subject'] = "Test Email"
msg['From'] = sender
msg['To'] = ",".join(receiver)
server = smtplib.SMTP("smtp.domain", 25)
sever.sendmail(sender, receiver, msg.as_string())
server.quit()
Run Code Online (Sandbox Code Playgroud) smtplib ×10
python ×9
email ×4
smtp ×2
ssl ×2
aiosmtpd ×1
html ×1
python-2.7 ×1
python-2.x ×1
python-3.7 ×1
receiver ×1
sendmail ×1