在 Python 中将主题 header 添加到 server.sendmail()
Add Subject header to server.sendmail() in Python
我正在编写一个 python 脚本来从终端发送电子邮件。在我目前发送的邮件中,它没有主题。我们如何为这封电子邮件添加主题?
我当前的代码:
import smtplib
msg = """From: hello@hello.com
To: hi@hi.com\n
Here's my message!\nIt is lovely!
"""
server = smtplib.SMTP_SSL('smtp.example.com', port=465)
server.set_debuglevel(1)
server.ehlo
server.login('examplelogin', 'examplepassword')
server.sendmail('me@me.com', ['anyone@anyone.com '], msg)
server.quit()
您需要将 subject
放在消息的 header 中。
例子-
import smtplib
msg = """From: hello@hello.com
To: hi@hi.com\n
Subject: <Subject goes here>\n
Here's my message!\nIt is lovely!
"""
server = smtplib.SMTP_SSL('smtp.example.com', port=465)
server.set_debuglevel(1)
server.ehlo
server.login('examplelogin', 'examplepassword')
server.sendmail('me@me.com', ['anyone@anyone.com '], msg)
server.quit()
的确,你错过的主题。使用一些 API(例如 yagmail)而不是 headers 风格可以很容易地避免这些事情。
我相信 yagmail(免责声明:我是维护者)可以对您有很大帮助,因为它提供了一个简单的 API.
import yagmail
yag = yagmail.SMTP('hello@gmail.com', 'yourpassword')
yag.send(to = 'hi@hi.com', subject ='Your subject', contents = 'Some message text')
确实只有三行。
使用 pip install yagmail
或 pip3 install yagmail
安装 Python 3.
更多信息请见 github。
import smtp
def send_email(SENDER_EMAIL,PASSWORD,RECEIVER_MAIL,SUBJECT,MESSAGE):
try:
server = smtplib.SMTP("smtp.gmail.com",587)
#specify server and port as per your requirement
server.starttls()
server.login(SENDER_EMAIL,PASSWORD)
message = """From: %s\nTo: %s\nSubject: %s\n\n%s""" % (SENDER_EMAIL, ", ".join(TO), SUBJECT, MESSAGE)
server.sendmail(SENDER_EMAIL,TO,message)
server.quit()
print 'successfully sent the mail'
except:
print "failed to send mail"
send_email("sender@gmail.com","Password","receiver@gmail.com","SUBJECT","MESSAGE")
您可以简单地使用 MIMEMultipart()
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
msg = MIMEMultipart()
msg['From'] = 'EMAIL_USER'
msg['To'] = 'EMAIL_TO_SEND'
msg['Subject'] = 'SUBJECT'
body = 'YOUR TEXT'
msg.attach(MIMEText(body,'plain'))
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login('email','password')
server.sendmail(email_user,email_send,text)
server.quit()
希望这有用!!!
我正在编写一个 python 脚本来从终端发送电子邮件。在我目前发送的邮件中,它没有主题。我们如何为这封电子邮件添加主题?
我当前的代码:
import smtplib
msg = """From: hello@hello.com
To: hi@hi.com\n
Here's my message!\nIt is lovely!
"""
server = smtplib.SMTP_SSL('smtp.example.com', port=465)
server.set_debuglevel(1)
server.ehlo
server.login('examplelogin', 'examplepassword')
server.sendmail('me@me.com', ['anyone@anyone.com '], msg)
server.quit()
您需要将 subject
放在消息的 header 中。
例子-
import smtplib
msg = """From: hello@hello.com
To: hi@hi.com\n
Subject: <Subject goes here>\n
Here's my message!\nIt is lovely!
"""
server = smtplib.SMTP_SSL('smtp.example.com', port=465)
server.set_debuglevel(1)
server.ehlo
server.login('examplelogin', 'examplepassword')
server.sendmail('me@me.com', ['anyone@anyone.com '], msg)
server.quit()
的确,你错过的主题。使用一些 API(例如 yagmail)而不是 headers 风格可以很容易地避免这些事情。
我相信 yagmail(免责声明:我是维护者)可以对您有很大帮助,因为它提供了一个简单的 API.
import yagmail
yag = yagmail.SMTP('hello@gmail.com', 'yourpassword')
yag.send(to = 'hi@hi.com', subject ='Your subject', contents = 'Some message text')
确实只有三行。
使用 pip install yagmail
或 pip3 install yagmail
安装 Python 3.
更多信息请见 github。
import smtp
def send_email(SENDER_EMAIL,PASSWORD,RECEIVER_MAIL,SUBJECT,MESSAGE):
try:
server = smtplib.SMTP("smtp.gmail.com",587)
#specify server and port as per your requirement
server.starttls()
server.login(SENDER_EMAIL,PASSWORD)
message = """From: %s\nTo: %s\nSubject: %s\n\n%s""" % (SENDER_EMAIL, ", ".join(TO), SUBJECT, MESSAGE)
server.sendmail(SENDER_EMAIL,TO,message)
server.quit()
print 'successfully sent the mail'
except:
print "failed to send mail"
send_email("sender@gmail.com","Password","receiver@gmail.com","SUBJECT","MESSAGE")
您可以简单地使用 MIMEMultipart()
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
msg = MIMEMultipart()
msg['From'] = 'EMAIL_USER'
msg['To'] = 'EMAIL_TO_SEND'
msg['Subject'] = 'SUBJECT'
body = 'YOUR TEXT'
msg.attach(MIMEText(body,'plain'))
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login('email','password')
server.sendmail(email_user,email_send,text)
server.quit()
希望这有用!!!