I am able to send email when the message is a string directly typed into the function, but not when it is a variable.
This code works:
import smtplib
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login("something@gmail.com", "somepassword")
server.sendmail(
"something@gmail.com",
"somethingelse@gmail.com",
"a manually typed string like this")
server.quit()
But this code, with a variable string, doesn't:
import smtplib
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login("something@gmail.com", "somepassword")
someVariable = "any string"
server.sendmail(
"something@gmail.com",
"somethingelse@gmail.com",
someVariable)
server.quit()
More exactly, this second version does send an email but with an empty body. No characters show up.
How can I make the second version work?
print(someVariable)
and print(type(someVariable))
give the right (expected) outputs.
It turns out this worked, inspired by [these docs][1]
and by rogersdevop's earlier answer (which didn't work for me):
def sendEmail(msge):
import smtplib
from email.mime.text import MIMEText
msg = MIMEText(msge)
me = 'something@gmail.com'
you = 'somethingelse@gmail.com'
msg['Subject'] = 'The subject line'
msg['From'] = me
msg['To'] = you
s = smtplib.SMTP_SSL('smtp.gmail.com', 465)
s.login("something@gmail.com", "somepassword")
s.send_message(msg)
s.quit()