電子郵件已成為現代生活和工作中必不可缺的一部分。作為一個Python工程師,你需要知道如何使用Python SMTP程序來發送電子郵件,因為這將對你的工作非常有用。
一、SMTP協議和Python中SMTP模塊的使用
SMTP(簡單郵件傳輸協議)是一種標準化的互聯網電子郵件傳輸協議。使用SMTP協議可以將郵件從一個郵件服務器傳輸到另一個郵件服務器。Python中內置SMTP模塊,可以輕鬆地使用Python編寫SMTP程序來發送電子郵件。以下是一個使用Python SMTP模塊發送電子郵件的示例代碼:
import smtplib sender_email = "sender@example.com" receiver_email = "receiver@example.com" password = input("Type your password and press enter: ") message = """\ Subject: Hi there! This is a test email. This is a test email sent using Python SMTP module.""" context = ssl.create_default_context() with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: server.login(sender_email, password) server.sendmail(sender_email, receiver_email, message) print("Email sent successfully!")
在此示例中,我們使用SMTP_SSL函數將連接安全地升級到SSL。我們還使用了一個標準的文本消息,其中指定了郵件主題和郵件正文。最後,我們調用sendmail函數來發送郵件。
二、郵件頭和郵件尾的添加
電子郵件通常由郵件頭、郵件正文和郵件尾組成。發電子郵件時通常需要添加自定義的郵件頭和郵件尾,來表明電子郵件的發送者和接收者以及附加信息。以下是一個示例Python代碼,演示了如何添加郵件頭和郵件尾:
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication #設置郵件頭 msg = MIMEMultipart() msg['From'] = 'sender@example.com' msg['To'] = 'receiver@example.com' msg['Subject'] = 'Testing' #設置郵件正文 body = 'This is a test email.' msg.attach(MIMEText(body,'plain')) #添加郵件尾 filename = 'document.pdf' with open(filename,"rb") as f: attachment = MIMEApplication(f.read(),_subtype="pdf") attachment.add_header('Content-Disposition','attachment',filename=filename) msg.attach(attachment) #發送郵件 context = ssl.create_default_context() with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: server.login(sender_email, password) server.sendmail(sender_email, receiver_email, msg.as_string())
在這個示例代碼中,我們使用Python中的email模塊來添加郵件頭和郵件尾。我們使用MIMEApplication添加了一個PDF文件,然後將其添加為郵件附件。郵件頭和郵件尾可以從msg中添加,使用‘附加’方法添加。
三、處理HTML文本和圖片
在現代電子郵件中,HTML文本和圖片已成為電子郵件中不可或缺的內容。Python SMTP程序可以輕鬆地處理HTML文本和圖片。以下是一個演示如何添加HTML文本和圖片的Python SMTP程序示例:
import smtplib import ssl from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage #設置郵件頭 msg = MIMEMultipart() msg['From'] = 'sender@example.com' msg['To'] = 'receiver@example.com' msg['Subject'] = 'Testing HTML and Images' #添加HTML文本和圖片 html = """Python SMTP程序可以輕鬆地處理HTML文本和圖片。
以下是一個演示如何添加HTML文本和圖片的Python SMTP程序示例:
""" msg.attach(MIMEText(html,'html')) with open('image.jpg', 'rb') as f: img_data = f.read() # 添加圖片 image = MIMEImage(img_data, name='image1') image.add_header('Content-ID', '') msg.attach(image) #發送郵件 context = ssl.create_default_context() with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: server.login(sender_email, password) server.sendmail(sender_email, receiver_email, msg.as_string())
在此示例中,我們添加了一個HTML文本,在文本中添加了一張嵌入的圖片。我們使用MIMEImage來添加圖片,並使用Content-ID標頭指定圖片的ID,然後我們在HTML文本中使用該ID來引用這張嵌入的圖片。
四、結論
在Python中使用SMTP協議發送電子郵件是非常簡單和容易的。Python SMTP程序可以輕鬆處理郵件頭、郵件尾、HTML文本和圖片,為我們的電子郵件添加更多自定義內容和風格。
以上就是Python SMTP程序的一些技巧和竅門。在實際工作中,我們可以將其與其他Python工具和庫相結合,比如定時任務、數據分析等,進一步提高工作效率。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/270404.html