Send mail based on Python

Source: Internet
Author: User

Implementation features:

e-mail, support text, audio files, text files, graphic files, applications and other types of files sent;

Support for different mailboxes;

Support to send to multiple mailboxes at once;

Support to send n attachments at once;

Support for Chinese named attachment sending;

mail.conf Configuration :

[SMTP]
Login_user = [email protected]
Login_pwd = xxxxx
FROM_ADDR = [email protected]
To_addrs = [' [email protected] ', ' [email protected] '
Host = smtp.163.com
Port = 25

Description: Different types of mailboxes ( sender's mailbox ) , you need to modify the configuration file for the corresponding Host and Port

Smtp.163.com:25

smtp.qq.com:465

Practice Code:

#!/usr/bin/env python
#-*-CODING:GBK-*-
' Shouke '
Configparser
Smtplib
Mimemultipart
Mimetext
Mimeimage
Mimeaudio
Mimeapplication
Mimetypes
Os
MyMail:
__init__ (self, mail_config_file):
Config = Configparser.configparser ()
Config.read (Mail_config_file)
SELF.SMTP = Smtplib. SMTP ()
Self.login_user = Config.get (' SMTP 'login_user ')
Self.login_pwd = Config.get (' SMTP 'login_pwd ')
SELF.FROM_ADDR = Config.get (' SMTP 'from_addr ')
Self.to_addrs = Config.get (' SMTP 'To_addrs ')
Self.host = Config.get (' SMTP 'host ')
Self.port = Config.get (' SMTP 'Port ')
Connect to Server
Connect (self):
Self.smtp.connect (Self.host, Self.port)
Log in to the mail server
Login (self):
Try:
Self.smtp.login (Self.login_user, Self.login_pwd)
E:
Print (% E)
Send mail
Send_mail (self, mail_subject, mail_content, Attachment_path_set):
Construction Mimemultipart object as the root container
msg = Mimemultipart ()
msg[' from '] = self.from_addr
# msg[' to '] = Self.to_addrs
Msg[' to '] = ', '. Join (Eval_r (Self.to_addrs))
msg[' Subject '] = Mail_subject
Add message Content
Content = Mimetext (mail_content, _charset=' GBK ')
Msg.attach (content)
Attachment_path_set:
If an attachment exists
Type, coding = Mimetypes.guess_type (Attachment_path)
Type = = None:
' Application/octet-stream '
Major_type, Minor_type = Type.split ('/', 1)
' RB 'file:
' text ':
Attachment = Mimetext (File.read (), _subtype=minor_type)
' Image ':
Attachment = Mimeimage (File.read (), _subtype=minor_type)
' Application ':
Attachment = Mimeapplication (File.read (), _subtype=minor_type)
' Audio ':
Attachment = Mimeaudio (File.read (), _subtype=minor_type)
Modify Attachment Name
Attachment_name = Os.path.basename (Attachment_path)
Attachment.add_header ('content-disposition 'attachment', filename = (' GBK ', Attachment_name))
Msg.attach (Attachment)
Get the full text after formatting
Full_text = msg.as_string ()
Send mail
Self.smtp.sendmail (Self.from_addr, Eval_r (Self.to_addrs), Full_text)
Exit
Quit (self):
Self.smtp.quit ()
' __main__ ':
MyMail = MyMail ('./mail.conf ')
Mymail.connect ()
Mymail.login ()
' Hello, Pro, this is a test e-mail, receive please reply ^^ '
Mymail.send_mail (' mail header -- Pro, receive an email, please check in time ', mail_content, {' d:  \ \shouke.csv 'd:\ \2345haoya_3.1.1.9229.exe ',
' d: \\ Shouke.ini ',' d:\ \shouke.ini 'd:\ \  Test.mp3 'd:\ \test.png 'd:\ \  Report20150507204645.html ',
' d: \\  copy . sql '})
Mymail.quit ()


PDF version and mimetypes.py:http://pan.baidu.com/s/1p3c3w

Send mail based on Python

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.