[Python] Several email sending methods, pythonemail
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
How does python send emails?
To send an email to python, you can use the python email library.
Here is a python Method for sending emails by calling system commands.
First, you need to call the system command, load the python OS library, and then OS. system () can execute the shell command.
For example
Import OS
OS. system ("mkdir newdoc ")
You can call "mail" to send normal mails, and call "sendmail" to send multimedia files. For more information, see related documents.
╬ Ython has been transferred €20.mail has