Day8-python Study notes (19) Send mail

Source: Internet
Author: User
Tags base64

How to send an email
1. Open the POP3/SMTP service in the mailbox settings
2, some mailbox with password can not send, must obtain authorization code to be able.



Import Smtplib,os
From email.mime.text import Mimetext
From email.mime.multipart import Mimemultipart
Import Base64
class SendMail (object):
def __init__ (Self,username,passwd,recv,title,content,
File=none,ssl=false,
email_host= ' stmp.qq.com ', port=25,ssl_port=465):
" "
:p Aram Username: User name
:p Aram passwd: Password
:p Aram recv: Recipient, multiple to send list [' [email protected] ', ' [email protected]
:p Aram title: Message title
:p Aram content: Message body
:p Aram File: Attachment path, if not in the current directory, to write absolute path, default no attachment
:p Aram SSL: Secure link, default is normal
:p Aram EMAIL_HOST:SMTP server address, default is 163 server
:p Aram Port: Non-Secure link ports, default
:p Aram Ssl_port: Secure link port, default is 465
" "
self.username = Username #用户名
self.passwd = passwd #密码
self.recv = recv #收件人, multiple to send list [' [email protected] ', ' [email protected]
self.title = title #邮件标题
self.content = Content #邮件正文
self.file = File #附件路径, if not in current directory, to write absolute path
self.email_host = email_host #smtp服务器地址
self.port = Port #普通端口
Self.ssl = SSL #是否安全链接
self.ssl_port = Ssl_port #安全链接端口
def send_mail (self):
msg = Mimemultipart ()
#发送内容的对象
if Self.file: #处理附件的
file_name = Os.path.split (Self.file) [-1] #只取文件名, no path is taken
Try:
f = open (Self.file, ' RB '). Read ()
except Exception as E:
raise Exception (' Accessory not OPEN!!!! ')
Else:
att = mimetext (f, "base64", "Utf-8")
att["content-type"] = ' application/octet-stream '
#base64. B64encode (File_name.encode ()). Decode ()
new_file_name= ' =?utf-8?b? ' + Base64.b64encode (File_name.encode ()). Decode () + '? = '
#这里是处理文件名为中文名的 must be written like this.
att["content-disposition"] = ' attachment; filename= '%s '% (new_file_name)
Msg.attach (ATT)
Msg.attach (Mimetext (self.content)) #邮件正文的内容
msg[' Subject ' = self.title # message Subject
msg[' from ' = self.username # sender Account
msg[' to '] = ', '. Join (SELF.RECV) # Recipient account List
if Self.ssl:
self.smtp = Smtplib. Smtp_ssl (Self.email_host,port=self.ssl_port)
Else:
self.smtp = Smtplib. SMTP (Self.email_host,port=self.port)
#发送邮件服务器的对象
Self.smtp.login (SELF.USERNAME,SELF.PASSWD)
Try:
Self.smtp.sendmail (self.username,self.recv,msg.as_string ())
Pass
except Exception as E:
print (' something went wrong. ', E)
Else:
print (' Send successfully! ')
self.smtp.quit ()


if __name__ = = ' __main__ ':
m = SendMail (
username= ' [email protected] ',
passwd= ' GEIRTEEFOVHVBJFB ',
recv=[' [email protected] ', ' [email protected] ',
title= ' Scorpio-fb-New Year good Bull Ox ',
content= ' Dog luck, great luck ~ ',
file=r ' C:\Users\Administrator\Desktop\ test case. xlsx ',
Ssl=true,
email_host = ' smtp.qq.com ',

)
M.send_mail ()

Day8-python Study notes (19) Send mail

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.