我有以下代码给自己发送一些电子邮件。我需要随机更改变量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()