Use smtplib and email to encapsulate the python mail sending module class sharing

Source: Internet
Author: User

Copy codeThe Code is as follows:
#! /Usr/bin/python
# Encoding = UTF-8
# Filename: send_email.py
From email. mime. image import MIMEImage
From email. mime. multipart import MIMEMultipart
From email. mime. text import MIMEText
Import smtplib


Class SendEmail:
# Constructor: Initialize Basic Information
Def _ init _ (self, host, user, passwd ):
LInfo = user. split ("@")
Self. _ user = user
Self. _ account = lInfo [0]
Self. _ me = self. _ account + "<" + self. _ user + ">"

Server = smtplib. SMTP ()
Server. connect (host)
Server. login (self. _ account, passwd)
Self. _ server = server

# Sending a file or html email
Def sendTxtMail (self, to_list, sub, content, subtype = 'html '):
# If a text message is sent, SET _ subtype to plain
# If an html message is sent, SET _ subtype to html.
Msg = MIMEText (content, _ subtype = subtype, _ charset = 'utf-8 ')
Msg ['subobject'] = sub
Msg ['from'] = self. _ me
Msg ['to'] = ";". join (to_list)
Try:
Self. _ server. sendmail (self. _ me, to_list, msg. as_string ())
Return True
Except t Exception, e:
Print str (e)
Return False

# Send an attachment file or html email
Def sendAttachMail (self, to_list, sub, content, subtype = 'html '):
# Create an instance with an attachment
Msg = MIMEMultipart ()
# Add Attachment 1
Att1 = MIMEText (open (r 'd: \ javawork \ PyTest \ src \ main. py', 'rb '). read (), 'base64', 'utf-8 ')
Att1 ["Content-Type"] = 'application/octet-stream'
# Here, the filename can be written at will. What name can be written, and what name is displayed in the email?
Att1 ["Content-Disposition"] = 'attachment; filename = "main. py "'
Msg. attach (att1)

# Add Attachment 2
Att2 = MIMEText (open (r 'd: \ javawork \ PyTest \ src \ main. py', 'rb '). read (), 'base64', 'utf-8 ')
Att2 ["Content-Type"] = 'application/octet-stream'
Att2 ["Content-Disposition"] = 'attachment; filename = "main.txt "'
Msg. attach (att2)

# Add email content
Msg. attach (MIMEText (content, _ subtype = subtype, _ charset = 'utf-8 '))

Msg ['subobject'] = sub
Msg ['from'] = self. _ me
Msg ['to'] = ";". join (to_list)

Try:
Self. _ server. sendmail (self. _ me, to_list, msg. as_string ())
Return True
Except t Exception, e:
Print str (e)
Return False
# Send an attachment file or html email
Def sendImageMail (self, to_list, sub, content, subtype = 'html '):
# Create an instance with an attachment
Msg = MIMEMultipart ()

# Add email content
Msg. attach (MIMEText (content, _ subtype = subtype, _ charset = 'utf-8 '))

# Adding image attachments
Image = MIMEImage (open (r 'd: \ javawork \ PyTest \ src \ test.jpg ', 'rb'). read ())
# File name displayed in the Attachment List
Image. add_header ('content-disposition', 'attachment?filename=p.jpg ')
Msg. attach (image)

Msg ['subobject'] = sub
Msg ['from'] = self. _ me
Msg ['to'] = ";". join (to_list)

Try:
Self. _ server. sendmail (self. _ me, to_list, msg. as_string ())
Return True
Except t Exception, e:
Print str (e)
Return False

# Destructor: release resources
Def _ del _ (self ):
Self. _ server. quit ()
Self. _ server. close ()

Mailto_list = ['xxx @ 163.com ']
Mail = SendEmail ('smtp .163.com ', 'xxx @ 163.com', 'xxxxxx ')
If mail. sendTxtMail (mailto_list, "test mail", "hello world! <Br> Print "sent successfully"
Else:
Print "failed to send"

If mail. sendAttachMail (mailto_list, "test mail-with two attachments", "hello world! <Br> Print "sent successfully"
Else:
Print "failed to send"

If mail. sendImageMail (mailto_list, "test email-attachment with an image", "hello world! <Br> Print "sent successfully"
Else:
Print "failed to send"

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.