Home >Backend Development >Python Tutorial >A detailed explanation of how to implement the function of sending emails and attachments in python

A detailed explanation of how to implement the function of sending emails and attachments in python

黄舟
黄舟Original
2017-05-21 14:10:221980browse

This article mainly introduces the function of sending emails and attachments in detail to everyone python, which has a certain reference value. Interested friends can refer to it

To everyone today Let’s talk about sending emails in python. If there are any more official ones, just go to Baidu. There are also a lot of documents. To be honest, most people will not read them unless they have to. Return to the topic:

I am a mac. If you don’t follow the instructions If you rely on modules, please follow the screenshot below to install

. If there are no errors, it means the installation has been successful.

Python sends a file attachment of an unknown MIME type. The basic idea is as follows:
1. Construct a MIMEMultipart object as the root container
2. Construct a MIMEText object as an email display Content and append to the root container
3. Construct a MIMEBase object as the file attachment content and append it to the root container
a. Read the file content and format it
b. Set the attachment header
4. Set Root containerProperties
5. Get the complete formatted text
6. Use smtp to send mail

Example code:

 #!/usr/bin/env python
 # -*- coding:utf-8 -*-
 
 import smtplib
 from email.mime.multipart import MIMEMultipart
 from email.mime.text import MIMEText
 from email.mime.application import MIMEApplication
 
 class Mailer(object):
   def init(self,maillist,mailtitle,mailcontent):
     self.mail_list = maillist
     self.mail_title = mailtitle
     self.mail_content = mailcontent
 
     self.mail_host = "smtp.163.com"
     self.mail_user = "your email name"
     self.mail_pass = "your email password"
     self.mail_postfix = "163.com"
 
   def sendMail(self):
 
     me = self.mail_user + "<" + self.mail_user + "@" + self.mail_postfix + ">"
     msg = MIMEMultipart()
     msg[&#39;Subject&#39;] = &#39;Python mail Test&#39;
     msg[&#39;From&#39;] = me
     msg[&#39;To&#39;] = ";".join(self.mail_list)
 
     #puretext = MIMEText(&#39;<h1>你好,<br/>&#39;+self.mail_content+&#39;</h1>&#39;,&#39;html&#39;,&#39;utf-8&#39;)
     puretext = MIMEText(&#39;纯文本内容&#39;+self.mail_content)
     msg.attach(puretext)
 
     # jpg类型的附件
     jpgpart = MIMEApplication(open(&#39;/home/mypan/1949777163775279642.jpg&#39;, &#39;rb&#39;).read())
     jpgpart.add_header(&#39;Content-Disposition&#39;, &#39;attachment&#39;, filename=&#39;beauty.jpg&#39;)
     msg.attach(jpgpart)
 
     # 首先是xlsx类型的附件
     #xlsxpart = MIMEApplication(open(&#39;test.xlsx&#39;, &#39;rb&#39;).read())
     #xlsxpart.add_header(&#39;Content-Disposition&#39;, &#39;attachment&#39;, filename=&#39;test.xlsx&#39;)
     #msg.attach(xlsxpart)
 
     # mp3类型的附件
     #mp3part = MIMEApplication(open(&#39;kenny.mp3&#39;, &#39;rb&#39;).read())
     #mp3part.add_header(&#39;Content-Disposition&#39;, &#39;attachment&#39;, filename=&#39;benny.mp3&#39;)
     #msg.attach(mp3part)
 
     # pdf类型附件
     #part = MIMEApplication(open(&#39;foo.pdf&#39;, &#39;rb&#39;).read())
     #part.add_header(&#39;Content-Disposition&#39;, &#39;attachment&#39;, filename="foo.pdf")
     #msg.attach(part)
 
     try:
       s = smtplib.SMTP() #创建邮件服务器对象
       s.connect(self.mail_host) #连接到指定的smtp服务器。参数分别表示smpt主机和端口
       s.login(self.mail_user, self.mail_pass) #登录到你邮箱
       s.sendmail(me, self.mail_list, msg.as_string()) #发送内容
       s.close()
       return True
     except Exception, e:
       print str(e)
       return False
 
 
 if name == &#39;main&#39;:
   #send list
   mailto_list = ["aaa@lsh123.com","bbb@163.com"]
   mail_title = &#39;Hey subject&#39;
   mail_content = &#39;Hey this is content&#39;
   mm = Mailer(mailto_list,mail_title,mail_content)
   res = mm.sendMail()
   print res

The above is the detailed content of A detailed explanation of how to implement the function of sending emails and attachments in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn