带有PHPmail的HTML表单 - 脚本不处理表单

时间:2015-01-11 15:42:31

标签: php phpmailer

我正在尝试使用PHPmail发送表单的内容。当我提交表单时,它会转到一个空白页面 - PHP脚本作为网页。

我试图重复使用PHPMailer文档中的代码生成器文件生成的代码,但无济于事。

表格代码摘录:

<form method="post" action="referralengine.php" id="myForm" role="form" enctype="multipart/form-data" >
    <div class="form-group">
        <label for="field1">Field 1</label>
        <input type="text" class="form-control" id="field1" name="field1" placeholder="Enter data" />
    </div>
    <div class="form-group">
        <label for="field2">Field 2</label>
        <textarea class="form-control" id="field2" name="field2" rows="4" placeholder="Enter data" ></textarea>
    </div>
    <div class="form-group">
        <label for="field3">Field 3</label>
        <textarea class="form-control" id="field2" name="field2" rows="4" placeholder="Enter data" ></textarea>
    </div>
    <div class="form-group">
        <label for="newupload">Upload file</label>
        <input type="file" id="newupload" name="newupload" class="input-file" />
    </div>
</form>

PHP如下:

<?php
    require_once 'PHPMailerAutoload.php';

    $field1= Trim(stripslashes($_POST['field1']));
    $field2= Trim(stripslashes($_POST['field2']));
    $field3= Trim(stripslashes($_POST['field3']));

    // Email body text
    $content .= "Item 1: " .$field1 ."\n";
    $content .= "Item 2: " .$field2 ."\n";
    $content .= "Item 3: " .$field3 ."\n";

    $results_messages = array();

    $mail = new PHPMailer(true);
    $mail->CharSet = 'utf-8';

    $mail->From = "fromAddress@mail.com";
    $mail->FromName = "Some name";
    $mail->addAddress("toAddress@mail.com");
    $mail->Subject = "PHPMailer test using SENDMAIL";
    $body = $content;
    $mail->WordWrap = 78;
    $mail->addAttachment($_FILES['newupload']['tmp_name']);

    try {
        $mail->send();
        $results_messages[] = "Message has been sent using SENDMAIL";
    }
    catch (phpmailerException $e) {
        throw new phpmailerAppException('Unable to send to: ' . $to. ': '.$e->getMessage());
    }
    catch (phpmailerAppException $e) {
        $results_messages[] = $e->errorMessage();
    }

    if (count($results_messages) > 0) {
        echo "<h2>Run results</h2>\n";
        echo "<ul>\n";
        foreach ($results_messages as $result) {
            echo "<li>$result</li>\n";
        }
        echo "</ul>\n";
    }

?>

欢迎提出任何建议,谢谢。

0 个答案:

没有答案