使用python将邮件发送给所需的收件人

时间:2018-11-28 06:43:48

标签: python email mime

我正在尝试将电子邮件从我的正式ID自动发送到另一个正式ID。为此,我使用了以下python脚本。

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

fromaddr = "<SENDER MAIL ID>"
toaddr = "<RECIPIENT MAIL ID>"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "SUBJECT OF THE MAIL"

body = "Robot Uprising, We are coming for you"
msg.attach(MIMEText(body, 'plain'))

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "YOUR PASSWORD")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

执行此操作时,我面临以下问题:

Traceback (most recent call last):
  File "mailsendtest.py", line 2, in <module>
    from email.MIMEMultipart import MIMEMultipart
ImportError: No module named MIMEMultipart

我是python的入门者。请提供输入。谢谢!

1 个答案:

答案 0 :(得分:0)

假设您使用的是Python3,我发现您的imports存在两个问题。

  1. from email.MIMEMultipart import MIMEMultipart更改为from email.mime.multipart import MIMEMultipart

  2. from email.MIMEText import MIMEText更改为from email.mime.text import MIMEText

如果这些库不起作用,请确保已安装这些库。