如何创建向特定电子邮件地址发送 'Item in stock' 电子邮件的 smtplib 代码?
How do I create a smtplib code that sends an 'Item in stock' email to a specific email address?
使用 selenium 我有一个代码可以确定一个项目是 'in stock' 还是 'out of stock'。一旦 'out of stock' 项目变为 'in stock',代码就可以检测到。使用 smtplib,我希望从以下电子邮件地址发送一封电子邮件:
示例 1@hotmail.com
密码:1234
至:
示例2@hotmail.com
在项目 'in stock' 后通知个人。
完整的 python 代码是什么样的?
您可以尝试类似的方法:
用于发送纯文本电子邮件
import smtplib, ssl
from email.mime.text import MIMEText
port = 587 # For starttls
smtp_server = "smtp.office365.com"
sender_email = "the_sender_email_address" # example1@hotmail.om
password = "the_sender_email_address_password" # 1234
receiver_email = "the_receiver_email_address" # example2@hotmail.com
message = MIMEText("Hurry up.. Item in Stock Again") # Your message
message['Subject'] = "Item in Stock Again" # Email Subject
message['From'] = sender_email
message['To'] = receiver_email
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
server.ehlo() # Can be omitted
server.starttls(context=context)
server.ehlo() # Can be omitted
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message.as_string())
包括一些HTML内容
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
port = 587 # For starttls
smtp_server = "smtp.office365.com"
sender_email = "sender_email"
password = "sender_email_password"
receiver_email = "receiver_email"
message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = sender_email
message["To"] = receiver_email
# Create the plain-text and HTML version of your message
text = """\
Hurry Up"""
html = """\
<html>
<body>
<p>
<a href="http://www.realpython.com">Item</a>
is in stock again.
</p>
</body>
</html>
"""
# Turn these into plain/html MIMEText objects
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")
# Add HTML/plain-text parts to MIMEMultipart message
# The email client will try to render the last part first
message.attach(part1)
message.attach(part2)
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
server.starttls(context=context)
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message.as_string())
使用电子邮件模块添加附件
import email, smtplib, ssl
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
port = 587 # For starttls
smtp_server = "smtp.office365.com"
sender_email = "sender_email_address"
password = "sender_email_password"
receiver_email = "receiver_email_address"
subject = "Item in Stock "
body = "email with attachment"
# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
message["Bcc"] = receiver_email # Recommended for bul mails
# Add body to email
message.attach(MIMEText(body, "plain"))
filename = "some_document.pdf" # In same directory as the script
# Open PDF file in binary mode
with open(filename, "rb") as attachment:
# Add file as application/octet-stream
# Email client can usually download this automatically as attachment
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
# Encode file in ASCII characters to send by email
encoders.encode_base64(part)
# Add header as key/value pair to attachment part
part.add_header(
"Content-Disposition",
f"attachment; filename= {filename}",)
# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()
# Log in to server using secure context and send email
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
server.starttls(context=context)
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, text)
注意 :虽然上面的代码示例可以工作,但请记住它是旧代码,并不是您可以编写的用于使用 python 发送电子邮件的最佳代码smtplib
、email
。
您应该使用新的 EmailMessage/EmailPolicy
API(在 python 3.6 中引入)。
您可以在 python docs.
上找到更多示例
使用 selenium 我有一个代码可以确定一个项目是 'in stock' 还是 'out of stock'。一旦 'out of stock' 项目变为 'in stock',代码就可以检测到。使用 smtplib,我希望从以下电子邮件地址发送一封电子邮件:
示例 1@hotmail.com 密码:1234
至:
示例2@hotmail.com
在项目 'in stock' 后通知个人。
完整的 python 代码是什么样的?
您可以尝试类似的方法:
用于发送纯文本电子邮件
import smtplib, ssl from email.mime.text import MIMEText port = 587 # For starttls smtp_server = "smtp.office365.com" sender_email = "the_sender_email_address" # example1@hotmail.om password = "the_sender_email_address_password" # 1234 receiver_email = "the_receiver_email_address" # example2@hotmail.com message = MIMEText("Hurry up.. Item in Stock Again") # Your message message['Subject'] = "Item in Stock Again" # Email Subject message['From'] = sender_email message['To'] = receiver_email context = ssl.create_default_context() with smtplib.SMTP(smtp_server, port) as server: server.ehlo() # Can be omitted server.starttls(context=context) server.ehlo() # Can be omitted server.login(sender_email, password) server.sendmail(sender_email, receiver_email, message.as_string())
包括一些HTML内容
import smtplib, ssl from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart port = 587 # For starttls smtp_server = "smtp.office365.com" sender_email = "sender_email" password = "sender_email_password" receiver_email = "receiver_email" message = MIMEMultipart("alternative") message["Subject"] = "multipart test" message["From"] = sender_email message["To"] = receiver_email # Create the plain-text and HTML version of your message text = """\ Hurry Up""" html = """\ <html> <body> <p> <a href="http://www.realpython.com">Item</a> is in stock again. </p> </body> </html> """ # Turn these into plain/html MIMEText objects part1 = MIMEText(text, "plain") part2 = MIMEText(html, "html") # Add HTML/plain-text parts to MIMEMultipart message # The email client will try to render the last part first message.attach(part1) message.attach(part2) context = ssl.create_default_context() with smtplib.SMTP(smtp_server, port) as server: server.starttls(context=context) server.login(sender_email, password) server.sendmail(sender_email, receiver_email, message.as_string())
使用电子邮件模块添加附件
import email, smtplib, ssl from email import encoders from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText port = 587 # For starttls smtp_server = "smtp.office365.com" sender_email = "sender_email_address" password = "sender_email_password" receiver_email = "receiver_email_address" subject = "Item in Stock " body = "email with attachment" # Create a multipart message and set headers message = MIMEMultipart() message["From"] = sender_email message["To"] = receiver_email message["Subject"] = subject message["Bcc"] = receiver_email # Recommended for bul mails # Add body to email message.attach(MIMEText(body, "plain")) filename = "some_document.pdf" # In same directory as the script # Open PDF file in binary mode with open(filename, "rb") as attachment: # Add file as application/octet-stream # Email client can usually download this automatically as attachment part = MIMEBase("application", "octet-stream") part.set_payload(attachment.read()) # Encode file in ASCII characters to send by email encoders.encode_base64(part) # Add header as key/value pair to attachment part part.add_header( "Content-Disposition", f"attachment; filename= {filename}",) # Add attachment to message and convert message to string message.attach(part) text = message.as_string() # Log in to server using secure context and send email context = ssl.create_default_context() with smtplib.SMTP(smtp_server, port) as server: server.starttls(context=context) server.login(sender_email, password) server.sendmail(sender_email, receiver_email, text)
注意 :虽然上面的代码示例可以工作,但请记住它是旧代码,并不是您可以编写的用于使用 python 发送电子邮件的最佳代码smtplib
、email
。
您应该使用新的 EmailMessage/EmailPolicy
API(在 python 3.6 中引入)。
您可以在 python docs.