简体   繁体   English

Python 无法向 Gmail 发送电子邮件

[英]Python Can't Send Emails to Gmail

I have a python script to send emails.我有一个 python 脚本来发送电子邮件。 I utilize this to send alerts to my email and phone.我利用它向我的电子邮件和电话发送警报。 But for some reason the emails are not received when sending to a gmail account.但是由于某种原因,发送到 Gmail 帐户时未收到电子邮件。 The big difference that I see between my code and other examples is I cannot use the smtp.gmail.com.我在我的代码和其他示例之间看到的最大区别是我不能使用 smtp.gmail.com。 Here's my email/text function:这是我的电子邮件/文本功能:

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)

The above code works for sending an sms to my phone and my work email but not to gmail.上面的代码适用于向我的手机和我的工作电子邮件发送短信,但不适用于 Gmail。 I checked and made sure the email doesn't show up in a spam folder.我检查并确保电子邮件没有出现在垃圾邮件文件夹中。 Google seems to be blocking it completely.谷歌似乎完全阻止了它。 Any ideas?有任何想法吗?

This may not be correct, as I don't have much experience with the texting side of smtplib , but it looks like you need to start the TLS connection.这可能不正确,因为我对smtplib的短信方面没有太多经验,但看起来您需要启动 TLS 连接。 I would change your script to the following:我会将您的脚本更改为以下内容:

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)

I also faced the same problem.我也面临同样的问题。 When I checked my inbox from webmail, I found that the email was rejected by gmail.当我从 webmail 检查我的收件箱时,我发现该电子邮件被 gmail 拒绝。 截屏 The problem was solved by adding the "From" header to the email.通过向电子邮件添加“发件人”标题解决了该问题。

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()
    )

You may need to change your gmail settings to allow less secure apps into your account.您可能需要更改 Gmail 设置以允许安全性较低的应用进入您的帐户。 See link below from Nick, an accounts expert at google.请参阅下面来自谷歌帐户专家尼克的链接。

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

Option 2: Change your settings to allow less secure apps into your account.选项 2:更改设置以允许安全性较低的应用进入您的帐户。 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.如果您仍想允许访问,请按照以下步骤操作: 转到您的 Google 帐户的安全性较低的应用部分。 Turn on Allow less secure apps.打开允许不太安全的应用程序。 If you don't see this setting, your administrator might have turned off less secure app account access.如果您没有看到此设置,则您的管理员可能已关闭安全性较低的应用帐户访问权限。

Step 1:步骤1:

Allowing less secure apps to access your account允许安全性较低的应用访问您的帐户

For Sending the Email:发送电子邮件:

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...'

Found the solution.找到了解决办法。

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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM