user1224398
user1224398

Reputation: 125

in python, how to send variable value by email

There is a variable x in python script, I wanna send the value of x by email. My code is

s=smtplib.SMTP('locaolhost')
s.sendmail(FROM, TO, "the answer is x")

But I always got the message the answer is x instead of x being the real value. How to solve this?

Upvotes: 0

Views: 16789

Answers (4)

mikaelb
mikaelb

Reputation: 2148

You can use string concatination here, as you can everywhere.

s.sendmail(FROM, TO, "the answer is " + x)

Or you can use the print format syntax:

s.sendmail(FROM, TO, "the answer is {}".format(x))

Read more: http://docs.python.org/tutorial/inputoutput.html#fancier-output-formatting

Upvotes: 5

user210870
user210870

Reputation:

s.sendmail(FROM, TO, "the answer is " + str(x))

You first convert the value of x into string by str(x), then append str(x) to the end of the string "the answer is " by +.

Upvotes: 4

Pradeep Nayak
Pradeep Nayak

Reputation: 675

Your sendmail line should be like this:

s.sendmail(FROM, TO, "The answer is "+x)

Upvotes: 1

Sandro Munda
Sandro Munda

Reputation: 41060

s=smtplib.SMTP('localhost')
s.sendmail(FROM, TO, "the answer is %s" % x) # here is the change

You forgot the %s formatter in your string !

So :

x = 'hello world'
s.sendmail(FROM, TO, "the answer is x")

Output: the answer is x

And :

x = 'hello world'
s.sendmail(FROM, TO, "the answer is %s" % x)

Output: the answer is hello world

Upvotes: 1

Related Questions