Python-based SMTP mail sending tutorial

Source: Internet
Author: User
This article mainly introduces a detailed tutorial on how to implement SMTP mail sending in Python, with the SMTP mail sending code. if you are interested, refer Introduction

I searched for a lot of Python mail sending tutorials during Website Search, but I talked about a lot of principles and then launched the implementation code, I failed to send emails using the code I provided. I found the cause for a long time later. this is due to the absence of a detailed environment debugging. so I am going to issue a detailed tutorial today, one step from environment debugging to code implementation is a complete tutorial, hoping to help those who are still struggling to find a solution but are still unable to get an effective solution.

SMTP protocol

First, understand SMTP (Simple Mail Transfer Protocol). The mail transfer agent uses the SMTP protocol to send an email to the recipient's mail server. The SMTP protocol can only be used to send mails, but not to receive mails. most mail sending servers use the SMTP protocol. The default TCP port number of SMTP protocol is 25.

Environment debugging

As mentioned above, the SMTP protocol is used to send emails. Therefore, you must first check whether the SMTP protocol is enabled in your sender's mailbox. if you do not need to enable the SMTP protocol, I tested the use of 163.com as the sender's mailbox and enabled the SMTP protocol, as shown in.

1. log on to the 163.com mailbox (NetEase mailbox), as shown in figure

# Coding: UTF-8 # forcibly use the UTF-8 encoding format import smtplib # load the smtplib module from email. mime. text import MIMETextfrom email. utils import formataddrmy_sender = 'sender's email account' # sender's email account. to facilitate subsequent maintenance, the variable my_user = 'recipient's email account '# recipient's email account is written to facilitate subsequent maintenance, therefore, the variable def mail (): ret = True try: msg = MIMEText ('fill in the mail content', 'plain ', 'utf-8') is written ') msg ['from'] = formataddr (["sender's email nickname", my_sender]) # corresponding sender's email nickname in brackets, sender's email account msg ['to'] = formataddr (["recipient's email nickname", my_user]) # The corresponding recipient's email nickname in the brackets, and the recipient's email account msg ['subobject'] = "topic" # The Subject of the email, also known as the title server = smtplib. SMTP ("smtp.xxx.com", 25) # SMTP server in the sender's mailbox, Port: 25 server. login (my_sender, "sender's email password") # it corresponds to the sender's email account and password server. sendmail (my_sender, [my_user,], msg. as_string () # it corresponds to the sender's email account, recipient's email account, and email sending server. quit () # this sentence means to close the connection except T Exception: # if the statement in try is not executed, the following ret = False return retret = mail () will be executed () if ret: print ("OK") # if the message is sent successfully, OK is returned. wait about 20 seconds to receive the email else: print ("filed ") # if the sending fails, filed is returned.

If the message is sent successfully, OK is returned. otherwise, execution fails, for example:

Error messageExample: File "F:/python/s12/pymail. py", line2

SyntaxError: Non-ASCIIcharacter '\ xe5 'infilef:/python/s12/pymail. pyonline2, butnoencodingdeclared; see #

So when the above encoding error prompt appears, you can add

# Coding: UTF-8 # forcibly encoded as UTF-8

Or

# Coding: GBK # forcibly encoded as GBK

The above is all the content of this article. I hope it will help you learn and support PHP.

For more information about how to use Python to send SMTP emails, see PHP!

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.