Send email in python and send email in python3
General email sending system process
To better understand the implementation of the mail sending function, you must first understand the general process of the mail sending system. First, emails are sent and accepted from one site (mail sending server) to the destination site (Mail receiving server), and then the destination site processes the received emails, and send it to the receiver. Each email server acts as the sender and receiver, and the address, port number, and configuration of each server are also different.
In fact, there are two steps to send an email:
First, write an email.
- Write the sender and receiver
- Write a topic
- Write the text (including attachments and images)
- Sort emails together
Second, send an email
- Connect to the email sending server
- Logon email
- Send email
- Exit email
Code Section:
#-*-Coding: UTF-8-*-import smtplibfrom email. header import Headerfrom email. mime. image import MIMEImagefrom email. mime. multipart import MIMEMultipartfrom email. mime. text import MIMEText # create an email object, which is equivalent to the email msg = MIMEMultipart () # sender msg ['from'] = 'xxxxxxxx .qq.com '# receiver msg ['to'] = '2017 @ qq.com' # Subject msg ['subobject'] = Header ('this is the email topic part ', charset = 'utf-8') # Chinese topic # text content txt = 'Dear user 'part1 = MIMEText (txt, 'plain ', 'utf-8 ') # Add msg to the email. attach (part1) # hypertext content with images. Note the src path. html = '<div> dear </div> '# Make sure to set the mail format. Otherwise, garbled characters may occur. At that time, I took over a project without the mail format in the hands of others, pitfall me for half a day part2 = MIMEText (html, 'html', 'utf-8') # add it to the mail msg. attach (part2) # Put the image in the email # specify the image as the current directory fp = open('test.png ', 'rb') msgImage = MIMEImage (fp. read () fp. close () # define the image ID and reference msgImage in HTML text. add_header ('content-id', '<image1>') msg. attach (msgImage) # Add the attachment att2 = MIMEText ('this is the text in the attachment ', 'base64', 'utf-8 ') att2 ["Content-Type"] = 'application/octet-stream'attachment set the attachment name to runoob.txt att2 ["Content-Disposition"] = 'attachment; filename = "runoob.txt" '# Place the attachment in the email msg. attach (att2) # non-ssl smtp (Simple Mail sending protocol). The following comments describe the ssl smtpsmtp = smtplib. SMTP () # smtp = smtplib. SMTP_SSL () try: # connect to the host address smtp. connect ('mail .yto.net.cn: 25') # log on to your mailbox. You need an email account and password smtp. login ('xxxxxxxx .qq.com ', '000000') # send the email, the email number to be logged on, the email Number of the sender, And the smtp sending content. sendmail ('xxxxxxxx .qq.com ', '2017 @ qq.com', msg. as_string () # exit smtp. quit () print 'the mail is sended successly. 'Small t smtplib. SMTPException, e: print "Error: unable to send mail", e
Sending result:
Note: When your mailbox is used as the sender, note that the smtp service is enabled in the settings of the mail server.