Python教程

Python学习笔记-发送一个邮件通知

本文主要是介绍Python学习笔记-发送一个邮件通知,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

获取当前机器IP地址并发信通知

# If you want to use this python script,
# please follow the instructions below.
#   1. Make sure you fill in the correct SMTP server domain
#   2. Make sure that the SMTP service is enabled in you email address (send-side).
#   3. Make sure you fill in the correct SMTP token about your email address (send-side).

import smtplib
import requests
from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# Set Email server and address info
smtp_server_domain = r'Please fill in your SMTP server domain here'

from_address = r'Please fill in the sender email address here'
from_address_smtp_token = r'Please fill in the sender email SMTP token here'

to_address = r'Please fill in the recipient email address here'

# Request below url to get public IP address
request_time = datetime.now()

request_url = 'http://members.3322.org/dyndns/getip'
public_ip_address = requests.get(request_url).text.strip()

# Organize Email message
email_msg = MIMEMultipart()

email_msg['From'] = from_address
email_msg['To'] = to_address
email_msg['Subject'] = r'{} Public IP Address is {}'.format(request_time, public_ip_address)

email_msg_body = r'{} Public IP Address is {}'.format(request_time, public_ip_address)
email_msg.attach(MIMEText(email_msg_body, 'plain'))

# Sent Email
email_server = smtplib.SMTP(smtp_server_domain, 25)
email_server.starttls()
email_server.login(from_address, from_address_smtp_token)
email_server.sendmail(from_address, to_address, email_msg.as_string())
email_server.quit()

# Echo execute result
print('Service Execute Success!\nService Name: Get Public IP Address\nExecute Time: {}'.format(datetime.now()))

这篇关于Python学习笔记-发送一个邮件通知的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!