[英]Send email using python with Gmail as provider. Not working
我试图发送电子邮件使用python
与gmail
的供应商。 代码如下:
import smtplib
def send_email():
email_address = 'xxx@gmail.com'
email_password = 'xxx'
try:
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(email_address, email_password)
sub= 'sdgsdg'
msg = 'sdfsf'
message = 'Subject: {}\n\n{}'.format(sub, msg)
server.sendmail(email_address, email_address, message)
server.quit()
print('Success: Email sent!')
except:
print('Email failed to send.')
if __name__ == "__main__":
send_email()
我正在Windows 10
上使用Python 3.6.2
执行。 该代码用于长时间执行,并且电子邮件未发送。 该代码永远不会停止执行,我被迫停止执行。 谁能指出该函数有什么问题? 当然,我已经在gmail
启用了less secure apps
。 我认为代码永远不会从server = smtplib.SMTP('smtp.gmail.com:587')
向前移动。
我制作了一个“自动邮件发件人”,它将接收大量产品和邮件文件,自动更正某些错误类型的邮件,根据特定算法和受支持的外部语言自动更改主题和邮件,以...
这只是我的代码的一部分,可能会有所帮助...
#// - You need to set "Allow less secure apps: ON" (go to link below).
#// - https://myaccount.google.com/lesssecureapps
#// Is work and with out that "allow..." but is more ok to set it.
# Imports
import smtplib, time
# Language
error_title = "ERROR"
send_error = "I'm waiting {idle} and I will try again."
send_error_mail = "Mail invalid"
send_error_char = "Invalid character"
send_error_connection_1_2 = "Connection problem..."
send_error_connection_2_2 = "Gmail server is down or internet connection is instabil."
login_browser = "Please log in via your web browser and then try again."
login_browser_info = "That browser and this software shood have same IP connection first time."
# Gmaild ID fro login
fromMail = "your_id@gmail.com"
fromPass = "your_password"
# To ho to send mails
mailTo = [
"test@me.com",
"test_2@ymail.com"
]
# Some configurations
mailDelay = 15
exceptionDelay = 180
# SEND MAILS
def send_mail(thisSubject="Just a subject", thisMessage="This is just a simple message..."):
# If still have mails to send
while len(mailTo) != 0:
sendItTo = mailTo[0] # Memorise what mail will be send it (debug purpose)
try:
# Connect to the server
server = smtplib.SMTP("smtp.gmail.com:587")
server.ehlo()
server.starttls()
# Sign In
server.login(fromMail, fromPass)
# Set the message
message = f"Subject: {thisSubject}\n{thisMessage}"
# Send one mail
server.sendmail(fromMail, mailTo.pop(0), message)
# Sign Out
server.quit()
# If is a problem
except Exception as e:
# Convert error in a string for som checks
e = str(e)
# Show me if...
if "The recipient address" in e and "is not a valid" in e:
print(f"\n>>> {send_error_mail} [//> {sendItTo}\n")
elif "'ascii'" in e and "code can't encode characters" in e:
print(f"\n>>> {send_error_char} [//> {sendItTo}\n")
elif "Please" in e and "log in via your web browser" in e:
print(f"\n>>> {login_browser}\n>>> - {login_browser_info}")
break
elif "[WinError 10060]" in e:
if "{idle}" in send_error:
se = send_error.split("{idle}"); seMsg = f"{se[0]}{exceptionDelay} sec.{se[1]}"
else:
seMsg = send_error
print(f"\n>>> {send_error_connection_1_2}\n>>> {send_error_connection_2_2}")
print(f">>> {seMsg}\n")
# Wait 5 minutes
waitTime = exceptionDelay - mailDelay
if waitTime <= 0:
waitTime = exceptionDelay
time.sleep(waitTime)
else:
if "{idle}" in send_error:
se = send_error.split("{idle}"); seMsg = f"{se[0]}{exceptionDelay} sec.{se[1]}"
else:
seMsg = send_error
print(f">>> {error_title} <<<", e)
print(f">>> {seMsg}\n")
# Wait 5 minutes
time.sleep(exceptionDelay)
# If are still mails wait before to send another one
if len(mailTo) != 0:
time.sleep(mailDelay)
send_mail()
) ( )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.