【问题标题】:String Formatting in Python/ThunderbirdPython/Thunderbird 中的字符串格式化
【发布时间】:2020-06-23 02:08:36
【问题描述】:

Noob,尝试使用 Thunderbird(而不是 SMTP)向几十个人发送个性化电子邮件。我基本上希望在 Thunderbird 中显示如下消息:

Dear Bob, 

It was nice to meet you the other day.

但是,我最终得到:

Dear Bob (comma missing, and rest of body missing)

我尝试了以下方法:

import subprocess
import os

def send_email(name, email_address):
    #print(name, email_address)
    os.system("thunderbird -compose to= 'to',subject='subject',body='body'")
    tbirdPath = r'c:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe'
    to = email_address
    subject = 'Test Subject LIne'
    #body = "Dear %s, \n\n This is the body." %(name)
    body = 'html><body>Dear %s, This is the body <br></body></html>'%(name) 
    composeCommand = 'format=html,to={},subject={},body={}'.format(to, subject, body)
    subprocess.Popen([tbirdPath, '-compose', composeCommand])

与往常一样,我可以实现的简单答案优于我无法实现的复杂答案。我怀疑我在字符串格式方面遗漏了一些愚蠢的东西,但不确定到底是什么。提前感谢您的帮助。

【问题讨论】:

  • 好像你在 Windows 上。试试\r\n
  • 是的,正在运行 Windows。 \r\n 没有变化

标签: python string-formatting


【解决方案1】:

来自this example,您可能需要用单引号和双引号将参数括起来。

像这样:

composeCommand = '"format=html,to=\'{}\',subject=\'{}\',body=\'{}\'"'.format(to, subject, body)

顺便说一句,如果你使用的是 python 3.6+,使用 f-strings 会使 str 更具可读性:

body = f'<html><body>Dear {name}, This is the body <br></body></html>' 
composeCommand = f'"format=html,to=\'{to}\',subject=\'{subject}\',body=\'{body}\'"'

【讨论】:

  • 我尝试在两行中添加,没有变化,但是谢谢
  • 当语法如下时它对我有用:composeCommand = "format=html,to={},subject={},body='{}'".format(to, subject,身体)
【解决方案2】:

所以这里有一个简单的程序,可以从 CSV 文件中读取姓名和电子邮件地址,并在 Windows 机器上使用 Python 自动从 Thunderbird 客户端起草电子邮件(您仍然需要在每个客户端上点击发送)。

import csv
import subprocess
import os

# a list that will contain OrderedDict ('Name', 'Bob'), ('email', bob@yahoo.com) 
each_persons_info = []

def load_email_info(data_file):
    """
    Load data from CSV files into memory.
    """
    # Load people
    with open(f"email_list.csv", encoding="utf-8") as f:
        reader = csv.DictReader(f)
        # using DictReader, starts reading at row 2, with row 1 forming your labels, append to each_persons_info list (differs from csv reader in that respect)        
    for row in reader:
        each_persons_info.append(row)

def send_email(name, email_address):
    """
    Launches Thunderbird and drafts personalized emails to people on your list, using content you supply in subject and body fields below.
    """
    subject = 'Test Subject LIne'
    body = "Dear {}".format(name) +  '\n' + '\n'  + "This is the body." + '\n'  + '\n'  + "The End." + '\n'

    to = email_address
    os.system("thunderbird -compose to= 'to',subject='subject',body='body'")
    tbirdPath = r'c:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe'
    composeCommand = "format=html,to={},subject={},body='{}'".format(to, subject, body) 
    subprocess.Popen([tbirdPath, '-compose', composeCommand])

def main():
    load_email_info("email_list.csv")

    # walk each person through the send email function 
    for item in each_persons_info:
        send_email(name, email_address)

if __name__ == "__main__":
    main()

【讨论】:

    猜你喜欢
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多