Details about the Python mail sending example and the python mail sending example

Source: Internet
Author: User
Tags starttls

Details about the Python mail sending example and the python mail sending example

Python needs two modules: smtplib and email. It is also because we can import these modules in our actual work that it makes processing tasks easier. Today, let's take a good look at sending emails using Python.

SMTP is the mail sending protocol. Python provides built-in support for SMTP, which can send plain text emails, HTML emails, and emails with attachments.

Python supports two SMTP modules: smtplib and email. email is responsible for constructing emails, while smtplib is responsible for sending emails.

1. The mail body is in text format.

#-*-Coding: UTF-8-*-from email. mime. multipart import MIMEMultipartfrom email. mime. text import MIMETextimport smtplibimport sysimport csvimport xlrdfrom pyExcelerator import * import osimport xlwtfrom xlutils. copy import copyimport pyExceleratorimport datetimeimport timereload (sys) sys. setdefaultencoding ("UTF-8 ") mailto_list = [""] # mail recipient's email address mail_host = "smtp.exmail.qq.com" # mail_user = "" # mail sender's email account mail_pass = "" # mail sender's email Password def send_mail (to_list, sub, content): me = "genius idiot dream" + "<" + mail_user + ">" msg = MIMEText (content, _ subtype = 'plain ', _ charset = 'utf-8') msg ['subobject'] = sub # mail Subject msg ['from'] = me msg ['to'] = ";". join (to_list) try: server = smtplib. SMTP () server. connect (mail_host) server. login (mail_user, mail_pass) server. sendmail (me, to_list, msg. as_string () server. close () return True except t Exception, e: print str (e) return Falseif _ name _ = '_ main __': sub = "genius idiot dream" content = '... 'If send_mail (mailto_list, sub, content): print "sent successfully" else: print "failed to send"

2. The mail body is in the form of a table:Because it is a table, we select HTML to implement the table function. The content displayed on the email is HTML.

#-*-Coding: UTF-8-*-from email. mime. multipart import MIMEMultipartfrom email. mime. text import MIMETextimport smtplibimport sysimport csvimport xlrdfrom pyExcelerator import * import osimport xlwtfrom xlutils. copy import copyimport pyExceleratorimport datetimeimport timereload (sys) sys. setdefaultencoding ("UTF-8 ") mailto_list = [""] # mail recipient's email address mail_host = "smtp.exmail.qq.com" # mail_user = "" # mail sender's email account mail_pass = "" # mail sender's email Password def send_mail (to_list, sub, content): me = "genius idiot dream" + "<" + mail_user + ">" # unlike the above Code, here we select the html format msg = MIMEText (content, _ subtype = 'html', _ charset = 'utf-8 ') msg ['subobject'] = sub # Subject msg ['from'] = me msg ['to'] = ";". join (to_list) try: server = smtplib. SMTP () server. connect (mail_host) server. login (mail_user, mail_pass) server. sendmail (me, to_list, msg. as_string () server. close () return True except t Exception, e: print str (e) return Falseif _ name _ = '_ main __': sub = "genius idiot dream" html = '

3. the email body is in the image format:To embed an image into the body of the email, we only need to add the email as an attachment by sending the attachment, and then reference src = "cid: 0 "to embed the attachment as an image. If there are multiple images, numbers them in sequence, and then reference different CIDS: x.

Def send_mail (to_list, sub, content): me = "genius idiot dream" + "<" + mail_user + ">" msg = MIMEMultipart () msg ['subobject'] = sub # Subject msg ['from'] = me msg ['to'] = ";". join (to_list) txt = MIMEText ("genius idiot dream", _ subtype = 'plain ', _ charset = 'utf8') msg. attach (txt) # <B>: <I>: italic msgText = MIMEText ('<B> Some <I> HTML </I> text </B> and an image.  good! ', 'Html', 'utf-8') msg. attach (msgText) file1 = "F: \ 1.jpg" image = MIMEImage (open (file1, 'rb '). read () image. add_header ('content-id', '<image1>') msg. attach (image) try: server = smtplib. SMTP () server. connect (mail_host) server. login (mail_user, mail_pass) server. sendmail (me, to_list, msg. as_string () server. close () return True except t Exception, e: print str (e) return Falseif _ name _ = '_ main __': sub = "genius idiot dream" html = '

4. Send email attachments: the email attachments are images.

Def send_mail (to_list, sub, content): me = "genius idiot dream" + "<" + mail_user + ">" msg = MIMEMultipart () msg ['subobject'] = sub # Subject msg ['from'] = me msg ['to'] = ";". join (to_list) txt = MIMEText ("genius idiot dream", _ subtype = 'plain ', _ charset = 'utf8') msg. attach (txt) # <B>: <I>: italic # msgText = MIMEText ('<B> Some <I> HTML </I> text </B> and an image.  good! ', 'Html', 'utf-8') # msg. attach (msgText) # file1 = "F: \ 1.jpg" # image = MIMEImage (open (file1, 'rb '). read () # image. add_header ('content-id', '<image1>') # msg. attach (image) att = MIMEText (open ('f: \ 1.jpg ', 'rb '). read (), 'base64', 'utf-8 ') att ["Content-Type"] = 'application/octet-stream' att ["Content-Disposition"] = 'attachment; filename = "1.jpg" 'msg. attach (att) try: server = smtplib. SMTP () server. connect (mail_host) server. login (mail_user, mail_pass) server. sendmail (me, to_list, msg. as_string () server. close () return True expect t Exception, e: print str (e) return False

5. Send group Emails: send emails to multiple users at the same time.
Mailto_list = [""] # mail recipient's email address
The above code is the email address of the email recipient. If we want to send emails to multiple users, we only need to bind the email account of the recipient to this list.

Encrypted SMTP

When the standard port 25 is used to connect to the SMTP server, plaintext transmission is used, and the whole process of sending emails may be eavesdropped. To send emails more securely, You can encrypt the SMTP session. In fact, you must first create an SSL secure connection and then use the SMTP protocol to send emails.

Method: After creating an SMTP object, call the starttls () method immediately to create a secure connection.

Smtp_server = 'smtp .qq.com 'smtp _ port = 25 # The default port number is 25 server = smtplib. SMTP (smtp_server, smtp_port) server. starttls () # The remaining code is exactly the same as the previous one: server. set_debuglevel (1) # print all interaction information with the SMTP server

The above is a detailed explanation of the Python mail, hope to help you learn.

Articles you may be interested in:
  • Example code for sending emails in python (html, images, and attachments are supported)
  • Two types of python mail sending example (python mail attachment can be implemented using the email module)
  • Python group email instance code
  • Example of sending and receiving emails using python
  • Example of sending a mail via python (Chinese mail title supported)
  • Example of seven email content Sending methods in python
  • Python automatically sends emails to QQ Group members
  • Python reads INI files, operates mysql, and sends mail instances
  • How does python send mail to two recipients at the same time?

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.