[Python] email sending Methods

Source: Internet
Author: User

[Python] email sending Methods

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 email service
#! /Usr/bin/env python #-*-coding: UTF-8-*-# python2.7x # send_simple_email_by_account.py @ 2014-07-30 # author: orangleliu ''' use python to write emails. simple use the 126 email service ''' import smtplibfrom email. mime. text import MIMETextSMTPserver = 'smtp .126.com 'sender = 'liuzhizhi123 @ 126.com' password = "xxxx" message = 'I send a message by Python. hello, 'msg = MIMEText (message) msg ['subobject'] = 'test Email by python' msg ['from'] = sendermsg ['to'] = destinationmailserver = 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)
#-*-Coding: UTF-8-*-# python2.7x # send_email_by _. py # author: orangleliu # date: 2014-08-15 ''' the mail may not be sent when using the sendmail command. You may need to change ''' from email to the hostname configuration. mime. text import MIMETextfrom subprocess import Popen, PIPEdef 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)
#! /Usr/bin/env python #-*-coding: UTF-8-*-# python2.7x # send_email_by_smtp.py # author: orangleliu # date: ''' in linux, you must enable the smtp service before using the local smtp service to send emails, check Method # ps-ef | grep sendmail # When telnet localhost 25, the email can still be sent. You may need to change the hostname configuration ''' import smtplibfrom email. mime. text import MIMETextfrom subprocess import Popen, PIPEdef 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)


This article is from the "orangleliu notebook" blog, please be sure to keep this http://blog.csdn.net/orangleliu/article/details/38591513


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.