簡體   English   中英

Email 未通過 python 發送

[英]Email is not being sent via python

我創建了這段代碼作為測試運行,通過 python 發送 email:

import smtplib
import random
import math
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
ADMIN_EMAIL = email
ADMIN_PASS = password



def generate_ver_code():
    digs = "0123456789"
    code = ""
    for i in range(0, 6):
        code += digs[math.floor(random.random() * 10)]
    return code



def signup(forename, surname, email, occupation, dob, pass1, pass2, pass_match):
    ran_num = str(random.randint(0, 9999))
    if len(ran_num) >= 4:
        if len(ran_num) == 1:
            ran_num = "000" + str(ran_num)
        elif len(ran_num) == 2:
            ran_num = "00" + str(ran_num)
        elif len(ran_num) == 3:
            ran_num = "0" + str(ran_num)
        elif len(ran_num) == 4:
            ver_code = str(ran_num)
    username = ran_num + forename[:3] + surname[:3] + dob[:2]
    
    if pass1 == pass2:
        passw = pass1
        pass_match = True
    else:
        pass_match = False
    s = smtplib.SMTP('smtp.gmail.com', 5354)
    #home port = 5354
    #school port = 
    s.starttls()
    s.login(ADMIN_EMAIL, ADMIN_PASS)
    msg = MIMEMultipart()
    message = message_template.substitute(PERSON_NAME=forename)
    msg['From']=ADMIN_EMAIL
    msg['To']=email
    msg['Subject']="Verify account: FAKtory Reset"
    msg.attach(MIMEText(message, '\nBefore you can continue to use your account please verify yur account and check if the credentials are correct:\nUsername: '+ username + '\nName: ' + forename + ' ' + surname + '\nOccupation: ' + occupation + '\nDoB: ' + dob + '\nPassword: ' + pass1 + '\nVerification Code: ' + ver_code + '\nIf any of the credentials are wrong please enter them again on our app.\nThank you,\nRegards,\nFaizan Ali Khan\nAdmin'))
    s.send_message(msg)
    del msg



pass_match = False
forename = str(input("Enter your forename: "))
surname = str(input("Enter your surname: "))
email = str(input("Enter your email: "))
occupation = str(input("Enter your occupation: "))
dob = str(input("Enter your date of birth (DD/MM/YYYY): "))
pass1 = str(input("Enter your password: "))
pass2 = str(input("Enter your password again: "))
print(signup(forename, surname, email, occupation, dob, pass1, pass2, pass_match))

現在,每當我運行代碼時,輸入都可以正常工作,但是在發送 email 時,我收到此錯誤:

TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

您將如何解決此錯誤? 我嘗試更改端口,但仍然無法正常工作。

s = smtplib.SMTP('smtp.gmail.com', 5354)

問題是這個代碼片段。 根據官方Gmail IMAP 指南,傳出端口是465587

傳出 SMTP 服務器, smtp.gmail.com ,需要 5 個如果您的客戶端在發出 STARTTLS 命令之前以純文本開頭,請使用端口465或端口587

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM