Python learning notes (SMTP mail) and python learning notes

Source: Internet
Author: User

Python learning notes (SMTP mail) and python learning notes

I want to add the mail sending function to the framework, so I want to sort out the mail sending function in python.

First, python supports sending mails, built-in smtp libraries, and Sending plain text, HTML, and adding attachments.

The SMTP service is disabled by default in emails such as 163, qq, and Sina.

After opening, it is sent through the sender's email address and Authorization password through the sender's SMTP Service

The Code is as follows:

1 #! /Usr/bin/env python 2 #-*-coding: utf_8-*-3 4 from email. mime. text import MIMEText 5 from email. mime. multipart import MIMEMultipart 6 from email. mime. multipart import MIMEBase 7 from email import encoders 8 from email. header import Header 9 from email. utils import parseaddr, formataddr10 import smtplib11 12 13 class SendEmail: 14 outbox = "pythondldysl01@163.com" 15 # sender address 16 password = "wxqcl258258" 17 # Authorization password not mailbox login password 18 inbox = "475680835@qq.com" 19 # inbox address 20 smtp_server = "smtp.163.com" 21 # sender server address 22 23 def _ init _ (self): 24 pass25 26 @ classmethod27 def _ format_address (cls, text): 28 name, address = parseaddr (text) 29 return formataddr (Header (name, "UTF-8 "). encode (), address) 30 31 @ classmethod32 def send_email_text (cls): 33 msg = MIMEText ("test smtp mail sending function", "plain", "UTF-8 ") 34 # first parameter: Mail body 35 # second parameter: Mail type plain text 36 # third parameter: encoding 37 38 msg ["From"] = SendEmail. _ format_address ("an email from 163 <% s>" % SendEmail. outbox) 39 # sender's name and sender address 40 msg ["To"] = SendEmail. _ format_address ("Administrator <% s>" % SendEmail. inbox) 41 # Recipient Name and inbox address 42 msg ["Subject"] = Header ("Greetings from SMTP", "UTF-8 "). encode () 43 # mail title 44 45 try: 46 server = smtplib. SMTP (SendEmail. smtp_server, 25) 47 # construct an smtp server connection 48 # server. set_debuglevel (1) 49 # by default, 50 servers are disabled in debug output mode. login (SendEmail. outbox, SendEmail. password) 51 # log on to the smtp server 52 server. sendmail (SendEmail. outbox, [SendEmail. inbox], msg. as_string () 53 # Send email 54 server. quit () 55 print "mail sent successfully" 56 failed t Exception, e: 57 print str (e) 58 print "failed to send email" 59 60 if _ name _ = '_ main _': 61 SendEmail. send_email_text ()

 

Sending effect:

Inbox Effect

This is only plain text content, HTML-format content supported, and the modified content is as follows:

Msg = MIMEText ("test smtp mail sending function", "plain", "UTF-8 ")

Modify the content to HTML format, and change "plain" to "html"

Finally, add the attachment email.

The Code is as follows:

1 @ classmethod 2 def send_email_multipart (cls): 3 msg = MIMEMultipart () 4 5 msg ["From"] = SendEmail. _ format_address ("an email from 163 <% s>" % SendEmail. outbox) 6 # sender name and sender address 7 msg ["To"] = SendEmail. _ format_address ("Administrator <% s>" % SendEmail. inbox) 8 # Recipient Name and inbox address 9 msg ["Subject"] = Header ("Greetings from SMTP", "UTF-8 "). encode () 10 # mail Title 11 12 msg. attach (MIMEText ("test the smtp mail sending function for adding attachments", "plain", "UTF-8") 13 14 with open ("E: \ work \ python project \ CreateProject \ 20160421140953.xml", "rb") as f: 15 # Set the MIME of the attachment and the file name 16 mime = MIMEBase ("xml ", "xml", filename = "test report. xml ") 17 # Add the necessary header information 18 mime. add_header ('content-disposition', 'attachment', filename = "test report. xml ") 19 mime. add_header ('content-id', '<0>') 20 mime. add_header ('x-Attachment-id', '0') 21 # Read the content of the Attachment: 22 mime. set_payload (f. read () 23 # Base64 encoding: 24 encoders. encode_base64 (mime) 25 # Add to MIMEMultipart: 26 msg. attach (mime) 27 28 try: 29 server = smtplib. SMTP (SendEmail. smtp_server, 25) 30 # construct an smtp server connection 31 # server. set_debuglevel (1) 32 #33 server is disabled by default in debug output mode. login (SendEmail. outbox, SendEmail. password) 34 # log on to the smtp server 35 server. sendmail (SendEmail. outbox, [SendEmail. inbox], msg. as_string () 36 # Send email 37 server. quit () 38 print "mail sent successfully" 39 failed t Exception, e: 40 print str (e) 41 print "mail failed to be sent"

 

Inbox effect:

The blogger is also a comprehensive python Learning Website Based on the website of great god.

Here is the link address:

Liao Xuefeng python learning address

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.