问题
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.
However, I instead end up with:
Dear Bob (comma missing, and rest of body missing)
I have tried the following:
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.
回答1:
From this example, you may need to surround the arguments with single and double quotes.
Like this:
composeCommand = '"format=html,to=\'{}\',subject=\'{}\',body=\'{}\'"'.format(to, subject, body)
By the way, if you are using python 3.6+, using f-strings makes str more readable:
body = f'<html><body>Dear {name}, This is the body <br></body></html>'
composeCommand = f'"format=html,to=\'{to}\',subject=\'{subject}\',body=\'{body}\'"'
回答2:
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()
来源:https://stackoverflow.com/questions/62502776/string-formatting-in-python-thunderbird