在HTML电子邮件中使用变量?

2024-09-28 18:51:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用这篇文章中的信息Sending Email Using Python

到目前为止,指令是完美的。我还有两件事想做:

  1. 调用主体内的变量
  2. 添加附件

变量应该是今天的日期。就这样:

today = datetime.datetime.today ()
tday = today.strftime ("%m-%d-%Y")

我知道用mailx,你可以附加-a选项。


Tags: 信息附件todaydatetimeemail选项指令两件事
3条回答

对于第一个问题:有很多方法可以创建使用变量的字符串。

一些方法是:

body = "blablabla " + tday + " bloo bloo bloo"
body = "Today's date is {}, in case you wondered".format(tday)

对于第二个问题,您必须告诉我们您正在使用哪个库/模块,然后您可以转到该模块的文档页面,查看是否有添加附件的内容。

感谢大家贴出贴士。

对于后人来说,这就是工作脚本。

剩下的唯一一项是我需要能够发送相同的电子邮件给多个人。

我试图将所有电子邮件地址都添加到变量中,变量之间用逗号隔开,但他们没有收到。当我看到收到的电子邮件时,它们似乎在“收件人”行中。它可能只发送到列出的第一个电子邮件地址吗?

#!/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()

要调用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/

编辑: 上面提供的链接似乎不可用,因此通过电子邮件(特别是从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

相关问题 更多 >