{"id":955043,"date":"2024-12-27T02:08:16","date_gmt":"2024-12-26T18:08:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/955043.html"},"modified":"2024-12-27T02:08:19","modified_gmt":"2024-12-26T18:08:19","slug":"python%e5%a6%82%e4%bd%95%e5%86%85%e5%ae%b9%e5%88%b0%e9%82%ae%e7%ae%b1","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/955043.html","title":{"rendered":"python\u5982\u4f55\u5185\u5bb9\u5230\u90ae\u7bb1"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095057\/b41a326e-9d56-4528-ab8f-861c9eda95a3.webp\" alt=\"python\u5982\u4f55\u5185\u5bb9\u5230\u90ae\u7bb1\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528smtplib\u5e93\u3001yagm<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l\u5e93\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9API\uff08\u5982SendGrid\u3001Mailgun\uff09\u7b49\u591a\u79cd\u65b9\u5f0f\u5c06\u5185\u5bb9\u53d1\u9001\u5230\u90ae\u7bb1<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528smtplib\u5e93\u662f\u6700\u4e3a\u57fa\u7840\u7684\u65b9\u6cd5\uff0c\u800cyagmail\u5e93\u5219\u63d0\u4f9b\u4e86\u66f4\u53cb\u597d\u7684\u63a5\u53e3\uff0c\u7b2c\u4e09\u65b9API\u5219\u9002\u5408\u53d1\u9001\u5927\u91cf\u90ae\u4ef6\u6216\u9700\u8981\u9ad8\u7ea7\u529f\u80fd\u7684\u573a\u666f\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u6765\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528SMTP\u53d1\u9001\u90ae\u4ef6<\/p>\n<\/p>\n<p><p>SMTP\uff08Simple Mail Transfer Protocol\uff09\u662f\u7535\u5b50\u90ae\u4ef6\u4f20\u8f93\u534f\u8bae\uff0cPython\u5185\u7f6e\u7684smtplib\u5e93\u63d0\u4f9b\u4e86\u4e0eSMTP\u670d\u52a1\u5668\u8fdb\u884c\u901a\u4fe1\u7684\u529f\u80fd\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u53d1\u9001\u7b80\u5355\u7684\u90ae\u4ef6\uff0c\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bbe\u7f6eSMTP\u670d\u52a1\u5668<\/strong><br \/>SMTP\u670d\u52a1\u5668\u662f\u7528\u4e8e\u53d1\u9001\u90ae\u4ef6\u7684\u670d\u52a1\u5668\uff0c\u6bcf\u4e2a\u7535\u5b50\u90ae\u4ef6\u670d\u52a1\u63d0\u4f9b\u5546\u90fd\u6709\u81ea\u5df1\u7684SMTP\u670d\u52a1\u5668\u3002\u6bd4\u5982\uff0cGmail\u7684SMTP\u670d\u52a1\u5668\u662f<code>smtp.gmail.com<\/code>\uff0c\u7aef\u53e3\u53f7\u662f587\uff08TLS\uff09\u6216465\uff08SSL\uff09\u3002\u5728\u4f7f\u7528smtplib\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u77e5\u9053\u60a8\u6240\u4f7f\u7528\u7684\u90ae\u4ef6\u670d\u52a1\u7684SMTP\u670d\u52a1\u5668\u5730\u5740\u53ca\u7aef\u53e3\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199\u53d1\u9001\u90ae\u4ef6\u7684Python\u4ee3\u7801<\/strong><br \/>\u4f7f\u7528smtplib\u5e93\uff0c\u60a8\u53ef\u4ee5\u7f16\u5199Python\u4ee3\u7801\u6765\u8fde\u63a5SMTP\u670d\u52a1\u5668\u5e76\u53d1\u9001\u90ae\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u4f7f\u7528Gmail\u7684SMTP\u670d\u52a1\u5668\u53d1\u9001\u90ae\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import smtplib<\/p>\n<p>from email.mime.text import MIMEText<\/p>\n<p>from email.mime.multipart import MIMEMultipart<\/p>\n<p>def send_email(subject, body, to_email):<\/p>\n<p>    # Gmail SMTP\u670d\u52a1\u5668\u5730\u5740\u53ca\u7aef\u53e3<\/p>\n<p>    smtp_server = &#39;smtp.gmail.com&#39;<\/p>\n<p>    smtp_port = 587<\/p>\n<p>    # \u53d1\u4ef6\u4eba\u7684\u90ae\u7bb1\u53ca\u5bc6\u7801<\/p>\n<p>    from_email = &#39;your_email@gmail.com&#39;<\/p>\n<p>    password = &#39;your_password&#39;<\/p>\n<p>    # \u521b\u5efaMIMEText\u5bf9\u8c61\uff0c\u4ee3\u8868\u90ae\u4ef6\u5185\u5bb9<\/p>\n<p>    msg = MIMEMultipart()<\/p>\n<p>    msg[&#39;From&#39;] = from_email<\/p>\n<p>    msg[&#39;To&#39;] = to_email<\/p>\n<p>    msg[&#39;Subject&#39;] = subject<\/p>\n<p>    # \u90ae\u4ef6\u6b63\u6587<\/p>\n<p>    msg.attach(MIMEText(body, &#39;plain&#39;))<\/p>\n<p>    try:<\/p>\n<p>        # \u8fde\u63a5\u5230SMTP\u670d\u52a1\u5668<\/p>\n<p>        server = smtplib.SMTP(smtp_server, smtp_port)<\/p>\n<p>        server.starttls()  # \u542f\u7528TLS\u52a0\u5bc6<\/p>\n<p>        server.login(from_email, password)  # \u767b\u5f55SMTP\u670d\u52a1\u5668<\/p>\n<p>        # \u53d1\u9001\u90ae\u4ef6<\/p>\n<p>        server.sendmail(from_email, to_email, msg.as_string())<\/p>\n<p>        print(&quot;Email sent successfully!&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Failed to send email: {e}&quot;)<\/p>\n<p>    finally:<\/p>\n<p>        server.quit()<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u53d1\u9001\u90ae\u4ef6<\/strong><\/h2>\n<p>send_email(&quot;Test Subject&quot;, &quot;This is the email body&quot;, &quot;recipient_email@example.com&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6ce8\u610f<\/strong>\uff1a\u4f7f\u7528Gmail\u53d1\u9001\u90ae\u4ef6\u65f6\uff0c\u60a8\u9700\u8981\u542f\u7528\u201c\u4e0d\u592a\u5b89\u5168\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u8bbf\u95ee\u201d\u8bbe\u7f6e\uff0c\u6216\u8005\u4f7f\u7528\u5e94\u7528\u4e13\u7528\u5bc6\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u90ae\u4ef6\u5185\u5bb9<\/strong><br \/>\u9664\u4e86\u53d1\u9001\u7b80\u5355\u7684\u6587\u672c\u90ae\u4ef6\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528MIME\uff08Multipurpose Internet Mail Extensions\uff09\u6765\u53d1\u9001HTML\u683c\u5f0f\u7684\u90ae\u4ef6\u3001\u9644\u4ef6\u7b49\u3002\u901a\u8fc7\u5c06<code>MIMEText<\/code>\u66ff\u6362\u4e3a<code>MIMEImage<\/code>\u6216<code>MIMEBase<\/code>\uff0c\u60a8\u53ef\u4ee5\u53d1\u9001\u56fe\u7247\u6216\u5176\u4ed6\u6587\u4ef6\u683c\u5f0f\u7684\u9644\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528Yagmail\u5e93<\/p>\n<\/p>\n<p><p>Yagmail\u662f\u4e00\u4e2a\u4e13\u4e3aGmail\u8bbe\u8ba1\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u7b80\u5316\u4e86\u4f7f\u7528Python\u53d1\u9001\u90ae\u4ef6\u7684\u8fc7\u7a0b\u3002\u5b83\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684\u63a5\u53e3\uff0c\u5e76\u652f\u6301HTML\u90ae\u4ef6\u3001\u9644\u4ef6\u3001\u6a21\u677f\u7b49\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Yagmail<\/strong><br \/>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5yagmail\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install yagmail<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199\u53d1\u9001\u90ae\u4ef6\u7684Python\u4ee3\u7801<\/strong><br \/>\u4f7f\u7528yagmail\u53d1\u9001\u90ae\u4ef6\u975e\u5e38\u7b80\u5355\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yagmail<\/p>\n<p>def send_email(subject, body, to_email):<\/p>\n<p>    # \u53d1\u4ef6\u4eba\u7684\u90ae\u7bb1\u53ca\u5bc6\u7801<\/p>\n<p>    from_email = &#39;your_email@gmail.com&#39;<\/p>\n<p>    password = &#39;your_password&#39;<\/p>\n<p>    # \u521b\u5efayagmail.SMTP\u5bf9\u8c61<\/p>\n<p>    yag = yagmail.SMTP(from_email, password)<\/p>\n<p>    try:<\/p>\n<p>        # \u53d1\u9001\u90ae\u4ef6<\/p>\n<p>        yag.send(to=to_email, subject=subject, contents=body)<\/p>\n<p>        print(&quot;Email sent successfully!&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Failed to send email: {e}&quot;)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u53d1\u9001\u90ae\u4ef6<\/strong><\/h2>\n<p>send_email(&quot;Test Subject&quot;, &quot;This is the email body&quot;, &quot;recipient_email@example.com&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Yagmail\u7684\u9ad8\u7ea7\u529f\u80fd<\/strong><br \/>Yagmail\u652f\u6301\u53d1\u9001HTML\u683c\u5f0f\u7684\u90ae\u4ef6\u3001\u6dfb\u52a0\u9644\u4ef6\u3001\u4f7f\u7528\u6a21\u677f\u7b49\u529f\u80fd\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u5c06<code>contents<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3aHTML\u5b57\u7b26\u4e32\u6765\u53d1\u9001HTML\u90ae\u4ef6\uff0c\u6216\u8005\u901a\u8fc7\u5c06\u9644\u4ef6\u8def\u5f84\u4f20\u9012\u7ed9<code>contents<\/code>\u6765\u53d1\u9001\u9644\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9API\u53d1\u9001\u90ae\u4ef6<\/p>\n<\/p>\n<p><p>\u5982\u679c\u60a8\u9700\u8981\u53d1\u9001\u5927\u91cf\u90ae\u4ef6\u6216\u9700\u8981\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd\uff08\u5982\u90ae\u4ef6\u8ddf\u8e2a\u3001\u7edf\u8ba1\u7b49\uff09\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7b2c\u4e09\u65b9\u90ae\u4ef6API\u670d\u52a1\uff0c\u5982SendGrid\u3001Mailgun\u3001Amazon SES\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684API\u670d\u52a1<\/strong><br \/>\u6839\u636e\u60a8\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684API\u670d\u52a1\u3002SendGrid\u548cMailgun\u662f\u6bd4\u8f83\u6d41\u884c\u7684\u9009\u62e9\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86Python SDK\uff0c\u7b80\u5316\u4e86\u4e0eAPI\u7684\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u83b7\u53d6API\u5bc6\u94a5<\/strong><br \/>\u6ce8\u518c\u5e76\u767b\u5f55\u6240\u9009\u7684API\u670d\u52a1\uff0c\u83b7\u53d6API\u5bc6\u94a5\u3002API\u5bc6\u94a5\u7528\u4e8e\u8ba4\u8bc1\u60a8\u7684\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199\u53d1\u9001\u90ae\u4ef6\u7684Python\u4ee3\u7801<\/strong><br \/>\u4ee5\u4e0b\u662f\u4f7f\u7528SendGrid API\u53d1\u9001\u90ae\u4ef6\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sendgrid<\/p>\n<p>from sendgrid.helpers.mail import Mail, Email, To, Content<\/p>\n<p>def send_email(subject, body, to_email):<\/p>\n<p>    # SendGrid API\u5bc6\u94a5<\/p>\n<p>    api_key = &#39;your_sendgrid_api_key&#39;<\/p>\n<p>    # \u521b\u5efaSendGrid\u5ba2\u6237\u7aef<\/p>\n<p>    sg = sendgrid.SendGridAPIClient(api_key=api_key)<\/p>\n<p>    # \u521b\u5efa\u90ae\u4ef6\u5bf9\u8c61<\/p>\n<p>    from_email = Email(&#39;your_email@example.com&#39;)<\/p>\n<p>    to_email = To(to_email)<\/p>\n<p>    content = Content(&#39;text\/plain&#39;, body)<\/p>\n<p>    mail = Mail(from_email, to_email, subject, content)<\/p>\n<p>    try:<\/p>\n<p>        # \u53d1\u9001\u90ae\u4ef6<\/p>\n<p>        response = sg.client.mail.send.post(request_body=mail.get())<\/p>\n<p>        print(f&quot;Email sent successfully! Status code: {response.status_code}&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Failed to send email: {e}&quot;)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u53d1\u9001\u90ae\u4ef6<\/strong><\/h2>\n<p>send_email(&quot;Test Subject&quot;, &quot;This is the email body&quot;, &quot;recipient_email@example.com&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406API\u54cd\u5e94\u548c\u9519\u8bef<\/strong><br \/>\u53d1\u9001\u90ae\u4ef6\u540e\uff0c\u60a8\u53ef\u4ee5\u68c0\u67e5API\u7684\u54cd\u5e94\u72b6\u6001\u7801\u548c\u4fe1\u606f\uff0c\u4ee5\u786e\u8ba4\u90ae\u4ef6\u662f\u5426\u6210\u529f\u53d1\u9001\uff0c\u5e76\u5904\u7406\u53ef\u80fd\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u5c06\u5185\u5bb9\u53d1\u9001\u5230\u90ae\u7bb1\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5bf9\u4e8e\u53d1\u9001\u7b80\u5355\u90ae\u4ef6\uff0csmtplib\u548cyagmail\u662f\u4e0d\u9519\u7684\u9009\u62e9\uff1b\u800c\u5bf9\u4e8e\u9700\u8981\u9ad8\u7ea7\u529f\u80fd\u6216\u6279\u91cf\u90ae\u4ef6\u53d1\u9001\u7684\u573a\u666f\uff0c\u4f7f\u7528\u7b2c\u4e09\u65b9API\u670d\u52a1\u53ef\u80fd\u66f4\u4e3a\u5408\u9002\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u786e\u4fdd\u90ae\u4ef6\u5b89\u5168\u6027\u548c\u9690\u79c1\u6027\u90fd\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\uff1f<\/strong><br \/>\u4f7f\u7528Python\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684<code>smtplib<\/code>\u5e93\u6765\u5b9e\u73b0\u3002\u60a8\u9700\u8981\u8bbe\u7f6eSMTP\u670d\u52a1\u5668\u7684\u5730\u5740\u548c\u7aef\u53e3\uff0c\u4f7f\u7528\u60a8\u7684\u90ae\u7bb1\u5e10\u6237\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\uff0c\u5e76\u6784\u5efa\u90ae\u4ef6\u7684\u5185\u5bb9\u548c\u4e3b\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import smtplib\nfrom email.mime.text import MIMEText\n\n# \u90ae\u4ef6\u5185\u5bb9\nmsg = MIMEText(&#39;\u8fd9\u662f\u90ae\u4ef6\u7684\u6b63\u6587\u5185\u5bb9&#39;)\nmsg[&#39;Subject&#39;] = &#39;\u90ae\u4ef6\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\u63a5\u5230SMTP\u670d\u52a1\u5668\nwith smtplib.SMTP(&#39;smtp.example.com&#39;, 587) as server:\n    server.starttls()  # \u542f\u7528TLS\u5b89\u5168\n    server.login(&#39;your_email@example.com&#39;, &#39;your_password&#39;)  # \u767b\u5f55\n    server.send_message(msg)  # \u53d1\u9001\u90ae\u4ef6\n<\/code><\/pre>\n<p><strong>\u9700\u8981\u54ea\u4e9b\u5e93\u624d\u80fd\u4f7f\u7528Python\u53d1\u9001\u90ae\u4ef6\uff1f<\/strong><br \/>\u57fa\u672c\u4e0a\uff0c\u60a8\u53ea\u9700\u8981<code>smtplib<\/code>\u548c<code>email<\/code>\u5e93\u3002<code>smtplib<\/code>\u7528\u4e8e\u5904\u7406SMTP\u534f\u8bae\uff0c\u800c<code>email<\/code>\u5e93\u7528\u4e8e\u6784\u5efa\u90ae\u4ef6\u7684\u5185\u5bb9\u548c\u683c\u5f0f\u3002\u6839\u636e\u60a8\u7684\u9700\u6c42\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982<code>yagmail<\/code>\u548c<code>Flask-Mail<\/code>\uff0c\u4ee5\u7b80\u5316\u53d1\u9001\u90ae\u4ef6\u7684\u8fc7\u7a0b\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u53d1\u9001\u90ae\u4ef6\u65f6\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f51\u7edc\u8fde\u63a5\u95ee\u9898\u3001\u8eab\u4efd\u9a8c\u8bc1\u5931\u8d25\u6216\u90ae\u4ef6\u5185\u5bb9\u683c\u5f0f\u9519\u8bef\u7b49\u95ee\u9898\u3002\u5efa\u8bae\u60a8\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\uff0c\u5e76\u76f8\u5e94\u5730\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\u6216\u5c1d\u8bd5\u91cd\u65b0\u53d1\u9001\u90ae\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u9519\u8bef\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    # \u53d1\u9001\u90ae\u4ef6\u7684\u4ee3\u7801\nexcept smtplib.SMTPException as e:\n    print(f&#39;\u90ae\u4ef6\u53d1\u9001\u5931\u8d25: {e}&#39;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728\u90ae\u4ef6\u4e2d\u6dfb\u52a0\u9644\u4ef6\uff1f<\/strong><br \/>\u8981\u5728\u90ae\u4ef6\u4e2d\u6dfb\u52a0\u9644\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>email.mime<\/code>\u6a21\u5757\u4e2d\u7684<code>MIMEBase<\/code>\u7c7b\u548c<code>encoders<\/code>\u6a21\u5757\u3002\u60a8\u9700\u8981\u521b\u5efa\u4e00\u4e2a<code>MIMEBase<\/code>\u5bf9\u8c61\uff0c\u8bfb\u53d6\u6587\u4ef6\u5e76\u5c06\u5176\u7f16\u7801\u4e3aBase64\u683c\u5f0f\uff0c\u7136\u540e\u5c06\u5176\u9644\u52a0\u5230\u90ae\u4ef6\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">from email.mime.base import MIMEBase\nfrom email import encoders\n\n# \u521b\u5efa\u90ae\u4ef6\u5bf9\u8c61\nmsg = MIMEMultipart()\nmsg[&#39;Subject&#39;] = &#39;\u542b\u9644\u4ef6\u7684\u90ae\u4ef6&#39;\nmsg[&#39;From&#39;] = &#39;your_email@example.com&#39;\nmsg[&#39;To&#39;] = &#39;recipient@example.com&#39;\n\n# \u6dfb\u52a0\u9644\u4ef6\nfile_path = &#39;path_to_your_file&#39;\nwith open(file_path, &#39;rb&#39;) as attachment:\n    part = MIMEBase(&#39;application&#39;, &#39;octet-stream&#39;)\n    part.set_payload(attachment.read())\n    encoders.encode_base64(part)\n    part.add_header(&#39;Content-Disposition&#39;, f&#39;attachment; filename={file_path}&#39;)\n    msg.attach(part)\n\n# \u53d1\u9001\u90ae\u4ef6\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528smtplib\u5e93\u3001yagmAIl\u5e93\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9API\uff08\u5982SendGrid\u3001Mailgu [&hellip;]","protected":false},"author":3,"featured_media":955047,"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\/955043"}],"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=955043"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/955043\/revisions"}],"predecessor-version":[{"id":955048,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/955043\/revisions\/955048"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/955047"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=955043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=955043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=955043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}