Send mail using Python's smtplib and email

Source: Internet
Author: User

Principle

There are a lot of tutorials on the Internet to explain the principle of sending mail, here is still recommended Liaoche teacher's Python tutorial, explain the easy to understand. Briefly, SMTP is the protocol that sends messages, and Python's built-in support for SMTP can send plain text messages, HTML messages, and messages with attachments. Python's built-in email module is responsible for the content of the message, sender, receiver, and so on, the specific operation can see the code.

The construction of a mail object is a Messag object, if a Mimetext object is constructed, it represents a text message object, if a Mimeimage object is constructed, it represents a picture as an attachment, in order to combine multiple objects together, the Mimemultipart object , and mimebase can represent any object. Their nesting relationship is as follows:

Message+- MIMEBase   +- MIMEMultipart   +- MIMENonMultipart      +- MIMEMessage      +- MIMEText      +- MIMEImage
A summary of some mistakes

[1] Hint smtplib. Smtpauthenticationerror: (550, B ' User has no permission ')
This is because the mailbox does not open the client authorization, the mailbox side of the SMTP service can not run, and now basically all the mail is required to authorize the client, it is important to note here. The solution is: Enter 163 mailbox-set-Client authorization password-on (authorization code is used to log in to the third-party mail client's private password), non-third-party login password is not changed.
[2] hint smtplib. Smtpauthenticationerror: (535, B ' error:authentication failed ')
Take 163 mailbox As an example, the authorization code will be set when the POP3/SMTP service is turned on and the client authorization password is turned on, instead of Smtplib. Password in the SMTP (). Login (User,password) method. This means that the password in your code is the authorization code you set.
[3] Tip 554
Note The message content is missing information, in the current mailbox in general, some content needs to be filled out.

Code
# Send Text#-*-Coding:utf-8-*- fromEmailImportEncoders fromEmail.headerImportHeader fromEmail.mime.textImportMimetext fromEmail.utilsImportPARSEADDR, FORMATADDRImportSmtplibdef_FORMAT_ADDR (s): Name, addr=PARSEADDR (s)returnFormataddr (Header (name,' Utf-8 '). Encode (), addr) from_addr= input(' from: ') password= input(' Password: ') to_addr= input(' to: ') Smtp_server= input(' SMTP server: ')# Content of the hairMsg=Mimetext (' Hello, send by Python ... ',' Plain ',' Utf-8 ')# Sendermsg[' from ']=_FORMAT_ADDR (u ' python enthusiasts <%s> ' %FROM_ADDR)# Recipientmsg[' to ']=_FORMAT_ADDR (u ' Admin <%s> ' %TO_ADDR)# titlemsg[' Subject ']=Header (u ' Greetings from SMTP ... ',' Utf-8 '). Encode () server=Smtplib. SMTP (Smtp_server, -) Server.set_debuglevel (1) Server.login (from_addr, password) server.sendmail (from_addr, [to_addr], msg.as_string ()) Server.quit ()
# Send Attachments#-*-Coding:utf-8-*- fromEmailImportEncoders fromEmail.headerImportHeader fromEmail.mime.multipartImportMimemultipart fromEmail.mime.textImportMimetext fromEmail.mime.baseImportMimebase fromEmail.utilsImportPARSEADDR, FORMATADDRImportSmtplibdef_FORMAT_ADDR (s): Name, addr=PARSEADDR (s)returnFormataddr (Header (name,' Utf-8 '). Encode (), addr) from_addr= input(' from: ') password= input(' Password: ') to_addr= input(' to: ') Smtp_server= input(' SMTP server: ')# Mail object:Msg=Mimemultipart () msg[' from ']=_FORMAT_ADDR (u ' python enthusiasts <%s> ' %FROM_ADDR) msg[' to ']=_FORMAT_ADDR (u ' Admin <%s> ' %TO_ADDR) msg[' Subject ']=Header (u ' Greetings from SMTP ... ',' Utf-8 '). Encode ()# The message body is Mimetext:Msg.attach (Mimetext (' send with file ... ',' Plain ',' Utf-8 '))# Adding an attachment is to add a mimebase that reads a picture from the Local: with Open(' e:/141.m4a ',' RB ') asF:# Set the MIME and file name of the attachment, here is the music type:Mime=Mimebase (' image ',' M4A ', filename=' 141.m4a ')# Add the necessary header information:Mime.add_header (' Content-disposition ',' attachment ', filename=' 141.m4a ') Mime.add_header (' Content-id ',' <0> ') Mime.add_header (' X-attachment-id ',' 0 ')# Read the contents of the attachment in:Mime.set_payload (F.read ())# Encoded with Base64:Encoders.encode_base64 (MIME)# added to Mimemultipart:Msg.attach (MIME) server=Smtplib. SMTP (Smtp_server, -) Server.set_debuglevel (1) Server.login (from_addr, password) server.sendmail (from_addr, [to_addr], msg.as_string ()) Server.quit ()

The code has been tested by the author, it should be no problem. Encounter errors to see if there is a problem with the client's authorization, or the version of Python.

Send mail using Python's smtplib and email

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.