做外链选择那些网站,怎样看出一个网站是那个公司做的,深圳网站建设找哪家公司,文创产品设计展板用Python发送电子邮件
你可以用 Python 发送邮件#xff0c;使用多个库#xff0c;但最常见的是 smtplib 和 email。
Python 中的“smtplib”模块定义了一个 SMTP 客户端会话对象#xff0c;可用于向任何带有 SMTP 或 ESMTP 监听器守护进程的互联网机器发送邮件。电子邮件…用Python发送电子邮件你可以用 Python 发送邮件使用多个库但最常见的是smtplib和email。Python 中的“smtplib”模块定义了一个 SMTP 客户端会话对象可用于向任何带有 SMTP 或 ESMTP 监听器守护进程的互联网机器发送邮件。电子邮件“包”是一个用于管理电子邮件的库包括MIME和其他基于RFC 2822的消息文档。处理和传递电子邮件的应用程序称为“邮件服务器”。简单邮件传输协议SMTP是一种协议用于在邮件服务器之间发送电子邮件和路由邮件。它是电子邮件传输的互联网标准。Python smptlib.SMTP() 功能Python smptlib.SMTP() 函数用于创建 SMTP 客户端会话对象建立与 SMTP 服务器的连接。这种连接允许你通过服务器发送邮件。搭建SMTP服务器在发送邮件之前你需要搭建一个SMTP服务器。常见的SMTP服务器包括Gmail、Yahoo或其他邮件服务提供商提供的服务器。创建 SMTP 对象发送电子邮件时你需要通过以下函数获得SMTP类的对象——import smtplib smtpObj smtplib.SMTP( [host [, port [, local_hostname]]] )以下是参数的详细情况 −host− 这是运行你 SMTP 服务器的主机。你可以指定主机的IP地址或域名比如tutorialspoint.com。这是一个可选的论点。port− 如果你提供主机参数则需要指定一个端口其中SMTP服务器正在监听。通常这个端口是25。local_hostname− 如果你的SMTP服务器运行在本地机器上那么你可以选择只用localhost作为选项。示例以下脚本连接到端口25的“smtp.example.com”SMTP服务器可选地识别并保护连接登录如有需要发送邮件然后退出会话−import smtplib # Create an SMTP object and establish a connection to the SMTP server smtpObj smtplib.SMTP(smtp.example.com, 25) # Identify yourself to an ESMTP server using EHLO smtpObj.ehlo() # Secure the SMTP connection smtpObj.starttls() # Login to the server (if required) smtpObj.login(username, password) # Send an email from_address your_emailexample.com to_address recipientexample.com message \ Subject: Test Email This is a test email message. smtpObj.sendmail(from_address, to_address, message) # Quit the SMTP session smtpObj.quit()Python smtpd 模块Pythonsmtpd模块用于创建和管理一个简单的邮件传输协议SMTP服务器。该模块允许您搭建一个SMTP服务器能够接收和处理来电邮件因此在测试和调试应用内的电子邮件功能时非常有价值。搭建 SMTP 调试服务器smtpd 模块预装了 Python并包含本地 SMTP 调试服务器。该服务器适合测试电子邮件功能而无需实际发送邮件到指定地址;相反它会将邮件内容打印到控制台。运行该本地服务器消除了处理消息加密或使用凭证登录邮件服务器的需求。启动 SMTP 调试服务器您可以使用命令提示符或终端中的以下命令启动本地 SMTP 调试服务器 −python -m smtpd -c DebuggingServer -n localhost:1025示例以下示例演示如何利用 smtplib 功能与本地 SMTP 调试服务器一起发送假邮件 −import smtplib def prompt(prompt): return input(prompt).strip() fromaddr prompt(From: ) toaddrs prompt(To: ).split() print(Enter message, end with ^D (Unix) or ^Z (Windows):) # Add the From: and To: headers at the start! msg (From: %s\r\nTo: %s\r\n\r\n % (fromaddr, , .join(toaddrs))) while True: try: line input() except EOFError: break if not line: break msg msg line print(Message length is, len(msg)) server smtplib.SMTP(localhost, 1025) server.set_debuglevel(1) server.sendmail(fromaddr, toaddrs, msg) server.quit()在此例中 −From− 你输入发送者的电子邮件地址fromaddr。To− 你输入收件人的电子邮件地址toaddrs可以是多个用空格分隔的地址。Message− 你输入消息内容结尾为 ^DUnix或 ^ZWindows。sendmail 方法中的“smtplib”会将邮件发送到运行于“localhost”1025端口的本地 SMTP 调试服务器使用指定的发送人、收件人和消息内容。输出当你运行程序时控制台会输出程序和SMTP服务器之间的通信。与此同时运行SMTPD服务器的终端会显示收到的邮件内容帮助你调试和验证邮件发送过程。python example.py From: abcxyz.com To: xyzabc.com Enter message, end with ^D (Unix) or ^Z (Windows): Hello World ^Z控制台反映的日志如下 −From: abcxyz.com reply: retcode (250); Msg: bOK send: rcpt TO:xyzabc.com\r\n reply: b250 OK\r\n reply: retcode (250); Msg: bOK send: data\r\n reply: b354 End data with CRLF.CRLF\r\n reply: retcode (354); Msg: bEnd data with CRLF.CRLF data: (354, bEnd data with CRLF.CRLF) send: bFrom: abcxyz.com\r\nTo: xyzabc.com\r\n\r\nHello World\r\n.\r\n reply: b250 OK\r\n reply: retcode (250); Msg: bOK data: (250, bOK) send: quit\r\n reply: b221 Bye\r\n reply: retcode (221); Msg: bByeSMTPD服务器运行的终端显示该输出−---------- MESSAGE FOLLOWS ---------- bFrom: abcxyz.com bTo: xyzabc.com bX-Peer: ::1 b bHello World ------------ END MESSAGE ------------使用 Python 发送 HTML 邮件要用 Python 发送 HTML 邮件你可以使用smtplib库连接到 SMTP 服务器使用email.mime模块来合理构建和格式化你的邮件内容。构建HTML邮件消息发送HTML邮件时你需要指定特定的头部并相应结构化消息内容以确保收件人的邮件客户端识别并呈现为HTML。示例以下是在 Python 中以电子邮件形式发送 HTML 内容的示例 −import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText # Email content sender fromfromdomain.com receivers [totodomain.com] # Create message container - the correct MIME type is multipart/alternative. msg MIMEMultipart(alternative) msg[From] From Person fromfromdomain.com msg[To] To Person totodomain.com msg[Subject] SMTP HTML e-mail test # HTML message content html \ html head/head body pThis is an e-mail message to be sent in bHTML format/b/p pbThis is HTML message./b/p h1This is headline./h1 /body /html # Attach HTML content to the email part2 MIMEText(html, html) msg.attach(part2) # Connect to SMTP server and send email try: smtpObj smtplib.SMTP(localhost) smtpObj.sendmail(sender, receivers, msg.as_string()) print(Successfully sent email) except smtplib.SMTPException as e: print(fError: unable to send email. Error message: {str(e)})以电子邮件发送附件要在 Python 中发送电子邮件附件您可以使用smtplib库连接到 SMTP 服务器使用email.mime模块来构建和格式化您的邮件内容包括附件。构建带有附件的电子邮件发送带有附件的邮件时你需要使用MIME多用途互联网邮件扩展正确格式化邮件。这需要将Content-Type头设置为multipart/mixed表示邮件包含文本和附件。邮件的每个部分文本和附件都被边界分隔开。示例以下是示例发送带有文件/tmp/test.txt作为附件的电子邮件 −import smtplib import base64 filename /tmp/test.txt # Read a file and encode it into base64 format fo open(filename, rb) filecontent fo.read() encodedcontent base64.b64encode(filecontent) # base64 sender webmastertutorialpoint.com reciever amrood.admingmail.com marker AUNIQUEMARKER body This is a test email to send an attachment. # Define the main headers. part1 From: From Person mefromdomain.net To: To Person amrood.admingmail.com Subject: Sending Attachment MIME-Version: 1.0 Content-Type: multipart/mixed; boundary%s --%s % (marker, marker) # Define the message action part2 Content-Type: text/plain Content-Transfer-Encoding:8bit %s --%s % (body,marker) # Define the attachment section part3 Content-Type: multipart/mixed; name\%s\ Content-Transfer-Encoding:base64 Content-Disposition: attachment; filename%s %s --%s-- %(filename, filename, encodedcontent, marker) message part1 part2 part3 try: smtpObj smtplib.SMTP(localhost) smtpObj.sendmail(sender, reciever, message) print Successfully sent email except Exception: print Error: unable to send email使用 Gmail 的 SMTP 服务器发送电子邮件要用 Gmail 的 Python SMTP 服务器发送邮件你需要在 “587” 端口smtp.gmail.com设置连接使用“TLS”加密使用 Gmail 凭证进行认证使用 Python 的 smtplib 和 email.mime 库构建邮件然后使用 sendmail 方法发送。最后用 quit 关闭 SMTP 连接。示例以下是一个示例脚本演示如何使用Gmail的SMTP服务器发送电子邮件 −import smtplib # Email content content Hello World # Set up SMTP connection to Gmails SMTP server mail smtplib.SMTP(smtp.gmail.com, 587) # Identify yourself to the SMTP server mail.ehlo() # Start TLS encryption for the connection mail.starttls() # Gmail account credentials sender your_emailgmail.com password your_password # Login to Gmails SMTP server mail.login(sender, password) # Email details recipient recipient_emailexample.com subject Test Email # Construct email message with headers header fTo: {recipient}\nFrom: {sender}\nSubject: {subject}\n content header content # Send email mail.sendmail(sender, recipient, content) # Close SMTP connection mail.quit()在运行上述脚本之前发件人的 Gmail 账户必须配置为允许访问“更少” 安全应用。点击以下链接。https://myaccount.google.com/lesssecureapps 将显示的切换按钮设置为开启。如果一切顺利执行上述脚本。消息应被送达到收件人的收件箱。