Print Contents of Variables to body of email python 3.7

1.9k Views Asked by At

trying to send email through python. Can get it to send email with correct text content in the body but it's printing the the actual variable name "New Companies" instead of it's content. here's the code and the resulting email.

(not sure why the html tags aren't showing up in my code below, but I used html and body tags before and after the email content)

Any and all help is appreciated.

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

EMAIL_SERVER = 'blahblah'
EMAIL_PORT = 25

f = open('diff.txt', 'r')
NEW_COMPANIES  = f.read()


EMAIL_FROM = 'blabal'
RECIPIENT_LIST = ['blahblah']

msg = MIMEMultipart()
msg['From'] = EMAIL_FROM
msg['To'] = ", ".join(RECIPIENT_LIST)
msg['Subject'] = 'North Carolina New Business Registration'


body = '''<html>
<body>

New Business Names:<br><br>
body.format('NEW_COMPANIES')
<br><br>
Affiliate: r2thek
<br><br>
Website Link: 
https://www.sosnc.gov/online_services/search/by_title/_Business_Registration
</body>
</html>'''

body.format('NEW_COMPANIES')

msg.attach(MIMEText(body, 'html'))


smtpserver = smtplib.SMTP(EMAIL_SERVER, EMAIL_PORT)
smtpserver.sendmail(EMAIL_FROM, RECIPIENT_LIST, msg.as_string())
smtpserver.quit()

Email Result:

New Business Names:

{NEW_COMPANIES}

Affiliate: r2thek

Website Link: https://www.sosnc.gov/online_services/search/by_title/_Business_Registration

1

There are 1 best solutions below

5
On BEST ANSWER

It doesn't look like you've passed any variables to format the string, you'll need to do

body.format(variable1, variable2, etc)

In your case I think it's just the one variable, so wherever you stored what NEW_COMPANIES should be needs to be used as the argument for str.format()