{"id":1186767,"date":"2025-01-15T19:56:44","date_gmt":"2025-01-15T11:56:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1186767.html"},"modified":"2025-01-15T19:56:48","modified_gmt":"2025-01-15T11:56:48","slug":"python%e5%a6%82%e4%bd%95%e8%bf%9b%e8%a1%8c%e5%8f%91%e9%82%ae%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1186767.html","title":{"rendered":"python\u5982\u4f55\u8fdb\u884c\u53d1\u90ae\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25135500\/3e47b969-8844-4c13-bcf2-b96bb4057709.webp\" alt=\"python\u5982\u4f55\u8fdb\u884c\u53d1\u90ae\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u8fdb\u884c\u53d1\u90ae\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528smtplib\u6a21\u5757\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982yagm<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l\u3001\u4f7f\u7528Django\u7b49\u6846\u67b6\u81ea\u5e26\u7684\u90ae\u4ef6\u53d1\u9001\u529f\u80fd\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u7684\u5185\u7f6e\u6a21\u5757smtplib\uff0c<\/strong> \u8be5\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7SMTP\u534f\u8bae\u53d1\u9001\u90ae\u4ef6\uff0c\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u8bbe\u7f6eSMTP\u670d\u52a1\u5668\u3001\u767b\u5f55\u90ae\u7bb1\u3001\u6784\u5efa\u90ae\u4ef6\u5185\u5bb9\u4ee5\u53ca\u53d1\u9001\u90ae\u4ef6\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528smtplib\u53d1\u9001\u90ae\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001SMTP\u670d\u52a1\u5668\u7684\u8bbe\u7f6e\u548c\u767b\u5f55<\/p>\n<\/p>\n<p><p>1\u3001\u8bbe\u7f6eSMTP\u670d\u52a1\u5668<\/p>\n<p>\u8981\u53d1\u9001\u90ae\u4ef6\uff0c\u9996\u5148\u9700\u8981\u77e5\u9053\u6240\u7528\u90ae\u7bb1\u7684SMTP\u670d\u52a1\u5668\u5730\u5740\u548c\u7aef\u53e3\u53f7\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u90ae\u7bb1\u7684SMTP\u670d\u52a1\u5668\u5730\u5740\u548c\u7aef\u53e3\u53f7\uff1a<\/p>\n<\/p>\n<ul>\n<li>Gmail: smtp.gmail.com, \u7aef\u53e3\u53f7587\uff08TLS\uff09\u6216465\uff08SSL\uff09<\/li>\n<li>Outlook: smtp-mail.outlook.com, \u7aef\u53e3\u53f7587\uff08TLS\uff09<\/li>\n<li>Yahoo: smtp.mail.yahoo.com, \u7aef\u53e3\u53f7587\uff08TLS\uff09<\/li>\n<\/ul>\n<p><p>2\u3001\u767b\u5f55\u90ae\u7bb1<\/p>\n<p>\u4f7f\u7528smtplib\u6a21\u5757\u65f6\uff0c\u9996\u5148\u9700\u8981\u521b\u5efaSMTP\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528\u90ae\u7bb1\u8d26\u6237\u548c\u5bc6\u7801\u767b\u5f55\u5230SMTP\u670d\u52a1\u5668\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import smtplib<\/p>\n<h2><strong>\u521b\u5efaSMTP\u5bf9\u8c61<\/strong><\/h2>\n<p>smtp_obj = smtplib.SMTP(&#39;smtp.gmail.com&#39;, 587)<\/p>\n<p>smtp_obj.starttls()  # \u542f\u52a8TLS\u52a0\u5bc6\u6a21\u5f0f<\/p>\n<p>smtp_obj.login(&#39;your_email@gmail.com&#39;, &#39;your_password&#39;)  # \u767b\u5f55\u90ae\u7bb1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u6784\u5efa\u90ae\u4ef6\u5185\u5bb9<\/p>\n<\/p>\n<p><p>1\u3001\u90ae\u4ef6\u5934\u548c\u90ae\u4ef6\u4f53<\/p>\n<p>\u90ae\u4ef6\u7684\u5185\u5bb9\u5305\u62ec\u90ae\u4ef6\u5934\uff08\u5982\u53d1\u4ef6\u4eba\u3001\u6536\u4ef6\u4eba\u3001\u4e3b\u9898\u7b49\uff09\u548c\u90ae\u4ef6\u4f53\uff08\u5b9e\u9645\u7684\u90ae\u4ef6\u5185\u5bb9\uff09\u3002\u53ef\u4ee5\u4f7f\u7528email\u6a21\u5757\u6765\u6784\u5efa\u90ae\u4ef6\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u672c\u90ae\u4ef6\u7684\u6784\u5efa\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from email.mime.text import MIMEText<\/p>\n<h2><strong>\u6784\u5efa\u90ae\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>msg = MIMEText(&#39;This is the body of the email.&#39;, &#39;plain&#39;, &#39;utf-8&#39;)<\/p>\n<p>msg[&#39;From&#39;] = &#39;your_email@gmail.com&#39;<\/p>\n<p>msg[&#39;To&#39;] = &#39;recipient_email@gmail.com&#39;<\/p>\n<p>msg[&#39;Subject&#39;] = &#39;Test Email&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u53d1\u9001HTML\u683c\u5f0f\u7684\u90ae\u4ef6<\/p>\n<p>\u5982\u679c\u9700\u8981\u53d1\u9001HTML\u683c\u5f0f\u7684\u90ae\u4ef6\uff0c\u53ef\u4ee5\u5c06\u90ae\u4ef6\u7c7b\u578b\u4eceplain\u6539\u4e3ahtml\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">msg = MIMEText(&#39;&lt;html&gt;&lt;body&gt;&lt;h1&gt;Hello&lt;\/h1&gt;&lt;p&gt;This is an HTML email.&lt;\/p&gt;&lt;\/body&gt;&lt;\/html&gt;&#39;, &#39;html&#39;, &#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u53d1\u9001\u90ae\u4ef6<\/p>\n<\/p>\n<p><p>1\u3001\u53d1\u9001\u90ae\u4ef6<\/p>\n<p>\u4f7f\u7528SMTP\u5bf9\u8c61\u7684sendmail\u65b9\u6cd5\u53d1\u9001\u90ae\u4ef6\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">smtp_obj.sendmail(&#39;your_email@gmail.com&#39;, &#39;recipient_email@gmail.com&#39;, msg.as_string())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u5173\u95edSMTP\u8fde\u63a5<\/p>\n<p>\u5b8c\u6210\u90ae\u4ef6\u53d1\u9001\u540e\uff0c\u9700\u8981\u5173\u95edSMTP\u8fde\u63a5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">smtp_obj.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\u4e0a\u8ff0\u5185\u5bb9\uff0c\u5b8c\u6574\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import smtplib<\/p>\n<p>from email.mime.text import MIMEText<\/p>\n<h2><strong>\u8bbe\u7f6eSMTP\u670d\u52a1\u5668\u5730\u5740\u548c\u7aef\u53e3\u53f7<\/strong><\/h2>\n<p>smtp_server = &#39;smtp.gmail.com&#39;<\/p>\n<p>smtp_port = 587<\/p>\n<h2><strong>\u767b\u5f55\u90ae\u7bb1\u8d26\u6237\u548c\u5bc6\u7801<\/strong><\/h2>\n<p>email_account = &#39;your_email@gmail.com&#39;<\/p>\n<p>email_password = &#39;your_password&#39;<\/p>\n<h2><strong>\u521b\u5efaSMTP\u5bf9\u8c61\u5e76\u767b\u5f55<\/strong><\/h2>\n<p>smtp_obj = smtplib.SMTP(smtp_server, smtp_port)<\/p>\n<p>smtp_obj.starttls()<\/p>\n<p>smtp_obj.login(email_account, email_password)<\/p>\n<h2><strong>\u6784\u5efa\u90ae\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>msg = MIMEText(&#39;This is the body of the email.&#39;, &#39;plain&#39;, &#39;utf-8&#39;)<\/p>\n<p>msg[&#39;From&#39;] = email_account<\/p>\n<p>msg[&#39;To&#39;] = &#39;recipient_email@gmail.com&#39;<\/p>\n<p>msg[&#39;Subject&#39;] = &#39;Test Email&#39;<\/p>\n<h2><strong>\u53d1\u9001\u90ae\u4ef6<\/strong><\/h2>\n<p>smtp_obj.sendmail(email_account, &#39;recipient_email@gmail.com&#39;, msg.as_string())<\/p>\n<h2><strong>\u5173\u95edSMTP\u8fde\u63a5<\/strong><\/h2>\n<p>smtp_obj.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93yagmail\u53d1\u9001\u90ae\u4ef6<\/p>\n<\/p>\n<p><p>1\u3001\u5b89\u88c5yagmail<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5yagmail\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install yagmail<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u4f7f\u7528yagmail\u53d1\u9001\u90ae\u4ef6<\/p>\n<p>yagmail\u7b80\u5316\u4e86\u90ae\u4ef6\u7684\u53d1\u9001\u8fc7\u7a0b\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yagmail<\/p>\n<h2><strong>\u767b\u5f55\u90ae\u7bb1<\/strong><\/h2>\n<p>yag = yagmail.SMTP(&#39;your_email@gmail.com&#39;, &#39;your_password&#39;)<\/p>\n<h2><strong>\u53d1\u9001\u90ae\u4ef6<\/strong><\/h2>\n<p>yag.send(<\/p>\n<p>    to=&#39;recipient_email@gmail.com&#39;,<\/p>\n<p>    subject=&#39;Test Email&#39;,<\/p>\n<p>    contents=&#39;This is the body of the email.&#39;<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528Django\u6846\u67b6\u53d1\u9001\u90ae\u4ef6<\/p>\n<\/p>\n<p><p>1\u3001Django\u90ae\u4ef6\u8bbe\u7f6e<\/p>\n<p>\u5728Django\u9879\u76ee\u7684settings.py\u6587\u4ef6\u4e2d\uff0c\u914d\u7f6e\u90ae\u4ef6\u8bbe\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">EMAIL_BACKEND = &#39;django.core.mail.backends.smtp.EmailBackend&#39;<\/p>\n<p>EMAIL_HOST = &#39;smtp.gmail.com&#39;<\/p>\n<p>EMAIL_PORT = 587<\/p>\n<p>EMAIL_USE_TLS = True<\/p>\n<p>EMAIL_HOST_USER = &#39;your_email@gmail.com&#39;<\/p>\n<p>EMAIL_HOST_PASSWORD = &#39;your_password&#39;<\/p>\n<p>DEFAULT_FROM_EMAIL = EMAIL_HOST_USER<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u53d1\u9001\u90ae\u4ef6<\/p>\n<p>\u5728Django\u89c6\u56fe\u6216\u5176\u4ed6\u5730\u65b9\u4f7f\u7528send_mail\u51fd\u6570\u53d1\u9001\u90ae\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.core.mail import send_mail<\/p>\n<p>send_mail(<\/p>\n<p>    &#39;Subject here&#39;,<\/p>\n<p>    &#39;Here is the message.&#39;,<\/p>\n<p>    &#39;your_email@gmail.com&#39;,<\/p>\n<p>    [&#39;recipient_email@gmail.com&#39;],<\/p>\n<p>    fail_silently=False,<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u5f02\u5e38\u5904\u7406\u548c\u5b89\u5168\u6027<\/p>\n<\/p>\n<p><p>1\u3001\u5f02\u5e38\u5904\u7406<\/p>\n<p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u90ae\u4ef6\u53d1\u9001\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u51fa\u73b0\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u7f51\u7edc\u95ee\u9898\u3001\u8eab\u4efd\u9a8c\u8bc1\u5931\u8d25\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u8fdb\u884c\u5f02\u5e38\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    smtp_obj = smtplib.SMTP(&#39;smtp.gmail.com&#39;, 587)<\/p>\n<p>    smtp_obj.starttls()<\/p>\n<p>    smtp_obj.login(&#39;your_email@gmail.com&#39;, &#39;your_password&#39;)<\/p>\n<p>    smtp_obj.sendmail(&#39;your_email@gmail.com&#39;, &#39;recipient_email@gmail.com&#39;, msg.as_string())<\/p>\n<p>    smtp_obj.quit()<\/p>\n<p>except smtplib.SMTPException as e:<\/p>\n<p>    print(f&#39;Error: {e}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u4fdd\u62a4\u8d26\u6237\u4fe1\u606f<\/p>\n<p>\u4e3a\u4e86\u4fdd\u62a4\u90ae\u7bb1\u8d26\u6237\u548c\u5bc6\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u6216\u914d\u7f6e\u6587\u4ef6\u6765\u5b58\u50a8\u654f\u611f\u4fe1\u606f\uff0c\u800c\u4e0d\u662f\u5728\u4ee3\u7801\u4e2d\u660e\u6587\u5199\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>email_account = os.getenv(&#39;EMAIL_ACCOUNT&#39;)<\/p>\n<p>email_password = os.getenv(&#39;EMAIL_PASSWORD&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u53d1\u9001\u5e26\u9644\u4ef6\u7684\u90ae\u4ef6<\/p>\n<\/p>\n<p><p>1\u3001\u6784\u5efa\u5e26\u9644\u4ef6\u7684\u90ae\u4ef6<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528email.mime.multipart\u6a21\u5757\u6765\u521b\u5efa\u5e26\u9644\u4ef6\u7684\u90ae\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from email.mime.multipart import MIMEMultipart<\/p>\n<p>from email.mime.base import MIMEBase<\/p>\n<p>from email import encoders<\/p>\n<h2><strong>\u6784\u5efa\u90ae\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>msg = MIMEMultipart()<\/p>\n<p>msg[&#39;From&#39;] = &#39;your_email@gmail.com&#39;<\/p>\n<p>msg[&#39;To&#39;] = &#39;recipient_email@gmail.com&#39;<\/p>\n<p>msg[&#39;Subject&#39;] = &#39;Test Email with Attachment&#39;<\/p>\n<h2><strong>\u90ae\u4ef6\u6b63\u6587<\/strong><\/h2>\n<p>body = &#39;This is the body of the email.&#39;<\/p>\n<p>msg.attach(MIMEText(body, &#39;plain&#39;, &#39;utf-8&#39;))<\/p>\n<h2><strong>\u9644\u4ef6<\/strong><\/h2>\n<p>filename = &#39;path\/to\/attachment.txt&#39;<\/p>\n<p>attachment = open(filename, &#39;rb&#39;)<\/p>\n<p>part = MIMEBase(&#39;application&#39;, &#39;octet-stream&#39;)<\/p>\n<p>part.set_payload(attachment.read())<\/p>\n<p>encoders.encode_base64(part)<\/p>\n<p>part.add_header(&#39;Content-Disposition&#39;, f&#39;attachment; filename={filename}&#39;)<\/p>\n<p>msg.attach(part)<\/p>\n<h2><strong>\u53d1\u9001\u90ae\u4ef6<\/strong><\/h2>\n<p>smtp_obj = smtplib.SMTP(&#39;smtp.gmail.com&#39;, 587)<\/p>\n<p>smtp_obj.starttls()<\/p>\n<p>smtp_obj.login(&#39;your_email@gmail.com&#39;, &#39;your_password&#39;)<\/p>\n<p>smtp_obj.sendmail(&#39;your_email@gmail.com&#39;, &#39;recipient_email@gmail.com&#39;, msg.as_string())<\/p>\n<p>smtp_obj.quit()<\/p>\n<p>attachment.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001\u53d1\u9001\u591a\u79cd\u683c\u5f0f\u7684\u90ae\u4ef6<\/p>\n<\/p>\n<p><p>1\u3001\u53d1\u9001HTML\u548c\u7eaf\u6587\u672c\u683c\u5f0f\u7684\u90ae\u4ef6<\/p>\n<p>\u4e3a\u4e86\u786e\u4fdd\u90ae\u4ef6\u80fd\u5728\u5404\u79cd\u90ae\u4ef6\u5ba2\u6237\u7aef\u4e2d\u6b63\u5e38\u663e\u793a\uff0c\u53ef\u4ee5\u540c\u65f6\u53d1\u9001HTML\u548c\u7eaf\u6587\u672c\u683c\u5f0f\u7684\u90ae\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from email.mime.multipart import MIMEMultipart<\/p>\n<p>from email.mime.text import MIMEText<\/p>\n<h2><strong>\u6784\u5efa\u90ae\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>msg = MIMEMultipart(&#39;alternative&#39;)<\/p>\n<p>msg[&#39;From&#39;] = &#39;your_email@gmail.com&#39;<\/p>\n<p>msg[&#39;To&#39;] = &#39;recipient_email@gmail.com&#39;<\/p>\n<p>msg[&#39;Subject&#39;] = &#39;Test Email with HTML and Plain Text&#39;<\/p>\n<h2><strong>\u7eaf\u6587\u672c\u683c\u5f0f<\/strong><\/h2>\n<p>text = &#39;This is the plain text version of the email.&#39;<\/p>\n<p>part1 = MIMEText(text, &#39;plain&#39;, &#39;utf-8&#39;)<\/p>\n<h2><strong>HTML\u683c\u5f0f<\/strong><\/h2>\n<p>html = &#39;&lt;html&gt;&lt;body&gt;&lt;h1&gt;Hello&lt;\/h1&gt;&lt;p&gt;This is the HTML version of the email.&lt;\/p&gt;&lt;\/body&gt;&lt;\/html&gt;&#39;<\/p>\n<p>part2 = MIMEText(html, &#39;html&#39;, &#39;utf-8&#39;)<\/p>\n<h2><strong>\u5c06\u4e24\u79cd\u683c\u5f0f\u7684\u5185\u5bb9\u6dfb\u52a0\u5230\u90ae\u4ef6\u4e2d<\/strong><\/h2>\n<p>msg.attach(part1)<\/p>\n<p>msg.attach(part2)<\/p>\n<h2><strong>\u53d1\u9001\u90ae\u4ef6<\/strong><\/h2>\n<p>smtp_obj = smtplib.SMTP(&#39;smtp.gmail.com&#39;, 587)<\/p>\n<p>smtp_obj.starttls()<\/p>\n<p>smtp_obj.login(&#39;your_email@gmail.com&#39;, &#39;your_password&#39;)<\/p>\n<p>smtp_obj.sendmail(&#39;your_email@gmail.com&#39;, &#39;recipient_email@gmail.com&#39;, msg.as_string())<\/p>\n<p>smtp_obj.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5d\u3001\u6279\u91cf\u53d1\u9001\u90ae\u4ef6<\/p>\n<\/p>\n<p><p>1\u3001\u6279\u91cf\u53d1\u9001\u90ae\u4ef6<\/p>\n<p>\u5728\u9700\u8981\u5411\u591a\u4e2a\u6536\u4ef6\u4eba\u53d1\u9001\u90ae\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u6765\u6279\u91cf\u53d1\u9001\u90ae\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6536\u4ef6\u4eba\u5217\u8868<\/p>\n<p>recipients = [&#39;recipient1@gmail.com&#39;, &#39;recipient2@gmail.com&#39;, &#39;recipient3@gmail.com&#39;]<\/p>\n<h2><strong>\u5faa\u73af\u53d1\u9001\u90ae\u4ef6<\/strong><\/h2>\n<p>for recipient in recipients:<\/p>\n<p>    msg[&#39;To&#39;] = recipient<\/p>\n<p>    smtp_obj.sendmail(&#39;your_email@gmail.com&#39;, recipient, msg.as_string())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u4f7f\u7528send_message\u65b9\u6cd5<\/p>\n<p>smtplib\u63d0\u4f9b\u4e86send_message\u65b9\u6cd5\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f20\u9012MIME\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">smtp_obj.send_message(msg, from_addr=&#39;your_email@gmail.com&#39;, to_addrs=recipients)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u5185\u5bb9\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u4e86\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u53d1\u90ae\u4ef6\u7684\u591a\u79cd\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u5305\u62ec\u4f7f\u7528smtplib\u6a21\u5757\u3001\u7b2c\u4e09\u65b9\u5e93yagmail\u3001Django\u6846\u67b6\u81ea\u5e26\u529f\u80fd\uff0c\u4ee5\u53ca\u53d1\u9001\u5e26\u9644\u4ef6\u548c\u591a\u79cd\u683c\u5f0f\u7684\u90ae\u4ef6\u7b49\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u4e3a\u4f60\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u63d0\u4f9b\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684<code>smtplib<\/code>\u5e93\u6765\u5b9e\u73b0\u3002\u60a8\u9700\u8981\u63d0\u4f9bSMTP\u670d\u52a1\u5668\u7684\u5730\u5740\u548c\u7aef\u53e3\uff0c\u4ee5\u53ca\u60a8\u7684\u7535\u5b50\u90ae\u4ef6\u8d26\u6237\u548c\u5bc6\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import smtplib\nfrom email.mime.text import MIMEText\n\n# \u8bbe\u7f6e\u90ae\u4ef6\u5185\u5bb9\nmsg = MIMEText(&quot;\u8fd9\u662f\u4e00\u5c01\u6d4b\u8bd5\u90ae\u4ef6&quot;)\nmsg[&#39;Subject&#39;] = &#39;\u6d4b\u8bd5\u4e3b\u9898&#39;\nmsg[&#39;From&#39;] = &#39;your_email@example.com&#39;\nmsg[&#39;To&#39;] = &#39;recipient@example.com&#39;\n\n# \u8fde\u63a5SMTP\u670d\u52a1\u5668\u5e76\u53d1\u9001\u90ae\u4ef6\nwith smtplib.SMTP(&#39;smtp.example.com&#39;, 587) as server:\n    server.starttls()  # \u542f\u7528TLS\u52a0\u5bc6\n    server.login(&#39;your_email@example.com&#39;, &#39;your_password&#39;)\n    server.send_message(msg)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5c06<code>your_email@example.com<\/code>\u548c<code>your_password<\/code>\u66ff\u6362\u4e3a\u60a8\u81ea\u5df1\u7684\u7535\u5b50\u90ae\u4ef6\u4fe1\u606f\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u53d1\u9001\u90ae\u4ef6\u9700\u8981\u54ea\u4e9b\u5e93\uff1f<\/strong><br \/>\u53d1\u9001\u90ae\u4ef6\u7684\u57fa\u672c\u5e93\u662f<code>smtplib<\/code>\uff0c\u6b64\u5916\uff0c<code>email<\/code>\u5e93\u4e2d\u7684<code>MIMEText<\/code>\u7528\u4e8e\u6784\u5efa\u90ae\u4ef6\u5185\u5bb9\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u90ae\u4ef6\u5185\u5bb9\uff0c\u5982\u9644\u4ef6\u6216HTML\u683c\u5f0f\u90ae\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>email.mime<\/code>\u6a21\u5757\u4e2d\u7684\u5176\u4ed6\u7c7b\uff0c\u5982<code>MIMEBase<\/code>\u548c<code>MIMEImage<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406SMTP\u670d\u52a1\u5668\u7684\u8fde\u63a5\u95ee\u9898\uff1f<\/strong><br \/>\u8fde\u63a5SMTP\u670d\u52a1\u5668\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u786e\u4fddSMTP\u670d\u52a1\u5668\u5730\u5740\u548c\u7aef\u53e3\u53f7\u6b63\u786e\uff0c\u68c0\u67e5\u60a8\u7684\u7f51\u7edc\u8fde\u63a5\u662f\u5426\u6b63\u5e38\u3002\u5982\u679c\u9700\u8981\uff0c\u60a8\u8fd8\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u4ee5\u4fbf\u5728\u8fde\u63a5\u5931\u8d25\u65f6\u63d0\u4f9b\u66f4\u53cb\u597d\u7684\u9519\u8bef\u63d0\u793a\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    with smtplib.SMTP(&#39;smtp.example.com&#39;, 587) as server:\n        server.starttls()\n        server.login(&#39;your_email@example.com&#39;, &#39;your_password&#39;)\n        server.send_message(msg)\nexcept Exception as e:\n    print(f&quot;\u53d1\u9001\u90ae\u4ef6\u5931\u8d25: {e}&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u66f4\u597d\u5730\u4e86\u89e3\u8fde\u63a5\u95ee\u9898\u7684\u539f\u56e0\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u8c03\u6574\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8fdb\u884c\u53d1\u90ae\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528smtplib\u6a21\u5757\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982yagmAIl\u3001\u4f7f\u7528Django\u7b49\u6846\u67b6\u81ea [&hellip;]","protected":false},"author":3,"featured_media":1186768,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1186767"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1186767"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1186767\/revisions"}],"predecessor-version":[{"id":1186770,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1186767\/revisions\/1186770"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1186768"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1186767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1186767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1186767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}