尝试通过烧瓶邮件发送电子邮件时发生SSL错误

时间:2019-12-20 12:01:12

标签: docker ssl flask flask-mail

我一直在尝试使用Flask-mail发送电子邮件,但我总是得到SSL.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1076) // Werkzeug Debugger的帮助,而且我还使用Mailtrap作为虚拟电子邮件服务器来测试我的应用程序。我一直在尝试使用不同的邮件客户端,并且总是遇到相同的错误。

我想做的是,一旦有新用户注册。发送电子邮件以通过令牌确认您的帐户。

<!--
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/usr/local/lib/python3.7/site-packages/flask_restplus/api.py", line 584, in error_router
    return original_handler(e)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 38, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/usr/local/lib/python3.7/site-packages/flask_restplus/api.py", line 584, in error_router
    return original_handler(e)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 38, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/local/lib/python3.7/site-packages/flask_restplus/api.py", line 325, in wrapper
    resp = resource(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/flask/views.py", line 89, in view
    return self.dispatch_request(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/flask_restplus/resource.py", line 44, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/usr/src/app/app/user/controller.py", line 35, in post
    return UseService.register(data)
  File "/usr/src/app/app/user/service.py", line 48, in register
    send_confirmation_email(new_user.email)
  File "/usr/src/app/app/utility/mailutility.py", line 40, in send_confirmation_email
    send_email(to, "Test server - confirm your registration", html)
  File "/usr/src/app/app/utility/mailutility.py", line 32, in send_email
    mail.send(message)
  File "/usr/local/lib/python3.7/site-packages/flask_mail.py", line 491, in send
    with self.connect() as connection:
  File "/usr/local/lib/python3.7/site-packages/flask_mail.py", line 144, in __enter__
    self.host = self.configure_host()
  File "/usr/local/lib/python3.7/site-packages/flask_mail.py", line 156, in configure_host
    host = smtplib.SMTP_SSL(self.mail.server, self.mail.port)
  File "/usr/local/lib/python3.7/smtplib.py", line 1031, in __init__
    source_address)
  File "/usr/local/lib/python3.7/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/local/lib/python3.7/smtplib.py", line 336, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/local/lib/python3.7/smtplib.py", line 1039, in _get_socket
    server_hostname=self._host)
  File "/usr/local/lib/python3.7/ssl.py", line 423, in wrap_socket
    session=session
  File "/usr/local/lib/python3.7/ssl.py", line 870, in _create
    self.do_handshake()
  File "/usr/local/lib/python3.7/ssl.py", line 1139, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1076)

我的环境文件

MAIL_SERVER=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=mail_username
MAIL_PASSWORD=mail_password
MAIL_USE_TLS=True
MAIL_USE_SSL=False
MAIL_DEFAULT_SENDER=username@mail.com

代码

# !/usr/bin/python
# -*- coding: utf-8 -*-
from itsdangerous import URLSafeTimedSerializer
from flask_mail import Message
from flask import url_for, render_template
from flask import current_app as app
from app import mail
def generate_confirmation_token(email):
    serializer = URLSafeTimedSerializer(app.config.get("SECRET_KEY"))
    return serializer.dumps(email, salt=app.config.get("SECURITY_PASSWORD_SALT"))
def confirm__token(token, expiration=3000):
    serializer = URLSafeTimedSerializer(app.config.get("SECRET_KEY"))
    try:
        email = serializer.loads(
            token,
            salt=app.congit.get("SECURITY_PASSWORD_SALT"),
            max_age=expiration
        )
    except:  # noqa
        return False
    return email
def send_email(to, subject, template):
    """ Send an email """
    message = Message(subject, recipients=[to], html=template, sender=app.config.get("MAIL_DEFAULT_SENDER"))
    mail.send(message)
def send_confirmation_email(to):
    """Send a confirmation email to the registered user."""
    token = generate_confirmation_token(to)
    confirm_url = url_for("user_user_confirmation_mail", token=token, _external=True)
    html = render_template('confirmation.html', confirm_url=confirm_url)
    send_email(to, "Test server - confirm your registration", html)

如果那没道理,我可以使用docker来运行我的应用

1 个答案:

答案 0 :(得分:0)

在我的配置文件中,我只有 MAIL_USE_TLS=True 而没有 MAIL_USE_SSL。 当我将 MAIL_USE_TLS 和 MAIL_USE_SSL 设置为 true 时,我遇到了和你一样的错误。因此,如果您仍然遇到问题,这可能会有所帮助。