Python 无法向 Gmail 发送电子邮件

Python Can't Send Emails to Gmail

我有一个 python 脚本来发送电子邮件。我利用它向我的电子邮件和 phone 发送警报。但由于某种原因,发送到 gmail 帐户时收不到电子邮件。我看到我的代码和其他示例之间的最大区别是我不能使用 smtp.gmail.com。这是我的 email/text 函数:

import smtplib

sender = 'xxxx@company.com'
smtpObj = smtplib.SMTP('smtp.company.com')

def text_func ( subject,text_message ):
    text_receivers = ['##########@vtext.com',
        'xxxxxxxx@company.com',
        'xxxxxxxx@gmail.com'
    ]
    text_message = """Subject: %s

    %s
    """ % (subject, text_message)

    smtpObj.sendmail(sender, text_receivers, text_message)

以上代码适用于向我的 phone 和我的工作电子邮件发送短信,但不适用于 gmail。我检查并确保电子邮件没有出现在垃圾邮件文件夹中。 Google 似乎完全阻止了它。有什么想法吗?

这可能不正确,因为我对 smtplib 的短信方面没有太多经验,但看起来您需要启动 TLS 连接。我会将您的脚本更改为以下内容:

import smtplib

sender = 'xxxx@company.com'
smtpObj = smtplib.SMTP('smtp.company.com')
smtpObj.starttls()

def text_func ( subject,text_message ):
    text_receivers = ['##########@vtext.com',
        'xxxxxxxx@company.com',
        'xxxxxxxx@gmail.com'
    ]
    text_message = """Subject: %s\n\n%s""" % (subject, text_message)

smtpObj.sendmail(sender, text_receivers, text_message)

您可能需要更改您的 gmail 设置以允许安全性较低的应用进入您的帐户。请参阅下面的 link 来自 google 的账户专家 Nick。

https://support.google.com/accounts/answer/6010255?hl=en

Option 2: Change your settings to allow less secure apps into your account. We don't recommend this option because it can make it easier for someone to break into your account. If you want to allow access anyway, follow these steps: Go to the Less secure apps section of your Google Account. Turn on Allow less secure apps. If you don't see this setting, your administrator might have turned off less secure app account access.

第 1 步:

Allowing less secure apps to access your account

发送邮件:

import smtplib

gmail_user = 'you@gmail.com'  
gmail_password = 'P@ssword!'

sent_from = gmail_user  
to = ['me@gmail.com', 'bill@gmail.com']  
subject = 'OMG Super Important Message'  
body = 'Hey, what's up?\n\n- You'

email_text = """\  
From: %s  
To: %s  
Subject: %s

%s
""" % (sent_from, ", ".join(to), subject, body)

try:  
    server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    server.ehlo()
    server.login(gmail_user, gmail_password)
    server.sendmail(sent_from, to, email_text)
    server.close()

    print 'Email sent!'
except:  
    print 'Something went wrong...'

找到解决方案。

import smtplib
from email.mime.text import MIMEText

sender = 'xxxx@company.com'
smtpObj = smtplib.SMTP('smtp.company.com')

def text_func ( subject,text_message ):
    text_receivers = ['##########@vtext.com',
        'xxxxxxxx@company.com',
        'xxxxxxxx@gmail.com'
    ]

    cont = '''\
       <html>
         <head></head>
         <body>
           <p>{0}</p>
         </body>
       </html>
       '''.format(text_message)

    msgTo = ""
    for person in text_receivers:
        msgTo += "{0} ".format(person)

    msg = MIMEText(cont, 'html')

    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = msgTo

    smtpObj.sendmail(sender, text_receivers, msg.as_string())

我也遇到了同样的问题。当我从 webmail 检查我的收件箱时,我发现该电子邮件被 gmail 拒绝。 通过在电子邮件中添加 "From" header 解决了问题。

import smtplib, ssl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

sender_email = "test@example.com"
receiver_email = "example@gmail.com"
password = "pass1234"
host = "example.com"
port = 465
message = MIMEMultipart("alternative")
message["Subject"] = "Email Subject"
message["From"] = sender_email
message["To"] = receiver_email
text = "This is the email Body"
message.attach(MIMEText(text, "plain"))
context = ssl.create_default_context()
with smtplib.SMTP_SSL(host, port, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(
        sender_email, receiver_email, message.as_string()
    )