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 |
|
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 |
code> |
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 |
|
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: