python-各主流メール送信方式
6825 ワード
smtplib python2.7 python3.x , 。 , 。
:https://docs.python.org/3/library/smtplib.html?highlight=smtplib
, :
$ pip3 install smtplib
Collecting smtplib
ERROR: Could not find a version that satisfies the requirement smtplib (from versions: none)
ERROR: No matching distribution found for smtplib
使用163.com送信メール:
# !/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
# SMTP
mail_host = "smtp.163.com" # SMTP
mail_port = 465
mail_user = "[email protected]" #
mail_pass = "esefskjhurhsjeisjie" # , ( , " ")
sender = '[email protected]' # ( , )
receivers = [ '[email protected]' ] # , QQ
content = ' Python !'
title = 'Python ' #
def sendEmail():
message = MIMEText(content, 'plain', 'utf-8') # , ,
message['From'] = "{}".format(sender)
message['To'] = ",".join(receivers)
message['Subject'] = title
try:
smtpObj = smtplib.SMTP_SSL(mail_host, mail_port) # SSL , 465
smtpObj.login(mail_user, mail_pass) #
smtpObj.ehlo()
smtpObj.sendmail(sender, receivers, message.as_string()) #
smtpObj.close()
print("mail has been send successfully.")
except smtplib.SMTPException as e :
print(e)
if __name__ == '__main__':
sendEmail()
163.com :http://help.163.com/09/1224/17/5RAJ4LMH00753VB8.html
gmailを使用してメールを送信するには:
# !/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
# SMTP
mail_host = "smtp.gmail.com" # SMTP
mail_port = 465
mail_user = "[email protected]" #
mail_pass = "esefskjhurhsjeisjie" # , (goole 2 , " ")
sender = '[email protected]' # ( , )
receivers = [ '[email protected]' ] # , QQ
content = ' Python !'
title = 'Python ' #
def sendEmail():
message = MIMEText(content, 'plain', 'utf-8') # , ,
message['From'] = "{}".format(sender)
message['To'] = ",".join(receivers)
message['Subject'] = title
try:
smtpObj = smtplib.SMTP_SSL(mail_host, mail_port) # SSL , 465
smtpObj.login(mail_user, mail_pass) #
smtpObj.ehlo()
smtpObj.sendmail(sender, receivers, message.as_string()) #
smtpObj.close()
print("mail has been send successfully.")
except smtplib.SMTPException as e :
print(e)
if __name__ == '__main__':
sendEmail()
gmail :https://support.google.com/mail/?p=BadCredentials
QQメールボックスを使用してメールを送信します.
QQメールサーバー:smtp.qq.comは、テンセント企業のメールボックスであれば、そのサーバーはsmtpです.exmail.qq.com、方式は上の163に与える.comはgmailと同じです.
添付ファイル付きメールを送信:
# !/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
import email.mime.text
# SMTP
mail_host = "smtp.gmail.com" # SMTP
mail_port = 465
mail_user = "[email protected]" #
mail_pass = "eskeskefksekjsefsek" # , (goole 2 , " ")
sender = '[email protected]' # ( , )
receivers = [ '[email protected]' ] # , QQ
content = ' Python !'
title = 'Python ' #
def sendEmail():
message = MIMEMultipart()
message['From'] = "{}".format(sender)
message['To'] = ",".join(receivers)
message['Subject'] = title
#
txt = MIMEText(content, 'plain', 'utf-8') # , ,
message.attach(txt)
# , 。 , part_2,part_3 , part_2.add_header() msg.attach(part_2) 。
part = MIMEApplication(open('/Users/my-user/PycharmProjects/py-flask/templates/list.html', 'rb').read())
part.add_header('Content-Disposition', 'attachment', filename="list.html") # , , .
message.attach(part)
try:
smtpObj = smtplib.SMTP_SSL(mail_host, mail_port) # SSL , 465
smtpObj.login(mail_user, mail_pass) #
smtpObj.ehlo()
smtpObj.sendmail(sender, receivers, message.as_string()) #
smtpObj.close()
print("mail has been send successfully.")
except smtplib.SMTPException as e :
print(e)
if __name__ == '__main__':
sendEmail()
HTML形式のメールを送信:
基本的に上と同じですが、修正箇所は2つあります.
1.コンテンツはhtml形式のテキストであってもよい.
content='私はPythonで過程を勉強します!baidu.com '
*aのhrefはhttp/httpを削除する必要があります.QQメールボックスにプロトコルを持たないと、リンクが開きません.
2.メール本文、html形式を使用:MIMEText(content,'html','utf-8')
# !/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
# SMTP
mail_host = "smtp.gmail.com" # SMTP
mail_port = 465
mail_user = "[email protected]" #
mail_pass = "esefskjhurhsjeisjie" # , (goole 2 , " ")
sender = '[email protected]' # ( , )
receivers = [ '[email protected]' ] # , QQ
content = ' Python !baidu.com'
title = 'Python ' #
def sendEmail():
message = MIMEText(content, 'html', 'utf-8') # , ,
message['From'] = "{}".format(sender)
message['To'] = ",".join(receivers)
message['Subject'] = title
try:
smtpObj = smtplib.SMTP_SSL(mail_host, mail_port) # SSL , 465
smtpObj.login(mail_user, mail_pass) #
smtpObj.ehlo()
smtpObj.sendmail(sender, receivers, message.as_string()) #
smtpObj.close()
print("mail has been send successfully.")
except smtplib.SMTPException as e :
print(e)
if __name__ == '__main__':
sendEmail()
:
https://www.runoob.com/python/python-email.html
https://stackabuse.com/how-to-send-emails-with-gmail-using-python/ :
https://www.runoob.com/python/python-email.html