我正在尝试整理一个脚本,该脚本会自动将符合特定条件的某些电子邮件转发给另一封电子邮件.
我使用imaplib和电子邮件工作下载和解析消息,但我无法弄清楚如何将整个电子邮件转发到另一个地址.我是否需要从头开始构建新消息,还是可以以某种方式修改旧消息并重新发送?
这是我到目前为止(client是一个imaplib.IMAP4连接,id是一个消息ID):
import smtplib, imaplib
smtp = smtplib.SMTP(host, smtp_port)
smtp.login(user, passw)
client = imaplib.IMAP4(host)
client.login(user, passw)
client.select('INBOX')
status, data = client.fetch(id, '(RFC822)')
email_body = data[0][1]
mail = email.message_from_string(email_body)
# ...Process message...
# This doesn't work
forward = email.message.Message()
forward.set_payload(mail.get_payload())
forward['From'] = 'source.email.address@domain.com'
forward['To'] = 'my.email.address@gmail.com'
smtp.sendmail(user, ['my.email.address@gmail.com'], forward.as_string())
Run Code Online (Sandbox Code Playgroud)
我确定我需要对消息的MIME内容稍微复杂一些.当然,有一些简单的方法可以转发整个邮件吗?
# This doesn't work either, it just freezes...?
mail['From'] = 'source.email.address@domain.com'
mail['To'] = 'my.email.address@gmail.com'
smtp.sendmail(user, ['my.email.address@gmail.com'], mail.as_string())
Run Code Online (Sandbox Code Playgroud) 我试图使用python发送电子邮件,但尽管我使用本地SMTP服务器,它似乎需要身份验证.我运行的代码和我得到的错误可以在下面看到.我使用端口587,因为无法在我的服务器上打开端口25.你能帮我在端口587上使用python设置本地SMTP服务器吗?
>>> import smtplib
>>> from email.mime.text import MIMEText
>>> msg = MIMEText('Test body')
>>> me = 'support@mywebsite.com'
>>> to = 'myemail@gmail.com'
>>> msg['Subject'] = 'My Subject'
>>> msg['From'] = me
>>> msg['To'] = to
>>> s = smtplib.SMTP('localhost', 587)
>>> s.sendmail(me, [to], msg.as_string())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/smtplib.py", line 722, in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (530, '5.7.0 Authentication required', 'support@mywebsite.com')
Run Code Online (Sandbox Code Playgroud) 我通过以下代码发送电子邮件:
msg = MIMEText(u'<a href="www.google.com">abc</a>')
msg['Subject'] = 'subject'
msg['From'] = 'xxx'
msg['To'] = 'xxx'
s = smtplib.SMTP(xxx, 25)
s.sendmail(xxx, xxx, msg.as_string())
Run Code Online (Sandbox Code Playgroud)
我想要收到的是
我实际收到的是:
<a href="www.google.com">abc</a>
Run Code Online (Sandbox Code Playgroud) 我通过Python email/ 发送的电子邮件有一个奇怪的问题smtplib。
我正在尝试撰写包含以下内容的电子邮件:
MIME结构的设置如下:
multipart/mixed
multipart/alternative
text/plain
multipart/related
text/html
image/png - inline
application/pdf - attachment
Run Code Online (Sandbox Code Playgroud)
除 Windows 10邮件客户端外,我测试过的所有邮件客户端{在Android上的BlueMail,iOS邮件客户端,Roundcube}似乎都可以正常工作。出于某种原因,Windows 10内置邮件客户端似乎可以很好地显示嵌入式图像,但是看不到其他附件的痕迹。
我可以在Internet上找到的有限信息表明这是Windows 10邮件客户端的错误,但是我个人在此客户端中收到了其他电子邮件,包括内联附件和附件,它们显示得很好-因此,显然是某种可行的解决方法/替代消息结构。
因此,我的问题是:如何更改此邮件的格式,使其在所有相关的邮件客户端中正确显示?
我正在用Python编写这样的电子邮件:
message = MIMEMultipart("mixed")
message["From"] = ...
.
.
.
bodyText = "..."
bodyHTML = "..."
mailFrom = "..."
targetEmail = "..."
imageContent = ...
messageBody = MIMEMultipart("alternative")
messageBody.attach(MIMEText(bodyText, "plain"))
messageBodyHTML = MIMEMultipart("related")
messageBodyHTML.attach(MIMEText(bodyHTML, "html"))
messageImage = MIMEImage(imageContent)
messageImage.add_header("Content-Disposition", 'inline; filename="..."')
messageImage.add_header("Content-ID", "<id used in html body>")
messageBodyHTML.attach(messageImage) …Run Code Online (Sandbox Code Playgroud) 我当前的脚本允许我发送电子邮件很好,但只有一些它不喜欢的字符,特别是':'在这个示例中.
import smtplib, sys
mensaje = sys.argv[1]
def mailto(toaddrs, msg):
fromaddr = 'myemailblabla'
username = 'thisismyemail'
password = '122344'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username, password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
mailto('test@gmail.com', mensaje)
Run Code Online (Sandbox Code Playgroud)
如果我写一个示例消息,例如,让我们说它"Hi there\n how are you?"工作正常,但让我说我尝试发送一个网址http://www.neopets.com,电子邮件发送空白.我相信':'这个问题的原因,所以我试图逃避它,但没有.
我想在低RAM的VPS中发送10MB或更多附件的电子邮件; 在Python 3中发送带附件的电子邮件的常用方法(我发现)是这样的:
from email.message import EmailMessage
# import other needed stuff here omitted for simplicity
attachment = 'some_file.tar'
msg = EmailMessage()
# set from, to, subject here
# set maintype, subtype here
with open(attachment, 'rb') as fd:
msg.add_attachment(fd.read(), # this is the problem, the whole file is loaded
maintype=maintype,
subtype=subtype,
filename=attachment)
# smtp_serv is an instance of smtplib.SMTP
smtp_serv.send_message(msg)
Run Code Online (Sandbox Code Playgroud)
使用这种方法,整个文件被加载到内存中,然后使用smtplib.SMTP.send_message发送EmailMessage对象,我期望的是一种给add_attachment提供文件描述符(或可迭代)而不是文件内容的方法,当附件被发送到服务器时,以惰性方式(例如逐行或一些固定数量的字节)读取,例如:
with open('somefile') as fd:
msg.add_attachment(fd, maintype=mt, subtype=st, filename=fn)
smtp_serv.send_message(msg)
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点(发送一个附件,而不是一次加载整个文件)与标准库(电子邮件和smtplib)???? 我在python文档中找不到任何线索.
提前致谢.
我正在尝试使用 Gmail 创建一个发送电子邮件的脚本。但是,当运行以下行时,我的代码会冻结:
smtplib.SMTP("smtp.gmail.com", 587)
Run Code Online (Sandbox Code Playgroud)
这是在我输入用户名和密码之前,所以与我的 Gmail 帐户无关。为什么会这样?我正在使用 Python 3.6.3
完整代码如下:
import smtplib
# Specifying the from and to addresses
fromaddr = 'XXX@gmail.com'
toaddrs = 'YYY@gmail.com'
# Writing the message (this message will appear in the email)
msg = 'Enter you message here'
# Gmail Login
username = 'XXX@gmail.com'
password = 'PPP'
# Sending the mail
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
Run Code Online (Sandbox Code Playgroud) 我正在尝试编辑一个文本文件,然后使用 python 脚本将其作为电子邮件正文发送,但我收到了 unicode 编码错误。经过一番研究,我发现解决方案是使用 .encode('utf-8') 方法,但这对我不起作用,因为 sendmail() 方法只发送字符串
这是我使用的python代码片段:
irtem = open('irtemplate.txt')
data = irtem.read().replace('(name)', eng_name).replace('(customer)',
cu_name).replace('(sr)', SR_num).replace('(problem)',
prob_description).replace('(email)', eng_email).replace('(details)',
details_req).replace('(tele)', eng_tele)
message_text = data
message = "From: %s\r\n" % fromaddr + "To: %s\r\n" % toaddr + "CC:
%s\r\n" % ",".join(cc) + "Subject: %s\r\n" % message_subject + "\r\n" +
message_text
toaddrs = [toaddr] + cc + bcc
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, message)
server.quit()
Run Code Online (Sandbox Code Playgroud)
追溯:
Traceback (most recent call last):
File "autoIR.py", line 39, in <module>
server.sendmail(fromaddr, toaddrs, message)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", …Run Code Online (Sandbox Code Playgroud) 我正在使用带有质子邮件的苹果邮件应用程序 - 我有桥接应用程序。(此处安装 MacOS 和 Windows;此处安装linux。)
激活桥接应用程序后,我尝试使用 smtp 库用 python 发送电子邮件,但它不起作用。这是我尝试运行但失败的代码:
import smtplib
server = smtplib.SMTP("127.0.0.1", portnumber)
server.login("mymail@protonmail.com", "my password")
server.sendmail(
"mymail@protonmail.com",
"receiver@protonmail.com",
"hello")
server.quit()
Run Code Online (Sandbox Code Playgroud)
我收到的错误消息:
smtplib.SMTPDataError: (554, b'Error: 交易失败,归咎于天气:格式错误的 MIME 标题行:00')
smtplib.SMTPAuthenticationError: (534, b'5.7.9 需要应用程序专用密码。了解更多信息,请访问\n5.7.9 https://support.google.com/mail/?p=InvalidSecondFactor h22sm15927247pfv.25 - gsmtp')
环境变量已设置但不起作用(即使它是确切的值)。
export EMAIL_USER='da24@gmail.com'os.environ.get('EMAIL_USER')邮件值字符串是否为True我试图将其导入配置文件中,当我使用编码值时,它工作正常,但使用 I use 时os.environ.get('EMAIL_USER'),它会停止工作并引发此错误。