[英]How does one send an e-mail from python NOT using gmail?
我已經有了用 python 發送電子郵件的代碼:
def send_email_gmail(subject, message, destination):
""" Send an e-mail using gmail with message to destination email.
Arguments:
message {str} -- message string to send.
destination {str} -- destination email (as string)
"""
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
# not a real email account nor password, its all ok!
server.login('me123@gmail.com', 'fakepassword111!!!')
# craft message
msg = EmailMessage()
message = f'{message}\n'
msg.set_content(message)
msg['Subject'] = subject
msg['From'] = 'me123@gmail.com'
msg['To'] = destination
# send msg
server.send_message(msg)
我已經閱讀了解決常見錯誤的多個問題( 登錄憑據不使用 Gmail SMTP或SMTPAuthenticationError 當使用 gmail 和 python 發送郵件時):
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sadfdgjsfgrp=1&dsfgscc=1dsdfgsfg&pldsfgt=AKsdfsdfggsdfggnsbu\n5.7.14 G0crCr0qSvWTng9xRE_pd3WnK3S2sDMsdfgsdfgX0J-xoetn7aHyFQi2qYrQisdfgsdfgKIwMCcgD7zLB1t7Z\n5.7.14 -OjHjpJqasdftBuTi9wh0sYlNW637SmPLuMnnLGn_WcZX5TGH4sddsfgXYar-Aasdfw0ctWfLhasdffPQV>\n5.7.14 Please log in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/787521345364524 n21sm17577sadfdsf46qtn.17 - gsmtp')
無論如何,我按照這些答案的建議做了,但我仍然遇到錯誤。 所以我決定不再為此使用 gmail 。 我從一個假賬戶發送電子郵件只是為了發送電子郵件,所以它的安全性對我來說並不重要。
那么如何更改上面的代碼,使其適用於在 python/code 中發送電子郵件更可靠的不同電子郵件服務?
想法答案將包含自包含並包含一個有效的示例腳本。
我當然檢查在我的假 gmail 上打開不太安全的應用程序功能,復制該頁面內容的粘貼文本:
Turn off less secure app access
Your account is vulnerable to malicious activity because you’re allowing apps & devices that use less secure sign-in technology to access your account. You should turn off this type of access. Google will automatically turn this setting OFF if it’s not being used. Learn more
還有一個黃色感嘆號警告我。
EmailMessage()
輸出:
建議我粘貼這個(空消息)。
我發現連接到 google 的 SMTP 服務器的最可靠方法是通過應用程序密碼。
import smtplib
from email.message import EmailMessage
def send_email_gmail(subject, message, destination):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
#This is where you would replace your password with the app password
server.login('account@gmail.com', 'App_Password')
msg = EmailMessage()
message = f'{message}\n'
msg.set_content(message)
msg['Subject'] = subject
msg['From'] = 'me123@gmail.com'
msg['To'] = destination
server.send_message(msg)
send_email_gmail('Test subject', 'This is the message', 'to_email@email.com')
希望這可以幫助!
我有一個用於通過雅虎發送電子郵件的功能。 您也不需要pip install
任何東西。
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_email(subject, to_email, message):
password = "some_password" # needs to be changed
my_email = "some_email@yahoo.com" # needs to be changed
smtp_obj = smtplib.SMTP('smtp.mail.yahoo.com', 587)
smtp_obj.starttls()
smtp_obj.ehlo()
smtp_obj.login(my_email, password)
msg = MIMEMultipart()
msg['From'] = my_email
msg['To'] = to_email
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain'))
smtp_obj.sendmail(msg['From'], msg['To'], msg.as_string())
smtp_obj.quit()
if __name__ == '__main__':
send_email('Subject: Automated e-mail', 'my_test_email@test.com', 'This was sent via script')
對於只有主題和正文的電子郵件來說,這顯然是最基本的格式,但您可以做的還有很多。 如果您想通過標准庫中的電子郵件和smtplib文檔獲得更高級的閱讀。
對我來說,當我使用 SSL 登錄時,它按預期工作,如下所示:
import smtplib, ssl
from email.mime.text import MIMEText
def send_email_gmail(subject, message, destination):
# First assemble the message
msg = MIMEText(message, 'plain')
msg['Subject'] = subject
# Login and send the message
port = 465
my_mail = 'me123@gmail.com'
my_password = 'fakepassword111!!!'
context = ssl.create_default_context()
with smtplib.SMTP_SSL('smtp.gmail.com', port, context=context) as server:
server.login(my_mail, my_password)
server.sendmail(my_mail, destination, msg.as_string())
send_email_gmail('Test subject', 'This is the message', 'to_email@email.com')
編輯:
但是,如果您真的想使用另一個 smtp 服務器,您可以使用 Outlook。 通過在端口 587 上連接到 smtp-mail.outlook.com,我可以在沒有 SSL 的情況下使用它,如下所示:
def send_email_gmail(subject, message, destination):
# First assemble the message
msg = MIMEText(message, 'plain')
msg['Subject'] = subject
# Login and send the message
port = 587
my_mail = 'my_mail@hotmail.com'
my_password = 'my_password'
with smtplib.SMTP('smtp-mail.outlook.com', port) as server:
server.starttls()
server.login(my_mail, my_password)
server.sendmail(my_mail, destination, msg.as_string())
send_email_gmail('Test', 'Bericht', 'm.drillenburg@gmail.com')
如果您對 gmail 提供的服務不滿意,如果不探索其他選項,建議切換到其他提供商是不完整的。
您可以將消息直接發送到 TCP 端口 25 上的目標電子郵件服務器。SMTP 服務器應該接受來自任何客戶端的“自己的”域的消息,而無需任何授權。 這里的憑據沒有問題。
該服務器的地址可以通過查找收件人電子郵件域的 MX 記錄(MX 代表郵件交換器)從 DNS 中獲得。 通常有幾個。 MX 主機具有首選項(最低編號 = 最高首選項),為了獲得最佳結果,應按照從最高優先級到最低優先級的順序聯系他們,直到接受郵件。
縮小是如果第一次嘗試失敗,您必須注意重試 - 通常使用郵件隊列來實現此目的。 一些站點使用“灰名單”作為反垃圾郵件措施,其中第一次嘗試將失敗。
當消息中有多個收件人並且此類程序成為真實 SMTP 服務器的一個很小的子集時,所有這些都變得不那么簡單了。 您甚至可能想要安裝像 Linux “postfix”這樣的真實服務器並將其配置為僅發送,有可用的教程。 請注意,有大量的配置選項。
如果您只想要另一個電子郵件地址,請找到新電子郵件服務器的 SMTP。
server = smtplib.SMTP('smtp.newemail.com', port)
# replace the url and port with your new server's smtp.
其余的應該是一樣的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.