One, the python sends the mail uses the Smtplib module, is a standard package, the direct import imports uses, the code is as follows:
Import Smtplib
From email.mime.text import Mimetext
email_host = ' smtp.163.com ' #邮件服务器地址
email_user = ' [email protected] ' # Sender account
email_pwd = ' woshinige123 ' # sender password, yes, authorization code (in client settings)
maillist = ' [email protected],[email protected] ' #收件人邮箱, multiple accounts, separated by commas
me = Email_user
msg = mimetext (' zhangying Test Mail ') # message content
msg[' Subject ' = ' zhangying Test mail ' # message subject
msg[' from ' = Me # sender Account
msg[' to ' = maillist # recipient Account List
SMTP = Smtplib. SMTP (EMAIL_HOST,PORT=25) # Connection mailbox, incoming mailbox address, and port number, SMTP port number is
Smtp.login (Email_user, email_pwd) # Sender's email account, password
Smtp.sendmail (Me, Maillist, msg.as_string ())
# The parameters are sender, receiver, and the third is to turn the contents of the above message into a string
Smtp.quit () # Exit SMTP after sending
print (' Email send success. ')
Second, send the mail with attachments
Import Smtplib#smtplib This module is to send mail
From email.mime.text import mimetext# structuring message content
from Email.mime.multipart import mimemultipart# email with attachments
email_host = ' smtp.163.com ' #邮箱服务器地址
email_user = ' [email protected] ' # Sender account
email_pwd = ' woshinige123 ' # sender password is the authorization code for the mailbox, not the login password
maillist = ' [email protected],[email protected] ' #收件人邮箱, multiple accounts, separated by commas
new_msg = Mimemultipart () #构建了一个能发附件的邮件对象
New_msg.attach (Mimetext (' This is a Python test e-mail message, don't reply ') ") # Message content
new_msg[' Subject ' = ' zhangying ' # message subject
new_msg[' from ' = email_user # sender Account
new_msg[' to ' = maillist # recipient Account List
att = mimetext (open (' a.txt '). Read ())
att["content-type"] = ' application/octet-stream '
att["content-disposition"] = ' attachment; filename= ' Haha.txt '
New_msg.attach (ATT)
#以上四行就是附件邮件特有的
SMTP = Smtplib. SMTP (EMAIL_HOST,PORT=25) # Connection mailbox, incoming mailbox address, and port number, SMTP port number is
Smtp.login (Email_user, email_pwd) # Sender's email account, password
Smtp.sendmail (Email_user, Maillist, New_msg.as_string ()) # parameters are the sender, the receiver, and the third is to change the contents of the above sent message into a string
Smtp.quit () # Exit SMTP after sending
print (' Email send success. ')
Three, encapsulated into a class
Import Smtplib
From email.mime.text import Mimetext
From email.mime.multipart import Mimemultipart
class SendMail (object):
def __init__ (Self,username,passwd,recv,title,content,
File=none,
email_host= ' smtp.163.com ', port=25):
Self.username = Username
self.passwd = passwd
self.recv = recv
Self.title = Title
self.content = Content
self.file = File
self.email_host = Email_host
Self.port = Port
def send_mail (self):
msg = Mimemultipart ()
#发送内容的对象
if Self.file: #处理附件的
att = mimetext (open (self.file). Read ())
att["content-type"] = ' application/octet-stream '
att["content-disposition"] = ' attachment; filename= '%s '%self.file
Msg.attach (ATT)
Msg.attach (Mimetext (self.content)) #邮件正文的内容
msg[' Subject ' = self.title # message Subject
msg[' from ' = self.username # sender Account
msg[' to ' = self.recv # recipient Account List
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 ())
except Exception as E:
print (' something went wrong. ', E)
Else:
print (' Send successfully! ')
def __del__ (self):
self.smtp.quit ()
if __name__ = = ' __main__ ': #执行当前python文件的时候执行, not executed when imported by others
m = SendMail (
username= ' [email protected] ', passwd= ' woshinige123 ', recv= ' [email protected] ',
title= ' New Send mail ', content= ' hahaha ah haha ', file= ' a.txt '
)
M.send_mail ()
Python Send mail