3 methods for sending emails in Python

Source: Internet
Author: User

3 methods for sending emails in Python

This article mainly introduces three methods for sending emails in Python. This article describes how to use the logon email server method, sendmail command, and smtp service to send emails. For more information, see

It is relatively simple to send emails in python. you can log on to the mail service to send emails. in linux, you can also call the sendmail command to send emails. You can also use the local or remote smtp service to send emails, whether it is single, group, or CC, it is easy to implement.

First, record several simple email sending methods, such as html emails and attachments. You can check the document if necessary.

1. log on to the mail service

The Code is as follows:

#! /Usr/bin/env python

#-*-Coding: UTF-8 -*-

# Python2.7x

# Send_simple_email_by_account.py @ 2014-07-30

# Author: orangleliu

'''''

Use python to write simple emails

Use the 126 email service

'''

Import smtplib

From email. mime. text import MIMEText

SMTPserver = 'smtp .126.com'

Sender = 'liuzhizhi123 @ 126.com'

Password = "xxxx"

Message = 'I send a message by Python.'

Msg = MIMEText (message)

Msg ['subobject'] = 'test Email by python'

Msg ['from'] = sender

Msg ['to'] = destination

Mailserver = smtplib. SMTP (SMTPserver, 25)

Mailserver. login (sender, password)

Mailserver. sendmail (sender, [sender], msg. as_string ())

Mailserver. quit ()

Print 'send email success'

2. Call the sendmail command (linux)

The Code is as follows:

#-*-Coding: UTF-8 -*-

# Python2.7x

# Send_email_by _. py

# Author: orangleliu

# Date: 2014-08-15

'''''

The sendmail command is used.

At this time, the email may not be sent, and the hostname configuration may need to be changed.

'''

From email. mime. text import MIMEText

From subprocess import Popen, PIPE

Def get_sh_res ():

P = Popen (['/Application/2.0/nirvana/logs/log. Sh'], stdout = PIPE)

Return str (p. communicate () [0])

Def mail_send (sender, recevier ):

Print "get email info ..."

Msg = MIMEText (get_sh_res ())

Msg ["From"] = sender

Msg ["To"] = recevier

Msg ["Subject"] = "Yestoday interface log results"

P = Popen (["/usr/sbin/sendmail", "-t"], stdin = PIPE)

Res = p. communicate (msg. as_string ())

Print 'mail sended ...'

If _ name _ = "_ main __":

S = "957748332@qq.com"

R = "zhizhi.liu@chinacache.com"

Mail_send (s, r)

3. Use the smtp service for sending (local or remote server)

The Code is as follows:

#! /Usr/bin/env python

#-*-Coding: UTF-8 -*-

# Python2.7x

# Send_email_by_smtp.py

# Author: orangleliu

# Date: 2014-08-15

'''''

Use the local smtp service in linux to send emails

Before you enable the smtp service, check the Method

# Ps-ef | grep sendmail

# Telnet localhost 25

At this time, the email may not be sent, and the hostname configuration may need to be changed.

'''

Import smtplib

From email. mime. text import MIMEText

From subprocess import Popen, PIPE

Def get_sh_res ():

P = Popen (['/Application/2.0/nirvana/logs/log. Sh'], stdout = PIPE)

Return str (p. communicate () [0])

Def mail_send (sender, recevier ):

Msg = MIMEText (get_sh_res ())

Msg ["From"] = sender

Msg ["To"] = recevier

Msg ["Subject"] = "Yestoday interface log results"

S = smtplib. SMTP ('localhost ')

S. sendmail (sender, [recevier], msg. as_string ())

S. quit ()

Print 'send mail finished ...'

If _ name _ = "_ main __":

S = "zhizhi.liu@chinacache.com"

R = s

Mail_send (s, r)

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.