简体   繁体   English

使用Python和Gmail作为提供商发送电子邮件的安全方式是什么?

[英]What is a secure way to send an email using Python and Gmail as the provider?

I am trying to send emails to myself using a Python script, and luckily I came across this post: 我正在尝试使用Python脚本向自己发送电子邮件,幸运的是我遇到了这篇文章:

How to send an email with Gmail as provider using Python? 如何使用Python以Gmail作为提供商发送电子邮件?

The trouble is, smtplib sends out the password for the script in plain text and I am skeptical about its security. 麻烦的是,smtplib以纯文本形式发送脚本的密码,我对它的安全性持怀疑态度。 Further my script contains my username and password in plain text. 此外,我的脚本以纯文本格式包含我的用户名和密码。 Are there any good ways to use Python and send emails without having to keep my password as plain text? 有没有什么好方法可以使用Python并发送电子邮件而无需将我的密码保留为纯文本?

I also saw this on StackOverflow: Python smtplib security but the answer is not completely helping me resolve this conflict. 我也在StackOverflow上看到了这一点: Python smtplib安全性,但答案并不能完全帮助我解决这个冲突。 However, I'm not ready to give up yet. 但是,我还没准备好放弃。


Some more information: I'm trying to set up my Raspberry Pi as a server that scrapes through a website. 更多信息:我正在尝试将我的Raspberry Pi设置为通过网站进行擦除的服务器。 When a specific thing about the website changes, I want to be notified via email. 当关于网站的具体事情发生变化时,我希望通过电子邮件收到通知。 However, I don't want to leave my Pi sitting around with a script that has my username and password in plain text. 但是,我不想让我的Pi坐在一个带有我的用户名和密码的纯文本脚本。

The connection uses STARTTLS , so its not being sent over the internet in clear text. 该连接使用STARTTLS ,因此不会以明文形式通过互联网发送。

The function server.starttls() starts the encrypted communication with the server on port 465 instead of the normal port 25 for unencrypted SMTP mail traffic. 函数server.starttls()启动与端口465上的服务器的加密通信,而不是正常端口25,用于未加密的SMTP邮件通信。

An obvious solution would be to use 一个明显的解决方案是使用

getpass.getpass()

to get the password at the start of running, and store that in memory. 在运行开始时获取密码,并将其存储在内存中。

Even using starttls I just got blocked by Gmail from sending email from my Python script (which worked in the past)... I get a SMTPAuthenticationError with a link to continue sign-in via a browser, which is no help for my Python script trying to send mail via smtplib . 即使使用starttls我也被Gmail阻止从我的Python脚本发送电子邮件(过去有效)...我收到一个SMTPAuthenticationError其中包含一个链接,可以通过浏览器继续登录,这对我的Python脚本没有帮助试图通过smtplib发送邮件。 I do not have "2 step verification" setup on my account currently. 我目前没有在我的帐户上设置“2步验证”。

I had to go to my Google account (in a browser) and select an option to "enable access for less secure apps" 我必须转到我的Google帐户(在浏览器中),然后选择“为不太安全的应用启用访问权限”
https://www.google.com/settings/security https://www.google.com/settings/security

Changing that allowed my script to work again. 更改它允许我的脚本再次工作。 It looks like Google really wants us to use 2-step verification, and thus an app-specific password, going forward. 看起来Google真的希望我们使用两步验证,从而使用特定于应用程序的密码。 I guess I will do that and put up with having to enter an SMS code to login my email now and then... 我想我会这样做,忍受输入短信代码现在登录我的电子邮件然后......

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

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