填充其他电子邮件字段,例如 Python 邮件功能中的主题
Populate other email fields like Subject in Python mail function
我已经测试了这段代码,它成功发送了电子邮件,它倾向于将主题字段、抄送和密件抄送字段留空,如图所示。
import smtplib
gmail_user = 'dummy@gmail.com'
gmail_password = 'password'
sent_from = 'dummy@gmail.com'
to = ['receiver@gmail.com']
subject = 'OMG Super Important Message'
body = "Hey, what's up?\n\n- You"
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(to), subject, body)
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
print("establish connection successful")
server.ehlo()
print("identification successful")
server.login(gmail_user, gmail_password)
print("login successful")
server.sendmail(sent_from, to, email_text)
print("send mail successful")
server.close()
print('Email sent!')
except:
print('Something went wrong...')
有谁知道我如何通过这个脚本来填充它们?
对于电子邮件主题 - 您提供给 server.sendmail 的输入参数有一种特定的格式应该有效。你能试试吗:
subject = 'This is the email subject'
text = 'This is the email body'
message = "Subject: {}\n\n{}".format(subject, text)
server.sendmail(sender, recipient, message)
如果您尝试打开电子邮件的来源,您将看到如下内容:
Received: from ************.net ([***.**.2.17]) by
*****.****.net ([**.***.224.162]) with mapi id **.03.****.***;
Mon, 22 May 2017 09:14:59 +0200
From: *FROMEMAIL* <******@***.com>
To: *TOEMAIL* <********@***.com>
CC: *CCEMAIL* <********@*****.com>
Subject: E-mail - 150633**0686_****.pdf
...
那是电子邮件的 header,所以如果您尝试这样的操作:
import smtplib
gmail_user = 'dummy@gmail.com'
gmail_password = 'password'
sent_from = 'dummy@gmail.com'
to = ['receiver@gmail.com']
subject = 'OMG Super Important Message'
body = "Hey, what's up?\n\n- You"
cc = "****@***.com"
bcc = "******@*****.com"
email_text = """\
From: %s
To: %s
CC: %s
BCC: %s
Subject: %s
%s
""" % (sent_from, ", ".join(to), cc, bcc,subject, body)
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
print("establish connection successful")
server.ehlo()
print("identification successful")
server.login(gmail_user, gmail_password)
print("login successful")
server.sendmail(sent_from, to, email_text)
print("send mail successful")
server.close()
print('Email sent!')
except:
print('Something went wrong...')
我认为它会起作用
我已经测试了这段代码,它成功发送了电子邮件,它倾向于将主题字段、抄送和密件抄送字段留空,如图所示。
import smtplib
gmail_user = 'dummy@gmail.com'
gmail_password = 'password'
sent_from = 'dummy@gmail.com'
to = ['receiver@gmail.com']
subject = 'OMG Super Important Message'
body = "Hey, what's up?\n\n- You"
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(to), subject, body)
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
print("establish connection successful")
server.ehlo()
print("identification successful")
server.login(gmail_user, gmail_password)
print("login successful")
server.sendmail(sent_from, to, email_text)
print("send mail successful")
server.close()
print('Email sent!')
except:
print('Something went wrong...')
有谁知道我如何通过这个脚本来填充它们?
对于电子邮件主题 - 您提供给 server.sendmail 的输入参数有一种特定的格式应该有效。你能试试吗:
subject = 'This is the email subject'
text = 'This is the email body'
message = "Subject: {}\n\n{}".format(subject, text)
server.sendmail(sender, recipient, message)
如果您尝试打开电子邮件的来源,您将看到如下内容:
Received: from ************.net ([***.**.2.17]) by
*****.****.net ([**.***.224.162]) with mapi id **.03.****.***;
Mon, 22 May 2017 09:14:59 +0200
From: *FROMEMAIL* <******@***.com>
To: *TOEMAIL* <********@***.com>
CC: *CCEMAIL* <********@*****.com>
Subject: E-mail - 150633**0686_****.pdf
...
那是电子邮件的 header,所以如果您尝试这样的操作:
import smtplib
gmail_user = 'dummy@gmail.com'
gmail_password = 'password'
sent_from = 'dummy@gmail.com'
to = ['receiver@gmail.com']
subject = 'OMG Super Important Message'
body = "Hey, what's up?\n\n- You"
cc = "****@***.com"
bcc = "******@*****.com"
email_text = """\
From: %s
To: %s
CC: %s
BCC: %s
Subject: %s
%s
""" % (sent_from, ", ".join(to), cc, bcc,subject, body)
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
print("establish connection successful")
server.ehlo()
print("identification successful")
server.login(gmail_user, gmail_password)
print("login successful")
server.sendmail(sent_from, to, email_text)
print("send mail successful")
server.close()
print('Email sent!')
except:
print('Something went wrong...')
我认为它会起作用