Python provides several common email sending Methods: pythonemail

Source: Internet
Author: User

Python provides several common email sending Methods: pythonemail

Anyone who has learned Python knows that it is relatively simple to use Python to send emails. 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, which is easy to implement, whether it is single, group, or CC.

This article records several of the simplest ways of sending emails, such as html emails and attachments. You can refer to this document when necessary. The specific method is as follows:

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-08-18 # author: orangleliu ''' use python to write emails. simple use the 126 email service ''' import smtplibfrom email. mime. text import MIMETextSMTPserver = 'smtp .126.com 'sender = '2017 @ 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)

The Code is as follows:

#-*-Coding: UTF-8-*-# python2.7x # send_email_by _. py # author: orangleliu # date: 2014-08-18 ''' 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 = "12345678@qq.com" r = "123456@163.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: ''' 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 = "123456@163.com" r = s mail_send (s, r)

I believe that the method shown in this article can provide some reference value for everyone to design Python programs.


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.

Email sending in python

Python sends an email to the QQ mail server. If there is a problem, modify the smtplib. py file.
Specific methods can be found on the Internet.

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.