PHP联系表(PHPMailer)

时间:2015-09-22 18:05:10

标签: php forms email phpmailer

使用GitHub PHPMailer的时间,我遇到发送电子邮件的问题。 数据似乎被发送到表格,我失去了。

我知道我没有在页面中使用错误,这就是为什么我现在将它们打印到php表单。

html(index.php)

<form name="contactform" id="myform" class="fs-form fs-form-full" autocomplete="off" method="post" action="send.php">
                <ol class="fs-fields">
                    <li>
                        <label class="fs-field-label fs-anim-upper" for="q1">What's your name?</label>
                        <input class="fs-anim-lower" id="q1" name="q1" type="text" placeholder="Johnny Bravo" required/>
                    </li>
                    <li>
                        <label class="fs-field-label fs-anim-upper" for="q2" data-info="We won't send you spam, we promise...">What's your email address?</label>
                        <input class="fs-anim-lower" id="q2" name="q2" type="email" placeholder="me@email.com" required/>
                    </li>
                    <li data-input-trigger>
                        <label class="fs-field-label fs-anim-upper" for="q3" data-info="This will help us know what kind of service you need">What can we do for you? <span style="font-size:20px;">(select one)</span></label>
                        <div class="fs-radio-group fs-radio-custom clearfix fs-anim-lower">
                            <span><input id="q3b" name="q3" type="radio" value="graphic"/><label for="q3b" class="radio-graphic">Graphic Design</label></span>
                            <span><input id="q3c" name="q3" type="radio" value="web"/><label for="q3c" class="radio-web">Web Design</label></span>
                            <span><input id="q3a" name="q3" type="radio" value="motion"/><label for="q3a" class="radio-motion">Motion Graphics</label></span>
                            <span><input id="q3d" name="q3" type="radio" value="other"/><label for="q3d" class="radio-other">Other/More</label></span>

                        </div>
                    </li>

                    <li>
                        <label class="fs-field-label fs-anim-upper" for="q4">Describe your project in detail.</label>
                        <textarea class="fs-anim-lower" id="q4" name="q4" placeholder="Describe here"></textarea>
                    </li>
                    <li>
                        <label class="fs-field-label fs-anim-upper" for="q5">What's your budget? (USD)</label>
                        <input class="fs-mark fs-anim-lower" id="q5" name="q5" type="number" placeholder="1000" step="100" min="100"/>
                    </li>
                </ol><!-- /fs-fields -->
                <button class="fs-submit" type="submit" value="Send">Send It</button>
            </form><!-- /fs-form -->

PHP(send.php)

    <?php

   session_start();

   require_once 'libs/phpmailer/PHPMailerAutoload.php';

   $errors = [];

   if(isset($_POST['q1'], $_POST['q2'], $_POST['message'])) {

$fields = [
    'q1' => $_POST['q1'],
    'q2' => $_POST['q2'],
    'q3' => $_POST['q3'],
    'q4' => $_POST['q4'],
    'q5' => $_POST['q5']
];

foreach($fields as $field => $data) {
    if(empty($data)) {
        $errors[] = 'The ' . $field . ' field is required.';
    }
}

if(empty($errors)) {

    $m = new PHPMailer;

    $m->isSMTP();
    $m->SMTPAuth = true;

    $m->Host = 'smtp.gmail.com';
    $m->Username = 'myemail@gmail.com';
    $m->Password = 'mypassword';
    $m->SMTPSecure = 'ssl';
    $m->Port = 465;

    $m->isHTML();

    $m->Subject = 'Contact form submitted';
    $m->Body = 'From: ' . $fields['q1'] . ' (' . $fields['q2'] . ')<p>Budget</p><p>' . $fields['q5'] . '</p><p>Service</p><p>' . $fields['q3'] . '</p><p>Details</p><p>' . $fields['q4'] . '</p>';

    $m->FromName = 'Contact';

    $m->AddAddress('myemail@gmail.com', 'Ollie Taylor');

    if($m->send()) {
        header('Location: thanks.php');
        die();
    } else {
        $errors[] = 'Sorry, could not send email. Try again later.';
    }

}

} else {
    $errors[] = 'Something went wrong.';
}

$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;

header('Location: index.php');

三江源!

1 个答案:

答案 0 :(得分:1)

初学者有一些语法错误。

send.php第30行

$m-Host = 'smtp outgoing here';

应该是

$m->Host = 'smtp outgoing here';

和第39行

$m->Body = 'From:: ' . $fields['q1'] . '(' $fields['q2'] ')' . $fields['q3'] . $fields['q4'] . $fields['q5'];

应该是

$m->Body = 'From:: ' . $fields['q1'] . '(' . $fields['q2'] . ')' . $fields['q3'] . $fields['q4'] . $fields['q5'];

(在括号之间添加连接)

总的来说,我建议您查看PHP错误日志。我没有进一步测试它,看看电子邮件是否实际发送。但是这里的大图:检查PHP错误日志。