Python/Thunderbird 中的字符串格式 [英] String Formatting in Python/Thunderbird

查看:34
本文介绍了Python/Thunderbird 中的字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Noob, trying to use Thunderbird (rather than SMTP) to send personalized emails to a few dozen people. I am basically looking to have the message display in Thunderbird as follows:

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])

与往常一样,我可以实现的简单答案比我不能实现的复杂答案更受欢迎.我怀疑我错过了关于字符串格式的一些愚蠢的东西,但我不确定到底是什么.预先感谢您的帮助.

As always, simple answers I can implement are preferred to complex ones I cannot. I suspect I'm missing something stupid about string formatting, but am unsure as to exactly what. Thanks in advance for your help.

推荐答案

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

So here is a simple program to read in names and email addresses from a CSV file, and to automate drafting emails from your Thunderbird client (you will still need to hit send on each), using Python on a Windows machine.

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()

这篇关于Python/Thunderbird 中的字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆