[英]Error sending an email through gmail via python
你好!
我正在嘗試用Python編寫一個程序,該程序將接收電子郵件地址和命令行參數中的某些短語,並且該程序會將短語(帶有一些背景信息)發送到所提供的電子郵件地址。
該代碼如下:
#Mail Module
try:
import smtplib
import sys
from email.mime.text import MIMEText
except ImportError:
print "Error importing necessary modules into the project, Please contact MagshiProject staff"
else:
def sendAnEmail():
try:
sender = "magshiproject@gmail.com"
recipient = str(sys.argv[1])
password = str(sys.argv[2])
message = MIMEText("""From: From Magshimim Project <magshiproject@gmail.com>
To: To User <'{0}'>
Subject: Your password to further use MagshiProject Program
Your password is: '{1}'
Please copy and paste it from here to the window requesting this password in order to proceed.
This password will not be useful in 24 hours.""".format(recipient, password))
message['Subject'] = "Your password to use MagshiProject"
message['From'] = sender
message['To'] = recipient
s = smtplib.SMTP("smtp.google.com", 587)
s.ehlo()
s.starttls()
s.ehlo()
s.login("Username", "Password")
s.sendmail(me, [you], msg.as_string())
s.quit()
print "Message has been sent successfully."
except Exception as e:
print "Something went wrong, Please try again later."
print "Error Message: '{0}'.".format(str(e))
print "Error type: '{0}'.".format(str(type(e)))
sendAnEmail()
錯誤如下:
Something went wrong, Please try again later.
Error message: '[Errno 11001] getaddrinfo failed'.
Error type: '<class 'socket.gaierror'>'.
我該怎么辦才能解決這個問題?
預先感謝您,Iliya
您的代碼中存在一些問題,似乎表明這有點像復制/粘貼工作,但希望我們可以通過這一過程為您提供幫助。
服務器地址
您會注意到,您已使用以下代碼連接到SMTP服務器
s = smtplib.SMTP("smtp.google.com", 587)
但是,通過Google快速搜索“ Gmail SMTP”,我們可以看到Gmail的所有SMTP設置 ,包括地址: "smtp.gmail.com"
。 所以我們將其更改為
s = smtplib.SMTP("smtp.gmail.com", 587)
錯誤/缺少變量
此外,還有另一個小錯誤
s.sendmail(me, [you], msg.as_string())
顯然, me
和[you]
未定義,因此根據您的其他變量, me
應該是電子郵件的sender
,而[you]
應該是recipient
,因此我們將用您現有的變量替換這些值。 您也沒有變量msg
,因此在這種情況下,它可能應該是message.as_string()
。
另外,我們與s.login("Username","Password")
一起使用的s.login("Username","Password")
可能是發送電子郵件的同一個人,因此我們將其替換為sender
。
所以這是正確形式的代碼
#Mail Module
try:
import smtplib
import sys
from email.mime.text import MIMEText
except ImportError:
print "Error importing necessary modules into the project, Please contact MagshiProject staff"
else:
def sendAnEmail():
try:
sender = "magshiproject@gmail.com"
recipient = str(sys.argv[1])
password = str(sys.argv[2])
message = MIMEText("""From: From Magshimim Project <magshiproject@gmail.com>
To: To User <'{0}'>
Subject: Your password to further use MagshiProject Program
Your password is: '{1}'
Please copy and paste it from here to the window requesting this password in order to proceed.
This password will not be useful in 24 hours.""".format(recipient, password))
message['Subject'] = "Your password to use MagshiProject"
message['From'] = sender
message['To'] = recipient
s = smtplib.SMTP("smtp.gmail.com", 587)
s.ehlo()
s.starttls()
s.ehlo()
s.login(sender, "Password")
s.sendmail(sender, recipient, message.as_string())
s.quit()
print "Message has been sent successfully."
except Exception as e:
print "Something went wrong, Please try again later."
print "Error Message: '{0}'.".format(str(e))
print "Error type: '{0}'.".format(str(type(e)))
sendAnEmail()
假設您將"Username"
和"Password"
替換為實際值,並使用如下參數調用程序(假設文件名為sendmail.py
),則此方法有效
python sendmail.py "someone@somewhere.com" "password"
編輯:獲取sender
密碼
為了使此腳本更加完善,可以使用getpass
模塊提示輸入sender
密碼。 添加以下導入
import getpass
再改"Password"
在s.login(sender, "Password")
以
s.login(sender,getpass.getpass("Please enter your password for %s: " % sender))
然后系統會提示您輸入sender
的gmail密碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.