如何在 python3 中使用 smtplib 通过电子邮件发送变量值?
How to send variable value via email with smtplib in python3?
当消息是直接输入函数的字符串时,我可以发送电子邮件,但当它是变量时则不能。
此代码有效:
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()
但是这段带有可变字符串的代码不会:
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()
更确切地说,第二个版本确实发送了一封电子邮件,但正文为空。没有字符出现。
我怎样才能使第二个版本工作?
print(someVariable)
和 print(type(someVariable))
给出正确的(预期的)输出。
你可以试试
import smtplib
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login("something@gmail.com", "somepassword")
server.sendmail(
"something@gmail.com",
"somethingelse@gmail.com",
"{}".format(someVariable))
server.quit()
我想你只需要将变量格式化为字符串
我对 Office365 的个人体验使我想到了这个解决方案:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart()
msg['Subject'] = 'confirmation email'
msg['From'] = 'my.address@email.net'
msg['To'] = ", ".join(['your.address@email.net','another.address@email.net'])
body = 'Example email text here.'
msg.attach(MIMEText(body, 'html')) #set to whatever text format is preferred
然后是它如何适合您当前脚本的最后一部分
server.sendmail('my.address@email.net','your.address@email.net',msg.as_string())
事实证明这是有效的,灵感来自 [these docs][1]
和 rogersdevop 的早期回答(对我不起作用):
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()
当消息是直接输入函数的字符串时,我可以发送电子邮件,但当它是变量时则不能。
此代码有效:
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()
但是这段带有可变字符串的代码不会:
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()
更确切地说,第二个版本确实发送了一封电子邮件,但正文为空。没有字符出现。
我怎样才能使第二个版本工作?
print(someVariable)
和 print(type(someVariable))
给出正确的(预期的)输出。
你可以试试
import smtplib
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login("something@gmail.com", "somepassword")
server.sendmail(
"something@gmail.com",
"somethingelse@gmail.com",
"{}".format(someVariable))
server.quit()
我想你只需要将变量格式化为字符串
我对 Office365 的个人体验使我想到了这个解决方案:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart()
msg['Subject'] = 'confirmation email'
msg['From'] = 'my.address@email.net'
msg['To'] = ", ".join(['your.address@email.net','another.address@email.net'])
body = 'Example email text here.'
msg.attach(MIMEText(body, 'html')) #set to whatever text format is preferred
然后是它如何适合您当前脚本的最后一部分
server.sendmail('my.address@email.net','your.address@email.net',msg.as_string())
事实证明这是有效的,灵感来自 [these docs][1]
和 rogersdevop 的早期回答(对我不起作用):
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()