首页 > 解决方案 > 将 PHP 链接到 HTML 失败(使用 react 和 jsx)

问题描述

我似乎无法将我的 PHP 文件成功链接到 HTML 表单。

这是我的PHP代码:

<?php
    require_once('Mail.php');

    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $body = $_POST['body'];

    $to='email@gmail.com';
    $headers= array(
        'From' => $email,
        'To' => $to,
        'Subject' => $subject
    );

    //smtp settings
    $smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => 'email@gmail.com',
        'password' => 'password'
    ));

    $mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail)) {
        echo('<p>' . $mail->getMessage() . '</p>');
    } else {
        echo('<p>Message successfully sent!</p>');
    }

?>

这是我的 HTML 表单代码:

import React from 'react';
import tiagoImg from '../images/tiagoPhoto.jpg';
import cell from '../images/cell.jpg';
import email from '../images/email.jpg';
import www from '../images/www.jpg';
import phone from '../images/phone.jpg';
import address from '../images/address.jpg';

const Contact = () => {
    return (
        <div className='contactContainer'>
            <center>
                <h4 class='sent-notification'></h4>
                <form method='post' action='sendEmail.php'>
                    <h2>Send an Email</h2>

                    <label>Name</label>
                    <input id='name' name='name' type='text' placeholder='Enter name' required/>
                    <br />
                    <label>Email</label>
                    <input id='email' name='email' type='text' placeholder='Enter email' required/>
                    <br />
                    <label>Subject</label>
                    <input id='subject' name='subject' type='text' placeholder='Enter subject' required/>
                    <br />
                    <p>Message</p>
                    <textarea id='body' name='body' rows='S' placeholder='Type message' required></textarea>
                    <br />
                    <input type='submit' name='submit' value='Submit'/>
                </form>
            </center>
        </div>
    )
}

export default Contact;

这就是我的目录的样子:

在此处输入图像描述

这是我点击提交时收到的错误:

在此处输入图像描述

标签: phphtmlreactjsformsemail

解决方案


推荐阅读