【问题标题】:Simple script to send email with python使用 python 发送电子邮件的简单脚本
【发布时间】:2016-06-12 21:46:10
【问题描述】:

所以我试图用 python 发送一封带有以下代码的基本电子邮件,但它给了我一个错误,提示我使用浏览器登录。我做到了,它仍然吐出同样的错误。 代码:

import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("projtig6@gmail.com", "redacted")

msg = "Your security system has detected an intrusion of some kind please refere to http://projetotig6.orgfree.com/ for an image of the current situation"
server.sendmail("projtig6@gmail.com", "grenskul@gmail.com", msg)
server.quit()

当我运行它时它给了我这个

user@ubuntu:~/Desktop$ python3 tent.py
Traceback (most recent call last):
  File "tent.py", line 5, in <module>
    server.login("projtig6@gmail.com", "redacted")
  File "/usr/lib/python3.4/smtplib.py", line 639, in login
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtT\n5.7.14 NJKUGchcZYhLMqcl9utRyvWa6jwkzOCuhBtdP3URW63HNIVrSlue8To8yKxxbDIwvlnO2g\n5.7.14 V2GooN21jsn0X8_d6W_CxGwuOXmBrkoDzFCqqbB72xz3MbY0Kj3Z7ZzdXlQCc8sjdavwes\n5.7.14 bUlIhA8GIqRKJAyBbildXtsSqF8Fh_7AYPI0W3SKlidhoUOK7o4hI0IIUmgVdqOCpFpxyU\n5.7.14 fvShqxoTn6W_bAWqXfS5akND40-gQ> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14  Learn more at\n5.7.14  https://support.google.com/mail/answer/78754 c142sm10923092wme.18 - gsmtp')

【问题讨论】:

  • 忘了提到我已经关注了该页面上的所有内容。我激活了 POP 和 IMAP 。可以连接到不发送附件的 SMTP 服务器。我唯一可能导致错误的是 ssl/TSL 的事情,但我不明白,所以我在这里发布。
  • 您确实意识到您正在公开显示您的密码...
  • 这不是实际密码。即使是一次性邮件。

标签: python email gmail smtplib


【解决方案1】:

首先,确保您“允许不太安全的应用通过您的 gmail 进行身份验证”:https://support.google.com/accounts/answer/6010255?hl=en

如果这不是问题,请尝试使用我在 python 中发送电子邮件时编写的这个包装器;它使发送电子邮件变得超级简单:https://github.com/krystofl/krystof-utils/blob/master/python/krystof_email_wrapper.py

像这样使用它:

emailer = Krystof_email_wrapper()

email_body = 'Hello, <br /><br />' \
             'This is the body of the email.'

emailer.create_email('sender@example.com', 'Sender Name',
                     ['recipient@example.com'],
                     email_body,
                     'Email subject!',
                     attachments = ['filename.txt'])

cred = { 'email': 'sender@example.com', 'password': 'VERYSECURE' }

emailer.send_email(login_dict = cred, force_send = True)

您还可以查看源代码以了解其工作原理。 相关位:

import email
import smtplib   # sending of the emails
from email.mime.multipart   import MIMEMultipart
from email.mime.text        import MIMEText

self.msg = MIMEMultipart()

self.msg.preamble   = "This is a multi-part message in MIME format."

# set the sender
author = email.utils.formataddr((from_name, from_email))
self.msg['From'] = author

# set recipients (from list of email address strings)
self.msg['To' ] = ', '.join(to_emails)
self.msg['Cc' ] = ', '.join(cc_emails)
self.msg['Bcc'] = ', '.join(bcc_emails)

# set the subject
self.msg['Subject'] = subject

# set the body
msg_text = MIMEText(body.encode('utf-8'), 'html', _charset='utf-8')
self.msg.attach(msg_text)

# send the email
session = smtplib.SMTP('smtp.gmail.com', 587)
session.ehlo()
session.starttls()
session.login(login_dict['email'], login_dict['password'])
session.send_message(self.msg)
session.quit()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-03
    • 2015-01-16
    • 2014-09-11
    • 1970-01-01
    • 2011-09-16
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多