微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐