在 HTML 电子邮件中使用变量?
Use a variable inside HTML email?
我正在使用在此 post Sending Email Using Python
上找到的信息
到目前为止,说明是完美的。我还有两件事想做:
- 在正文中调用一个变量
- 添加附件
变量将是今天的日期。就是这样:
today = datetime.datetime.today ()
tday = today.strftime ("%m-%d-%Y")
我知道使用 mailx,您可以使用 -a 选项附加。
对于您的第一个问题:有很多方法可以创建使用变量的字符串。
部分方式为:
body = "blablabla " + tday + " bloo bloo bloo"
body = "Today's date is {}, in case you wondered".format(tday)
对于你的第二个问题,你必须告诉我们你正在使用哪个库/模块,然后你可以转到该模块的文档页面,看看是否有添加附件的东西。
要调用html体内的变量,只需将它们转换为字符串以在体内连接它们
from datetime import datetime
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
today = datetime.today ()
tday = today.strftime ("%m-%d-%Y")
# email subject, from , to will be defined here
msg = MIMEMultipart()
html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
""" +str(today)+ """ and """ +str(tday)+ """
</p>
</body>
</html>
"""
msg.attach(MIMEText(html, 'html'))
附件请看http://naelshiab.com/tutorial-send-email-python/
编辑:
上面提供的 link 似乎不可用,因此通过电子邮件(特别是来自 gmail)发送附件的代码片段如下
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
msg = MIMEMultipart()
msg['From'] = "from email address"
msg['To'] = "to email address"
msg['Subject'] = "Subject line"
body = """Body
content"""
msg.attach(MIMEText(body, 'plain'))
attachment = open("/path/to/file", "rb")
p = MIMEBase('application', 'octet-stream')
# To change the payload into encoded form
p.set_payload((attachment).read())
# encode into base64
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
# attach the instance 'p' to instance 'msg'
msg.attach(p)
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls() # for security
s.login("from email address", "your password")
text = msg.as_string()
# sending the mail
s.sendmail("from email address", "to email address" , text)
s.quit()
注意:Google 有时会阻止来自其他应用程序(安全性较低的应用程序)的登录,因此需要在您的 Google 帐户设置中允许此访问
https://myaccount.google.com/u/1/lesssecureapps?pli=1&pageId=none
感谢大家发帖提示。
为了后代,这是工作脚本。
唯一剩下的项目是我需要能够将同一封电子邮件发送给多个人。
我尝试将所有电子邮件地址添加到变量中,中间用逗号分隔,但他们没有收到。当我查看收到的电子邮件时,它们似乎在“收件人”行中。是否可能只发送到列出的第一个电子邮件地址?
#!/usr/bin/python
import smtplib
import time
import datetime
from datetime import date
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
fromaddr = "NOREPLY@test.com"
toaddr = ['me@test.com', 'thatguy@test.com']
# Date
today = datetime.datetime.today ()
tday = today.strftime ("%m-%d-%Y")
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = ", ".join(toaddr)
msg['Subject'] = "My Subject Goes Here"
body = """\
<html>
<head></head>
<body>
<p>DO NOT REPLY TO THIS EMAIL!!<br>
<br>
Script run for data as of """ + tday + """.<br>
<br>
See attachment for items to discuss<br>
<br>
The files have also been uploaded to <a href="http://testing.com/getit">SharePoint</a><br>
<br>
If you have any issues, email admin@test.com<br>
<br>
</p>
</body>
</html>
"""
msg.attach(MIMEText(body, 'html'))
filename = "discuss.csv"
attachment = open("discuss.csv", "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP('localhost')
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
我正在使用在此 post Sending Email Using Python
上找到的信息到目前为止,说明是完美的。我还有两件事想做:
- 在正文中调用一个变量
- 添加附件
变量将是今天的日期。就是这样:
today = datetime.datetime.today ()
tday = today.strftime ("%m-%d-%Y")
我知道使用 mailx,您可以使用 -a 选项附加。
对于您的第一个问题:有很多方法可以创建使用变量的字符串。
部分方式为:
body = "blablabla " + tday + " bloo bloo bloo"
body = "Today's date is {}, in case you wondered".format(tday)
对于你的第二个问题,你必须告诉我们你正在使用哪个库/模块,然后你可以转到该模块的文档页面,看看是否有添加附件的东西。
要调用html体内的变量,只需将它们转换为字符串以在体内连接它们
from datetime import datetime
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
today = datetime.today ()
tday = today.strftime ("%m-%d-%Y")
# email subject, from , to will be defined here
msg = MIMEMultipart()
html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
""" +str(today)+ """ and """ +str(tday)+ """
</p>
</body>
</html>
"""
msg.attach(MIMEText(html, 'html'))
附件请看http://naelshiab.com/tutorial-send-email-python/
编辑: 上面提供的 link 似乎不可用,因此通过电子邮件(特别是来自 gmail)发送附件的代码片段如下
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
msg = MIMEMultipart()
msg['From'] = "from email address"
msg['To'] = "to email address"
msg['Subject'] = "Subject line"
body = """Body
content"""
msg.attach(MIMEText(body, 'plain'))
attachment = open("/path/to/file", "rb")
p = MIMEBase('application', 'octet-stream')
# To change the payload into encoded form
p.set_payload((attachment).read())
# encode into base64
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
# attach the instance 'p' to instance 'msg'
msg.attach(p)
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls() # for security
s.login("from email address", "your password")
text = msg.as_string()
# sending the mail
s.sendmail("from email address", "to email address" , text)
s.quit()
注意:Google 有时会阻止来自其他应用程序(安全性较低的应用程序)的登录,因此需要在您的 Google 帐户设置中允许此访问 https://myaccount.google.com/u/1/lesssecureapps?pli=1&pageId=none
感谢大家发帖提示。
为了后代,这是工作脚本。
唯一剩下的项目是我需要能够将同一封电子邮件发送给多个人。
我尝试将所有电子邮件地址添加到变量中,中间用逗号分隔,但他们没有收到。当我查看收到的电子邮件时,它们似乎在“收件人”行中。是否可能只发送到列出的第一个电子邮件地址?
#!/usr/bin/python
import smtplib
import time
import datetime
from datetime import date
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
fromaddr = "NOREPLY@test.com"
toaddr = ['me@test.com', 'thatguy@test.com']
# Date
today = datetime.datetime.today ()
tday = today.strftime ("%m-%d-%Y")
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = ", ".join(toaddr)
msg['Subject'] = "My Subject Goes Here"
body = """\
<html>
<head></head>
<body>
<p>DO NOT REPLY TO THIS EMAIL!!<br>
<br>
Script run for data as of """ + tday + """.<br>
<br>
See attachment for items to discuss<br>
<br>
The files have also been uploaded to <a href="http://testing.com/getit">SharePoint</a><br>
<br>
If you have any issues, email admin@test.com<br>
<br>
</p>
</body>
</html>
"""
msg.attach(MIMEText(body, 'html'))
filename = "discuss.csv"
attachment = open("discuss.csv", "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP('localhost')
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()