使用 cpanel python 应用无法发送电子邮件

Email wont send using the cpanel python app

由于我的 NGINX 服务器上的域问题,我正在尝试为我的 python 应用程序使用 cpanel python 应用程序。

我已经设置了 python 应用程序并将所有模块安装到 运行 该应用程序。应用程序 运行 没问题,但在联系页面上,当我尝试发送联系表单信息时,我收到 500 内部服务器错误。

该代码在 NGINX 服务器上运行良好。我确实需要更改一些东西才能在 cpanel 中的 python 应用程序上将代码设置为 运行,但它只是 passenger_wsgi.py 文件和我的 [=32] 中的 app.run =] 文件

这是我的 app.py 代码(主 python 文件)

import os

from flask import Flask, render_template, request
from flask_mail import Mail, Message
from forms import ContactForm

mail = Mail()

app = Flask(__name__)

app.secret_key = os.urandom(32)
app.config["MAIL_SERVER"] = "mail.example.com"
app.config["MAIL_PORT"] = 25
app.config["MAIL_USE_SSL"] = False
app.config["MAIL_USE_TLS"] = False
app.config["MAIL_USERNAME"] = 'info@exmple.com'
app.config["MAIL_PASSWORD"] = 'example'

mail.init_app(app)
application = app

@app.route('/')
def home():
    return render_template('index.jinja2')


@app.route('/contact_us', methods=['GET', 'POST'])
def contact_us():
    form = ContactForm()
    if request.method == 'POST':
        msg = Message(form.subject.data, sender=form.email.data, recipients=['info@example.com'])
        msg.body = """
              From: %s 
              Company Name: %s
              Message: %s
              """ % (form.full_name.data, form.company.data, form.message.data)
        mail.send(msg)

        return render_template('contact_us.jinja2', success=True, form=form)

    elif request.method == 'GET':
        return render_template('contact_us.jinja2', form=form)


if __name__ == "__main__":
    app.run()

这是form.py代码

from flask_wtf import Form
from wtforms import StringField, TextAreaField, SubmitField
from wtforms.validators import DataRequired, Email


class ContactForm(Form):
    full_name = StringField('Name', validators=[DataRequired('Please add your name')])
    email = StringField('Email',
                        validators=[DataRequired('Please enter your email'), Email('Please enter a valid email')])
    subject = StringField('Subject', validators=[DataRequired('Please enter the subject of this email')])
    company = StringField('Company', validators=[DataRequired('Please enter your company name')])
    message = TextAreaField('message', validators=[DataRequired('Please enter a message')])
    submit = SubmitField("Send")

这是联系我们页面上的表格

<div class="card-body form">
                        {% if success %}
                            <p class="lead text-success">Thank you for feedback !</p>
                        {% endif %}
                        <form method="post" action="{{ url_for('contact_us') }}">
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="md-form mb-0">
                                        {% for error in form.full_name.errors %}
                                            <span style="color: red;">[{{ error }}]</span>
                                        {% endfor %}
                                        {{ form.full_name(class='form-control', id='form-contact-name') }}
                                        {{ form.full_name.label(for='form-contact-name') }}
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="md-form mb-0">
                                        {% for error in form.email.errors %}
                                            <span style="color: red;">[{{ error }}]</span>
                                        {% endfor %}
                                        {{ form.email(class='form-control', id='form-contact-email') }}
                                        {{ form.email.label(for='form-contact-email') }}
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="md-form mb-0">
                                        {% for error in form.company.errors %}
                                            <span style="color: red;">[{{ error }}]</span>
                                        {% endfor %}
                                        {{ form.company(class='form-control', id='form-contact-company') }}
                                        {{ form.company.label(for='form-contact-company') }}
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="md-form mb-0">
                                        {% for error in form.subject.errors %}
                                            <span style="color: red;">[{{ error }}]</span>
                                        {% endfor %}
                                        {{ form.subject(class='form-control', id='form-contact-subject') }}
                                        {{ form.subject.label(for='form-contact-subject') }}
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="md-form mb-0">
                                        {% for error in form.message.errors %}
                                            <span style="color: red;">[{{ error }}]</span>
                                        {% endfor %}
                                        {{ form.message(class='form-control md-textarea', id='form-contact-message') }}
                                        {{ form.message.label(for='form-contact-message') }}
                                        <div class="text-center text-md-left">
                                            {{ form.submit(class='btn btn-sm rounded btn-primary') }}
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>

我将电子邮件和密码更改为示例。没有错误日志或代码,单击发送按钮后只给我一个 500 内部服务器错误。关于应用程序的其他所有内容都可以完美加载。

如果有关于这个问题的文档就好了。我已经用谷歌搜索了大约 4 天,现在无法找到有关该问题或如何使其正常工作的任何可靠信息。

设法找出问题所在,Cpanel 不允许用户 form.email.data 成为发件人。所以我不得不将其更改为实际的发件人电子邮件 info@example.com。我添加了一个 reply-to 这样你就可以回复客户了。

我还必须更改配置

app.config["SECRET_KEY"] = "Long Secret Key"
app.config["MAIL_SERVER"] = "mail.example.com"
app.config["MAIL_PORT"] = 25
app.config["MAIL_USE_SSL"] = False
app.config["MAIL_USERNAME"] = 'info@example.com'
app.config["MAIL_PASSWORD"] = 'password'

@app.route('/contact_us', methods=['GET', 'POST'])
def contact_us():
    form = ContactForm()

    if request.method == 'POST':
        msg = Message(form.subject.data, sender=("Contact Form", "info@example.com"), recipients=['info@example.com'], reply_to=form.email.data)
        msg.body = """
              From: %s
              Email: %s
              Company Name: %s
              Message: %s
              """ % (form.full_name.data, form.email.data, form.company.data, form.message.data)
        mail.send(msg)

        return render_template('contact_us.jinja2', success=True, form=form)

    elif request.method == 'GET':
        return render_template('contact_us.jinja2', form=form)