一、SMTP郵件服務器介紹
SMTP(Simple Mail Transfer Protocol)是互聯網上的郵件傳輸協議,SMTP郵件服務器是負責發送和接收電子郵件的服務器。在發送郵件時,需要使用SMTP郵件服務器進行身份驗證,確保要發送的郵件不是垃圾郵件。SMTP郵件服務器通常需要通過用戶名和密碼進行身份驗證,並提供基於SSL或TLS的加密通信功能。在這個前提下,我們才能夠利用Python實現SMTP郵件服務器發送郵件。
二、Python SMTP庫介紹
Python SMTP庫是Python標準庫中提供的郵件發送庫,可以方便地使用SMTP郵件服務器發送郵件。Python SMTP庫支持常見的郵件發送需求,包括添加收件人、添加附件、設置郵件主題和正文等。
import smtplib
from email.mime.text import MIMEText
mail_host = 'smtp.example.com'
mail_user = 'your_username'
mail_pass = 'your_password'
sender = 'your_sender_address'
receiver = 'your_receiver_address'
subject = 'your_subject'
content = 'your_content'
msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receiver, msg.as_string())
smtpObj.quit()
print('郵件發送成功')
except Exception as e:
print(e)
print('郵件發送失敗')
三、Python SMTP庫使用示例
以下示例代碼展示了如何使用Python SMTP庫實現發送一份郵件。在該郵件中,我們向收件人發送一份包含主題和正文的簡單郵件。
import smtplib
from email.mime.text import MIMEText
mail_host = 'smtp.example.com'
mail_user = 'your_username'
mail_pass = 'your_password'
sender = 'your_sender_address'
receiver = 'your_receiver_address'
subject = 'your_subject'
content = 'your_content'
msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receiver, msg.as_string())
smtpObj.quit()
print('郵件發送成功')
except Exception as e:
print(e)
print('郵件發送失敗')
四、Python SMTP庫實現添加附件
除了發送純文本郵件外,我們還可以使用Python SMTP庫實現添加附件的功能。下面的示例展示了如何實現添加附件的操作。
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
mail_host = 'smtp.example.com'
mail_user = 'your_username'
mail_pass = 'your_password'
sender = 'your_sender_address'
receiver = 'your_receiver_address'
subject = 'your_subject'
content = 'your_content'
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver
attachment_path = '/path/to/your/attachment'
with open(attachment_path, 'rb') as fp:
attachment = MIMEApplication(fp.read())
attachment.add_header('Content-Disposition', 'attachment', filename='your_attachment_filename')
msg.attach(attachment)
text = MIMEText(content)
msg.attach(text)
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receiver, msg.as_string())
smtpObj.quit()
print('郵件發送成功')
except Exception as e:
print(e)
print('郵件發送失敗')
五、Python SMTP庫實現HTML郵件
Python SMTP庫也支持發送HTML郵件,下面是一個使用Python SMTP庫實現發送HTML郵件的簡單示例。
import smtplib
from email.mime.text import MIMEText
mail_host = 'smtp.example.com'
mail_user = 'your_username'
mail_pass = 'your_password'
sender = 'your_sender_address'
receiver = 'your_receiver_address'
subject = 'your_subject'
content = 'your_html_content
'
msg = MIMEText(content, 'html')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receiver, msg.as_string())
smtpObj.quit()
print('郵件發送成功')
except Exception as e:
print(e)
print('郵件發送失敗')
六、Python SMTP庫實現發送帶有圖片的HTML郵件
除了發送HTML郵件外,我們還可以在HTML郵件中添加圖片。下面的示例展示了如何實現發送帶有圖片的HTML郵件。
import smtplib
import os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.image import MIMEImage
from urllib.request import urlopen
mail_host = 'smtp.example.com'
mail_user = 'your_username'
mail_pass = 'your_password'
sender = 'your_sender_address'
receiver = 'your_receiver_address'
subject = 'your_subject'
content = ''
msg = MIMEMultipart('related')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver
image_url = 'https://your_image_url.jpg'
image_data = urlopen(image_url).read()
image = MIMEImage(image_data)
image.add_header('Content-ID', '')
msg.attach(image)
text = MIMEText(content, 'html')
msg.attach(text)
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receiver, msg.as_string())
smtpObj.quit()
print('郵件發送成功')
except Exception as e:
print(e)
print('郵件發送失敗')
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/272259.html