Python: 发送具有多个接收者的多部分消息
Python: send multipart message with multiple recievers
我已经提到了这些帖子 - , 。请不要将其标记为重复
我正在尝试使用 python smtplib
发送电子邮件
rec_list.append(temp_email_df.iloc[0,4:].to_string(header=False, index=False))
print(rec_list) # this list contains one email id
print(type(rec_list))
message = MIMEMultipart()
message['Subject'] = 'For your review'
message['From'] = 'user1@org.com'
message['To'] = ", ".join(rec_list)
我的收件人电子邮件地址来自 pandas 数据框,如上所示。
后来,根据stack overflow的建议,我已经使用了join
运算符,并把它们放在了列表中。
我的 send_message 代码如下所示
msg_body = message.as_string()
server = SMTP('test.com', 25)
#server.send_message(msg_body,message['From'],message['To']) # doesn't work
server.send_message(message['From'],rec_list,msg_body) # doesn;t work
server.quit()
rec_list.clear()
I get error message as shown below
---> 71 server.send_message(message['From'],rec_list,msg_body)
72 server.quit()
73 rec_list.clear()
~\Anaconda3\lib\smtplib.py in send_message(self, msg, from_addr,
to_addrs, mail_options, rcpt_options)
942
943 self.ehlo_or_helo_if_needed()
--> 944 resent = msg.get_all('Resent-Date')
945 if resent is None:
946 header_prefix = ''
AttributeError: 'str' object has no attribute 'get_all'
问题是您正在使用 send_message(), when you should be using sendmail(). The send_message
method is designed to work with EmailMessage objects, whereas you are using a MIMEMultipart 对象。
发送带附件的电子邮件的方法如下:
首先,启动本地调试服务器(仅用于测试):
$ python -m smtpd -n -c DebuggingServer localhost:1025
现在发送实际的电子邮件:
import smtplib
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
message = MIMEMultipart()
message.attach(MIMEText('Test'))
part = MIMEApplication('Attachment', Name='file.txt')
part['Content-Disposition'] = 'attachment; filename="file.txt"'
message.attach(part)
sender = 'myaddress@123.com'
receivers = ['someone@abc.com', 'someone@xyz.com']
message['Subject'] = 'Test Email'
message['From'] = sender
message['To'] = ', '.join(receivers)
with smtplib.SMTP('localhost', 1025) as smtp:
smtp.sendmail(sender, receivers, message.as_string())
这会在调试服务器上产生以下输出:
---------- MESSAGE FOLLOWS ----------
b'Content-Type: multipart/mixed; boundary="===============8897222788102853002=="'
b'MIME-Version: 1.0'
b'Subject: Test Email'
b'From: myaddress@123.com'
b'To: someone@abc.com, someone@xyz.com'
b'X-Peer: ::1'
b''
b'--===============8897222788102853002=='
b'Content-Type: text/plain; charset="us-ascii"'
b'MIME-Version: 1.0'
b'Content-Transfer-Encoding: 7bit'
b''
b'Test'
b'--===============8897222788102853002=='
b'Content-Type: application/octet-stream; Name="file.txt"'
b'MIME-Version: 1.0'
b'Content-Transfer-Encoding: base64'
b'Content-Disposition: attachment; filename="file.txt"'
b''
b'QXR0YWNobWVudA=='
b''
b'--===============8897222788102853002==--'
------------ END MESSAGE ------------
我已经提到了这些帖子 -
我正在尝试使用 python smtplib
发送电子邮件rec_list.append(temp_email_df.iloc[0,4:].to_string(header=False, index=False))
print(rec_list) # this list contains one email id
print(type(rec_list))
message = MIMEMultipart()
message['Subject'] = 'For your review'
message['From'] = 'user1@org.com'
message['To'] = ", ".join(rec_list)
我的收件人电子邮件地址来自 pandas 数据框,如上所示。
后来,根据stack overflow的建议,我已经使用了join
运算符,并把它们放在了列表中。
我的 send_message 代码如下所示
msg_body = message.as_string()
server = SMTP('test.com', 25)
#server.send_message(msg_body,message['From'],message['To']) # doesn't work
server.send_message(message['From'],rec_list,msg_body) # doesn;t work
server.quit()
rec_list.clear()
I get error message as shown below
---> 71 server.send_message(message['From'],rec_list,msg_body) 72 server.quit() 73 rec_list.clear()
~\Anaconda3\lib\smtplib.py in send_message(self, msg, from_addr, to_addrs, mail_options, rcpt_options) 942 943 self.ehlo_or_helo_if_needed() --> 944 resent = msg.get_all('Resent-Date') 945 if resent is None: 946 header_prefix = ''
AttributeError: 'str' object has no attribute 'get_all'
问题是您正在使用 send_message(), when you should be using sendmail(). The send_message
method is designed to work with EmailMessage objects, whereas you are using a MIMEMultipart 对象。
发送带附件的电子邮件的方法如下:
首先,启动本地调试服务器(仅用于测试):
$ python -m smtpd -n -c DebuggingServer localhost:1025
现在发送实际的电子邮件:
import smtplib
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
message = MIMEMultipart()
message.attach(MIMEText('Test'))
part = MIMEApplication('Attachment', Name='file.txt')
part['Content-Disposition'] = 'attachment; filename="file.txt"'
message.attach(part)
sender = 'myaddress@123.com'
receivers = ['someone@abc.com', 'someone@xyz.com']
message['Subject'] = 'Test Email'
message['From'] = sender
message['To'] = ', '.join(receivers)
with smtplib.SMTP('localhost', 1025) as smtp:
smtp.sendmail(sender, receivers, message.as_string())
这会在调试服务器上产生以下输出:
---------- MESSAGE FOLLOWS ----------
b'Content-Type: multipart/mixed; boundary="===============8897222788102853002=="'
b'MIME-Version: 1.0'
b'Subject: Test Email'
b'From: myaddress@123.com'
b'To: someone@abc.com, someone@xyz.com'
b'X-Peer: ::1'
b''
b'--===============8897222788102853002=='
b'Content-Type: text/plain; charset="us-ascii"'
b'MIME-Version: 1.0'
b'Content-Transfer-Encoding: 7bit'
b''
b'Test'
b'--===============8897222788102853002=='
b'Content-Type: application/octet-stream; Name="file.txt"'
b'MIME-Version: 1.0'
b'Content-Transfer-Encoding: base64'
b'Content-Disposition: attachment; filename="file.txt"'
b''
b'QXR0YWNobWVudA=='
b''
b'--===============8897222788102853002==--'
------------ END MESSAGE ------------