3 Methods for sending emails in Python

Source: Internet
Author: User
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, if you want to send emails in python, you can log on to the mail service to send emails. in linux, you can also use the sendmail command to send emails, you can also use the local or remote smtp service to send emails, which is easy to implement, whether it is single, group, or CC.
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.