1024programmer Blog Three ways to send email with Python

Three ways to send email with Python

Three ways for Python to send emails are to log in to the mail server, use the smtp service, and call the sendmail command to send three methods

For the original text, please refer to the Mipu blog: Three ways of Python to send email

Python sending email is relatively simple, you can send it by logging in to the mail service, you can also use the sendmail command to send it under linux, and you can also use the local or remote smtp service to send emails, whether it is single, group, or copied Delivery is easier to achieve. This Mipu blog first introduces a few of the simplest ways to send emails and record them, such as html emails, attachments, etc. are also supported, and you can check the documents when needed.

1. Log in to the mail server

Log in to the third-party smtp mailbox to send mail through smtp, support port 25 and 465

vim python_email_1.py

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

>

32

33

34

35

#!/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 = '[email protected]'

password = "mimvp-password"

receiver= '[email protected]'

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

After sending the results, you will receive two emails, one of which is screenshotted as shown below:

Second, use smtp service

The test failed, skip or leave a message to correct

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

>

32

33

34

35

#!/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 = '[email protected]'

password = "mimvp-password"

receiver = '[email protected]'

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()

printprint

code> 'send success'

3. Call the sendmail command

Call the local linux’s own sendmail service to send emails, no need to start the sendmail background process, no need for the sender to log in, the sender can be any name, there is no limit.

Special attention: the sendmail command sends emails, and the port number 25 is used by default. Since the port number 25 is blocked by Alibaba Cloud and Tencent Cloud, this example needs to be tested on a machine with port 25 enabled

vim python_email_3.py

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

#!/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 = '[email protected]'

recevier = '[email protected] '

subject = 'sendmail-subject'

html_content = 'hello mimvp.com'

send_mail(sender, recevier, subject, html_content)

Execute the command:

python python_email_3.py

Receipt results:

in=PIPE)

p.communicate(msg.as_string())

sender = '[email protected]'

recevier = '[email protected]'

subject = 'sendmail-subject'

html_content = 'hello mimvp.com'

send_mail(sender, recevier, subject, html_content)

Execute the command:

python python_email_3.py

Receipt results:

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/three-ways-to-send-email-with-python/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索