python - SMTP mail failing on AWS EC2 - Stack Overflow

admin2025-05-02  2

I am working on a reset password feature for my web application and am using SMTP to send the reset emails. The functionality works perfectly fine when I run it on my local machine. However, when I deploy the application to an AWS EC2 server, it fails to send the email and immediately goes to the first except block in my code.

Here is the specific code I am using to send the reset password email:

@application.route('/reset', methods=['GET', 'POST'])
def reset():
    my_email = request.form['mail']
    password = request.form['pass']
    password1 = request.form['pass2']

    try:
        if password == password1:
            token = s.dumps({'pass': password, 'email': my_email}, salt='email-confirm')

            link = url_for('confirm_reset', token=token, _external=True)

            to_email = my_email
            servername = 'email-smtp.us-east-1.amazonaws'
            serverport = '587'
            if serverport:
                serverport = int(serverport)
            else:
                serverport = 25
            use_tls = 'no'
            from_ = "an email"
            username=""
            password = ""

            # Create the message

            msg = MIMEText('Please click on this link ' + link)
            msg.set_unixfrom('author')
            msg['To'] = email.utils.formataddr(('Recipient', to_email))
            msg['From'] = email.utils.formataddr(('Author',
                                                  from_))
            msg['Subject'] = 'Password restore'

            if use_tls == 'yes':

                server = smtplib.SMTP_SSL(servername, serverport)
            else:

                context = ssl.create_default_context()
                server = smtplib.SMTP(servername, serverport)
            try:
                server.set_debuglevel(True)

                # identify ourselves, prompting server for supported features
                server.ehlo()

                # If we can encrypt this session, do it
                if server.has_extn('STARTTLS'):

                    server.starttls()
                    server.ehlo()  # reidentify ourselves over TLS connection
                else:
                    print('(no STARTTLS)')

                if server.has_extn('AUTH'):

                    server.login(username, password)
                else:
                    print('(no AUTH)')

                server.sendmail(username,
                                [to_email],
                                msg.as_string())
            finally:
                server.quit()
            return render_template('verify.html')
    except:
        msg = 'We could not send you the link'

    else:
        msg = 'You have to give the same password both times'
    return render_template('reset.html', msg=msg)

I suspect the problem might be related to the way my application handles the SMTP connection in the deployed environment or possibly some configuration I am missing for production.

Has anyone encountered a similar issue? Are there specific settings or best practices I should be following when deploying an SMTP-based email functionality on AWS EC2? Any suggestions or insights would be greatly appreciated.

转载请注明原文地址:http://anycun.com/QandA/1746123959a92006.html