From efca29960bae290a92103d95bdb21c02acb762a2 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Mon, 22 Mar 2021 16:39:48 -0400 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20SMTP=20=E9=82=AE=E4=BB=B6=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/SendMailSMTP.py | 72 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 tests/SendMailSMTP.py diff --git a/tests/SendMailSMTP.py b/tests/SendMailSMTP.py new file mode 100644 index 0000000..a05efef --- /dev/null +++ b/tests/SendMailSMTP.py @@ -0,0 +1,72 @@ +# -*- coding: utf-8 -*- + +# Python Send Email by SMTP +# Author - HoneyMoose(huyuchengus@gmail.com) +# Link Article - https://www.ossez.com/t/python/13398 + +import getpass +import smtplib +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText + +smtp_server_address = input("Please Enter SMTP Server address with port:\n") +smtp_server_user_name = input("Please Enter SMTP Server User Name:\n") +smtp_server_user_passwd = getpass.getpass() + +email_address_from = input("Email Address [FROM]:\n") +email_address_to = input("Email Address [TO]:\n") + +subject = input("What is your email subject?\n") +body_text = input("What message did you want to email? (currently this is only one line)\n\n") + +body_html = """\ + + + +

Using Python Sending Test Email!

+

+ """ + +body_html += body_text + +body_html += """

+ + + """ + + +def send_mail(to_addr, subject="Test email", + body_text="Test message", + body_html="Test message", + from_addr=email_address_from, + email_user=smtp_server_user_name, + email_passwd=smtp_server_user_passwd, + smtpserver="smtp.mailgun.org:587"): + """A function to send email, in MIME multi-part (plain-text and HTML). + + For example: to send to myself: + send_mail(to_addr, subject, body_text=body_text, body_html=body_html) + """ + + # Construct the message header + message = MIMEMultipart('alternative') + message['From'] = from_addr + message['To'] = to_addr + message['Subject'] = subject + + # Append the body text + message.attach(MIMEText(body_text, 'plain')) + message.attach(MIMEText(body_html, 'html')) + + # Connect to the SMTP server + server = smtplib.SMTP(smtpserver) + server.starttls() + server.login(email_user, email_passwd) + sending_response = server.sendmail(from_addr, to_addr, message.as_string()) + print(sending_response) + server.quit() + + +send_mail(email_address_to, subject, body_text, body_html, email_address_from, smtp_server_user_name, + smtp_server_user_passwd, + smtp_server_address) From 5c5ee83ab8432d5c4132a77606ce4f4097f9faee Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Mon, 22 Mar 2021 16:42:41 -0400 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=8F=91=E9=80=81?= =?UTF-8?q?=E9=82=AE=E4=BB=B6=E6=9C=8D=E5=8A=A1=E5=99=A8=E7=9A=84=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=9D=A5=E8=BF=9B=E8=A1=8C=E9=85=8D=E7=BD=AE=E5=86=85?= =?UTF-8?q?=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/SendMailSMTP.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/SendMailSMTP.py b/tests/SendMailSMTP.py index a05efef..f687335 100644 --- a/tests/SendMailSMTP.py +++ b/tests/SendMailSMTP.py @@ -9,21 +9,21 @@ import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText -smtp_server_address = input("Please Enter SMTP Server address with port:\n") -smtp_server_user_name = input("Please Enter SMTP Server User Name:\n") +smtp_server_address = input("请输入 SMTP 邮件服务器地址和端口:\n") +smtp_server_user_name = input("请输入 SMTP 邮件服务器的登录用户名:\n") smtp_server_user_passwd = getpass.getpass() -email_address_from = input("Email Address [FROM]:\n") -email_address_to = input("Email Address [TO]:\n") +email_address_from = input("发送的邮件地址 [FROM]:\n") +email_address_to = input("接收的邮件地址 [TO]:\n") -subject = input("What is your email subject?\n") -body_text = input("What message did you want to email? (currently this is only one line)\n\n") +subject = input("请输入你的邮件主题\n") +body_text = input("请输入你希望发送的内容(所有内容将会显示为一行))\n\n") body_html = """\ -

Using Python Sending Test Email!

+

使用 Python 发送的电子邮件!

""" From 0ca7be619127cb9e048f3fca23a447cd8fb0a3b2 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Tue, 23 Mar 2021 10:11:34 -0400 Subject: [PATCH 3/3] =?UTF-8?q?=E5=8F=91=E9=80=81=E9=82=AE=E4=BB=B6?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/SendMailSMTP.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/SendMailSMTP.py b/tests/SendMailSMTP.py index f687335..f188d87 100644 --- a/tests/SendMailSMTP.py +++ b/tests/SendMailSMTP.py @@ -42,9 +42,9 @@ def send_mail(to_addr, subject="Test email", email_user=smtp_server_user_name, email_passwd=smtp_server_user_passwd, smtpserver="smtp.mailgun.org:587"): - """A function to send email, in MIME multi-part (plain-text and HTML). + """发送邮件的方法,可以使用这个方法发送纯文本或者 HTML 的邮件。 - For example: to send to myself: + 例如,如果你希望发送一个邮件给你的自己,你可以使用: send_mail(to_addr, subject, body_text=body_text, body_html=body_html) """