Python implements several common methods for sending emails

  • 2020-04-02 13:54:29
  • OfStack

Studied Python knows, practical Python implementation send email function is relatively simple, can be sent through login E-mail service, under Linux to invoke the sendmail command can also be used to send, you can also use the local or remote SMTP service to send mail, whether individual, mass, or cc is easy to implement.

This article will be a few of the simplest way to send mail down, like HTML mail, attachment is also supported, the reader can refer to the query when needed. Specific methods are as follows:

1. Log in to the mail service

The specific 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 email  simple
 use 126  Mail service 
'''

import smtplib
from email.mime.text import MIMEText

SMTPserver = 'smtp.126.com'
sender = '12345678@126.com'
password = "xxxx"

message = 'I send a message by Python.  hello '
msg = MIMEText(message)

msg['Subject'] = '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. Calling the sendmail command (Linux)

The specific code is as follows:


# -*- coding: utf-8 -*-
#python2.7x
#send_email_by_.py
#author: orangleliu
#date: 2014-08-18
'''
 Using a sendmail Mode of command 

 I don't know if the email can be sent at this time. hostname The configuration may need to change 
'''

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 = "12345678@qq.com"
  r = "123456@163.com"
  mail_send(s, r)

3 use SMTP service to send (local or remote server)

The specific code is as follows:


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#python2.7x
#send_email_by_smtp.py
#author: orangleliu
#date: 2014-08-18
'''
linux  Now use local smtp Service to send mail 
 The premise has to be turned on smtp Services, methods of inspection 
#ps -ef|grep sendmail
#telnet localhost 25

 I don't know if the email can be sent at this time. hostname The configuration may need to change 
'''
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 = "123456@163.com"
  r = s
  mail_send(s, r)

I believe that the method presented in this paper can play a certain reference value for the Python programming.


Related articles: