Python for mail delivery

Source: Internet
Author: User
Tags html form all mail

Using the Smtplib module to send mail,it provides a simple encapsulation of the SMTP protocol.

Basic commands for the SMTP protocol include:

HELO Identifying user identities to the server

Mail initiates message transfer mail from:

RCPT identifies a single recipient of a message; often behind the mail command, there can be multiple RCPT to:

Data after single or multiple RCPT commands, indicates that all mail recipients have been identified and initialized with the data transfer to. End

The VRFY is used to verify that the specified user/mailbox exists, and the server often prohibits this command for security reasons

EXPN verifies that a given list of mailboxes exists, expands the list of mailboxes, and is often disabled

What commands are supported by the Help query server

NOOP no action, server should respond OK

QUIT End Session

RSET Reset session, the current transfer is canceled

Mail from specify Sender address

RCPT to specified recipient address

General SMTP session There are two ways, one is direct mail delivery, that is, for example, you want to send e-mail to [email protected], then directly connected to the 163.com mail server, the letter to [email protected]; The other is the verification after the letter, the process is, for example, you want to send e-mail to [email protected], you do not directly to the 163.com, but through their own in Sina.com another mailbox to send. This will first connect the Sina.com SMTP server, and then authentication, and then send to the 163.com letter to Sina.com, Sina.com will help you deliver the letter to 163.com.

Mail in file Form

#!/usr/bin/env Python3

#coding: Utf-8

Import Smtplib

From Email.mime.text import Mimetext

From Email.header Import Header

Sender = ' * * * '

Receiver = ' * * * '

Subject = ' python email test '

SmtpServer = ' smtp.163.com '

Username = ' * * * '

Password = ' * * * '

msg = mimetext (' Hello ', ' text ', ' utf-8 ') #中文需参数 ' Utf-8 ', single-byte characters do not need

msg[' Subject ' = Header (Subject, ' utf-8 ')

SMTP = Smtplib. SMTP ()

Smtp.connect (' smtp.163.com ')

Smtp.login (username, password)

Smtp.sendmail (sender, receiver, msg.as_string ())

Smtp.quit ()

Messages in HTML form

#!/usr/bin/env Python3

#coding: Utf-8

Import Smtplib

From Email.mime.text import Mimetext

Sender = ' * * * '

Receiver = ' * * * '

Subject = ' python email test '

SmtpServer = ' smtp.163.com '

Username = ' * * * '

Password = ' * * * '

msg = Mimetext ('

msg[' Subject '] = Subject

SMTP = Smtplib. SMTP ()

Smtp.connect (' smtp.163.com ')

Smtp.login (username, password)

Smtp.sendmail (sender, receiver, msg.as_string ())

Smtp.quit ()

HTML message with picture

#!/usr/bin/env Python3

#coding: Utf-8

Import Smtplib

From Email.mime.multipart import Mimemultipart

From Email.mime.text import Mimetext

From email.mime.image import mimeimage

Sender = ' * * * '

Receiver = ' * * * '

Subject = ' python email test '

SmtpServer = ' smtp.163.com '

Username = ' * * * '

Password = ' * * * '

Msgroot = Mimemultipart (' related ')

msgroot[' Subject ' = ' test message '

Msgtext = Mimetext (' <b>some <i>HTML</i> text</b> and an image.<br><br>good! ', ' html ', ' Utf-8 ')

Msgroot.attach (msgtext)

fp = open (' h:\\python\\1.jpg ', ' RB ')

Msgimage = Mimeimage (Fp.read ())

Fp.close ()

Msgimage.add_header (' Content-id ', ' <image1> ')

Msgroot.attach (msgimage)

SMTP = Smtplib. SMTP ()

Smtp.connect (' smtp.163.com ')

Smtp.login (username, password)

Smtp.sendmail (sender, receiver, msgroot.as_string ())

Smtp.quit ()

Mail with Attachments

#!/usr/bin/env Python3

#coding: Utf-8

Import Smtplib

From Email.mime.multipart import Mimemultipart

From Email.mime.text import Mimetext

From email.mime.image import mimeimage

Sender = ' * * * '

Receiver = ' * * * '

Subject = ' python email test '

SmtpServer = ' smtp.163.com '

Username = ' * * * '

Password = ' * * * '

Msgroot = Mimemultipart (' related ')

msgroot[' Subject ' = ' test message '

#构造邮件中的附件

ATT = mimetext (open (' h:\\python\\1.jpg ', ' RB '). Read (), ' base64 ', ' Utf-8 ')

att["Content-type"] = ' application/octet-stream '

att["content-disposition"] = ' attachment; Filename= "1.jpg"

Msgroot.attach (att)


SMTP = Smtplib. SMTP ()

Smtp.connect (' smtp.163.com ')

Smtp.login (username, password)

Smtp.sendmail (sender, receiver, msgroot.as_string ())

Smtp.quit ()

Group Mail

#!/usr/bin/env Python3

#coding: Utf-8

Import Smtplib

From Email.mime.text import Mimetext

Sender = ' * * * '

Receiver = [' * * * ', ' * * * ',......]  

Subject = ' python email test '

SmtpServer = ' smtp.163.com '

Username = ' * * * '

Password = ' * * * '

msg = mimetext (' Hello ', ' text ', ' utf-8 ')

msg[' Subject '] = Subject

SMTP = Smtplib. SMTP ()

Smtp.connect (' smtp.163.com ')

Smtp.login (username, password)

Smtp.sendmail (sender, receiver, msg.as_string ())

Smtp.quit ()

You can also use the Yagmail module to quickly implement

Import Yagmail

YAG = Yagmail. SMTP (user= ' [email protected] ', password= ' XXX '

Yag.send (to = ' [email protected] ', subject = ' Test ', contents = ' This is a test e-mail from Windows CMD tools with the Yagmai ')


Python for mail delivery

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.