Use smtplib and email encapsulation python to send mail module class sharing _python

Source: Internet
Author: User
Tags base64

Copy Code code 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:
# Constructors: Initializing 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

# Send a file or HTML message
def sendtxtmail (self, to_list, sub, content, subtype= ' html '):
# If a text message is sent, the _subtype is set to plain
# If you send an HTML message, _subtype is set to HTML
msg = mimetext (content, _subtype=subtype, _charset= ' Utf-8 ')
msg[' Subject ' = Sub
Msg[' from '] = Self._me
Msg[' to '] = ";". Join (To_list)
Try
Self._server.sendmail (Self._me, To_list, msg.as_string ())
Return True
Except Exception, E:
Print str (e)
Return False

# Send files or HTML messages with attachments
def sendattachmail (self, to_list, sub, content, subtype= ' html '):
# Create an instance with an attachment
msg = Mimemultipart ()
# Add Annex 1
ATT1 = Mimetext (open (R ' D:\javawork\PyTest\src\main.py ', ' RB '). Read (), ' base64 ', ' Utf-8 '
att1["Content-type"] = ' application/octet-stream '
# The filename here can be arbitrarily written, what name to write, what name to display in the message
att1["content-disposition"] = ' attachment; Filename= "main.py"
Msg.attach (ATT1)

# Add Annex 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 Mail Content
Msg.attach (Mimetext (content, _subtype=subtype, _charset= ' Utf-8 '))

msg[' Subject ' = Sub
Msg[' from '] = Self._me
Msg[' to '] = ";". Join (To_list)

Try
Self._server.sendmail (Self._me, To_list, msg.as_string ())
Return True
Except Exception, E:
Print str (e)
Return False
# Send files or HTML messages with attachments
def sendimagemail (self, to_list, sub, content, subtype= ' html '):
# Create an instance with an attachment
msg = Mimemultipart ()

# Add Mail Content
Msg.attach (Mimetext (content, _subtype=subtype, _charset= ' Utf-8 '))

# Add Picture Accessories
Image = Mimeimage (open (R ' D:\javawork\PyTest\src\test.jpg ', ' RB '). Read ())
#附件列表中显示的文件名
Image.add_header (' content-disposition ', ' attachment;filename=p.jpg ')
Msg.attach (image)

msg[' Subject ' = Sub
Msg[' from '] = Self._me
Msg[' to '] = ";". Join (To_list)

Try
Self._server.sendmail (Self._me, To_list, msg.as_string ())
Return True
Except Exception, E:
Print str (e)
Return False

# destructor: Releasing 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><br>Print "Send Success"
Else
Print "Send Failed"

If Mail.sendattachmail (mailto_list, "test message-with two attachments", "Hello world! <br><br>Print "Send Success"
Else
Print "Send Failed"

If Mail.sendimagemail (mailto_list, "test message-with an attachment to a picture", "Hello world! <br><br>Print "Send Success"
Else
Print "Send Failed"

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.