Home  >  Article  >  Backend Development  >  How does Python send? Introduction to three ways to send email in python

How does Python send? Introduction to three ways to send email in python

不言
不言forward
2018-10-18 17:19:143371browse

The content of this article is about how PHP uses SwooleTaskWorker to implement asynchronous operation of Mysql (code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

There are three ways to send emails in Python, including logging in to the mail server, using the smtp service, and calling the sendmail command.

Sending emails in Python is relatively simple. You can log in to the mail service. To send, you can also use the sendmail command to send under Linux. You can also use local or remote smtp services to send emails. Whether it is single, group, or carbon copy, it is easier to implement. This Mipu blog first introduces a few of the simplest ways to send emails and records them. HTML emails, attachments, etc. are also supported. Just check the documentation when needed.

1. Log in to the mail server

Log in to the third-party smtp mailbox through smtp to send emails, supporting ports 25 and 465

vim python_email_1.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# author: mimvp.com
# 2015.10.05
import smtplib  
from email.mime.text import MIMEText  
smtpHost = 'smtp.exmail.qq.com' 
sender = 'robot@mimvp.com' 
password = "mimvp-password" 
receiver = 'yanggang@mimvp.com'
content = 'hello mimvp.com' 
msg = MIMEText(content)  
msg['Subject'] = 'email-subject' 
msg['From'] = sender  
msg['To'] = receiver  
## smtp port 25
smtpServer = smtplib.SMTP(smtpHost, 25)         # SMTP
smtpServer.login(sender, password)  
smtpServer.sendmail(sender, receiver, msg.as_string())  
smtpServer.quit()  
print 'send success by port 25' 
 
## smtp ssl port 465
smtpServer = smtplib.SMTP_SSL(smtpHost, 465)    # SMTP_SSL
smtpServer.login(sender, password)  
smtpServer.sendmail(sender, receiver, msg.as_string())  
smtpServer.quit()  
print 'send success by port 465'

Execute the command:

$ python python_email_1.py 
send success by port 25
send success by port 465

Send the result and you will receive two emails. Take a screenshot of one of the emails as shown below:

How does Python send? Introduction to three ways to send email in python

##2. Use smtp service

The test failed, skip it or leave a message to correct it

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# author: mimvp.com
# 2015.10.05
 
 
import smtplib  
from email.mime.text import MIMEText  
import subprocess
   
smtpHost = 'smtp.exmail.qq.com' 
sender = 'robot@mimvp.com' 
password = "mimvp-password" 
receiver = 'yanggang@mimvp.com'
   
content = 'hello mimvp.com' 
msg = MIMEText(content)   
   
   
 
if __name__ == "__main__":   
    p = subprocess.Popen(['/usr/sbin/sendmail', '-t'], stdout=subprocess.PIPE)  
    print(str(p.communicate()))
    p_res = str(p.communicate()[0])
    msg = MIMEText(p_res)
 
    msg["From"] = sender  
    msg["To"] = receiver  
    msg["Subject"] = "hello mimvp.com" 
    s = smtplib.SMTP(smtpHost)  
    s.login(sender, password)
    s.sendmail(sender, receiver, msg.as_string())  
    s.quit()  
    print 'send success'

3. Call the sendmail command

Call the native Linux sendmail service to send emails. There is no need to start the sendmail background process, and there is no need for the sender to log in. The email sender can be any name, without restrictions.

Special note: The sendmail command sends emails using port 25 by default. Since Alibaba Cloud, Tencent Cloud, etc. have blocked port 25, this example needs to be tested on a machine with port 25 enabled

vim python_email_3.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# author: mimvp.com
# 2015.10.05
  
  
from email.mime.text import MIMEText
from subprocess import Popen, PIPE
import commands
  
import sys 
reload(sys)
sys.setdefaultencoding('utf-8')
  
def send_mail(sender, recevier, subject, html_content):
        msg = MIMEText(html_content, 'html', 'utf-8')
        msg["From"] = sender
        msg["To"] = recevier
        msg["Subject"] = subject
        p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
        p.communicate(msg.as_string())
  
  
sender = 'robot@mimvp.com'
recevier = 'yanggang@mimvp.com'
subject = 'sendmail-subject'
html_content = 'hello mimvp.com'
send_mail(sender, recevier, subject, html_content)
Execute command:

python python_email_3.py
Receive result:

How does Python send? Introduction to three ways to send email in python

##

The above is the detailed content of How does Python send? Introduction to three ways to send email in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete

Related articles

See more