python 3发送不同主题和正文的电子邮件

时间:2016-11-08 20:04:39

标签: python-3.x

我有以下代码给自己发送一些电子邮件。我需要随机更改变量bodyemail和subjectEmail。做这个的最好方式是什么?我在Excel文件中有一些文本,但我不确定从那里提取数据是否有意义改变这两个变量,甚至是如何做到这一点。

 import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


fromaddr = "Y@X.com"
toaddr = "X@X.com"
subjectEmail = "Subject"
bodyemail = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
  <head></head>
  <body>
    <p>Hi!<br>
       How are you?<br>
       Here is the <a href="http://www.python.org">link</a> you wanted.
    </p>
  </body>
</html>
"""
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = subjectEmail

body = bodyemail
msg.attach(MIMEText(body, 'plain'))

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

0 个答案:

没有答案