Scott Vinzant
Scott Vinzant

Reputation: 23

Send an email in Python and include a vaiable in the subject line

I am trying to include a variable in the subject line of a Python email script to send a message to my phone. When I run the program, the message has "The Temp is: CTemp", but I want "The Temp is: 12.34" (or whatever the variable is set to at that time). How do I insert the variable CTemp into the subject line?

import smtplib
from email.mime.text import MIMEText

username = "****@****.com"
password = "****"
vtext = "**********@****.com"

CTemp = 12.34 #set for testing

msg = MIMEText
msg = MIMEText("""The Temp is: CTemp""")

server = smtplib.SMTP('****.****.net',25)
server.login(username,password)
server.sendmail(username, vtext, msg.as_string())
server.quit()

Upvotes: 1

Views: 1475

Answers (1)

user5457708
user5457708

Reputation:

You should concatenate the 'Text' string to a variable

CTemp = str(12.34) #set for testing
text = "The temp is: "+CTemp

msg = MIMEText(text)

http://www.pythonforbeginners.com/concatenation/string-concatenation-and-formatting-in-python

Upvotes: 1

Related Questions