python sample code for sending mail of Python2 and 3 can be used directly
- 2021-08-12 03:09:37
- OfStack
Send plain mail
Send text and html regular mail as follows:
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
def get_server(username):
"""
Get a mailbox server by mailbox address
:param username: User name, such as: 123456@qq.com
:return: Mailbox server address can be added according to your actual business, such as: smtp.qq.com
"""
servers = {'qq' : 'smtp.qq.com'
, '126' : 'smtp.126.com'
, '163' : 'smtp.163.com'
, '139' : 'smtp.139.com'}
for key,value in servers.items():
if key in username:
return value
def send_mail(username, password, to, sender_name, subject, content, email_type):
"""
:param username:
:param password:
:param to: Recipient list []
:param sender_name:
:param subject:
:param content:
:param email_type:
:return:
"""
from_addr = username
password = password
to_addr = to
smtp_server = get_server(username)
# The message body is MIMEText Type
msg = MIMEText('%s'%(content), '%s'%(email_type), 'utf-8')
msg['From'] = _format_addr('%s<%s>' % (sender_name, from_addr))
msg['To'] = _format_addr('<%s>' % to_addr)
msg['Subject'] = Header('%s'%(subject), 'utf-8').encode()
# The normal login port is 25 , belt ssl When verifying, the port is 465
# smtp_server = 'smtp.exmail.qq.com'
# server = smtplib.SMTP_SSL(smtp_server, 465)
server = smtplib.SMTP(smtp_server, 25)
server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, to_addr, msg.as_string())
server.quit()
if __name__ == '__main__':
""" Send Simple Text Mail """
username = '******@126.com'
password = '******'
sender_name = '******@126.com'
subject = 'test Mail '
content = '<html><h1>ikeguang Letter from </h1></html> <a href="http://www.ikeguang.com" rel="external nofollow" >ikeguang.com</a></html>'
# email_type Value: plain, Text type mail ;html,html Type mail
email_type = 'html'
_to = ['******@126.com', '******@qq.com']
for to in _to:
send_mail(username, password, to, sender_name, subject, content, email_type)
print('send mail to %s success' % to)
Send a message with attachments
def send_mail_multipart(username, password, to, sender_name, subject, content, email_type):
from_addr = username
password = password
to_addr = to
smtp_server = get_server(username)
msg = MIMEMultipart()
# The message body is MIMEText Type
msg.attach(MIMEText('%s'%(content), '%s'%(email_type), 'utf-8'))
msg['From'] = _format_addr('%s<%s>' % (sender_name, from_addr))
msg['To'] = _format_addr('<%s>' % to_addr)
msg['Subject'] = Header('%s'%(subject), 'utf-8').encode()
# Read attachments
filename = 'D:/ My Papers /Codes/PyCode/source/image/0.jpg'
with open(filename, 'rb') as f:
# Set the attachment's MIME And file name, here is png Type :
mime = MIMEBase('image', 'jpg', filename='0.jpg')
# Add the necessary header information :
mime.add_header('Content-Disposition', 'attachment', filename='0.jpg')
mime.add_header('Content-ID', '<0>')
mime.add_header('X-Attachment-Id', '0')
# Read in the contents of the attachment :
mime.set_payload(f.read())
# Use Base64 Code :
encoders.encode_base64(mime)
# Add to MIMEMultipart:
msg.attach(mime)
# The normal login port is 25 , belt ssl When verifying, the port is 465
# smtplib.SMTP_SSL(smtp_server, 465)
server = smtplib.SMTP(smtp_server, 25)
server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()