使用PHP邮件程序发送表单

时间:2015-08-05 17:53:25

标签: php forms phpmailer

真的希望有人可以提供帮助 - 我一直试图让它运行一周左右。

我的表单有问题,因为有些服务器阻止了电子邮件,因为'from'中的电子邮件不同。有人告诉我使用PHP Mailer。我对此完全陌生,所以我试图将它整合到我的表单中,并且在提交时只是得到一个空白页面。谁能告诉我我的错误?

<?php
$servername = "***";
$username = "***";
$password = "***";
$dbname = "***";


try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // prepare sql and bind parameters
    $stmt = $conn->prepare("INSERT INTO guestquestionnaire (date_submitted, choice, expectations, res, res_information, res_staff, further_comments1)
    VALUES (:date_submitted, :choice, :expectations, :res, :res_information, :res_staff, :further_comments1)");
    $stmt->bindParam(':date_submitted', $date, PDO::PARAM_STR);
    $stmt->bindParam(':choice', $choice);
    $stmt->bindParam(':expectations', $expectations);
    $stmt->bindParam(':res', $res);
    $stmt->bindParam(':res_information', $res_information);
    $stmt->bindParam(':res_staff', $res_staff);
    $stmt->bindParam(':further_comments1', $further_comments1);


    // insert a row
    $date = date('Y-m-d H:i:s');
    $choice = $_POST['choice'];
    $expectations = $_POST['expectations'];
    $res = $_POST['res'];
    $res_information = $_POST['res_information'];
    $res_staff = $_POST['res_staff'];
    $further_comments1 = $_POST['further_comments1'];
    $stmt->execute();

    }

catch(PDOException $e)
    {
    echo "Error: " . $e->getMessage();
    }
$conn = null;



?>



<?php

composer require phpmailer/phpmailer

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer;

//From email address and name
$mail->From = "info@personalemail.com";
$mail->FromName = "Host Person";

//To address and name
$mail->addAddress("user@icloud.com", "User");
$mail->addAddress("info@personalemail.com"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("info@personalemail.com", "Reply");

//CC and BCC
$mail->addCC("cc@example.com");
$mail->addBCC("bcc@example.com");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Guest Questionnaire Received";


// Build message.

$img="<img src='http://www.myurl.com/guestquestionnaire/images/star".$_POST['res'].".jpg'>";

$img2="<img src='http://www.myurl.com/guestquestionnaire/images/star".$_POST['res_information'].".jpg'>";

$img3="<img src='http://www.myurl.com/guestquestionnaire/images/star".$_POST['res_staff'].".jpg'>";

            $message = '<html><body>';  
            $message .=  "<strong>Guest Questionnaire </strong>";
            $message .=  "<strong>Received: </strong>";
            $message .=  "<P>";

            $message .= '<table rules="all" style="border: 1px solid #999;" cellpadding="7" width="100%" >';

            $message .= "<tr style='background: #566890; font-size: 8pt; font-weight: bold; color: #fff;'><td colspan='3'>Prior to Arrival</td></tr>";


            $message .= "<tr style='font-size: 8pt;'><td>What made you choose us for your recent trip? </td><td width='40%' colspan='2'>" . strip_tags($_POST['choice']) . "</td></tr>";

            $message .= "<tr style='font-size: 8pt;'><td>Did we meet your expectations as advertised? If no, please state why: </td><td width='40%' colspan='2'>" . strip_tags($_POST['expectations']) . "</td></tr>";

                        $message .= "<tr style='background: #566890; font-size: 8pt; font-weight: bold; color: #fff;'><td colspan='3'>Making your Reservation</td></tr>";


            $message .= "<tr style='font-size: 8pt;'><td>Ease of making your reservation: </td><td width='40%'>$img</td><td width='5%'>" . strip_tags($_POST['res']) . "</td></tr>";


            $message .= "<tr style='font-size: 8pt;'><td>Hotel information offered: </td><td width='40%'>$img2</td><td width='5%'>" . strip_tags($_POST['res_information']) . "</td></tr>";


            $message .= "<tr style='font-size: 8pt;'><td>Warmth and friendliness of staff: </td><td width='40%'>$img3</td><td width='5%'>" . strip_tags($_POST['res_staff']) . "</td></tr>";

            $message .= "<tr style='font-size: 8pt;'><td colspan='3'>Further Comments: </BR></BR>" . strip_tags($_POST['further_comments1']) . "</td></tr>";


            $message .= "</table>";

            $message .=  "<BR>";





            $message .= "</table>";
            $message .= "</body></html>";

$mail->Body = "<i>".$message."</i>";


if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else
{
    echo "Message has been sent successfully";
}




?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="surveystyle.css" media="all" />

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"></script>
    <script type="text/javascript" src="js/jquery.inputfocus-0.9.min.js"></script>
    <script type="text/javascript" src="/jquery.main.js"></script>



     <link rel="stylesheet" href="//code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script>

<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
</head>
<body>

<div id="container">


                <div class="logo-header"><img src="images/logo.jpg" alt="" width="205" height="119" /></div>


             <div class="question-start"><h1>Thank You!</h1><p>Your evaluation <b>will</b> make a difference.</p></div>


<div id="progress_bar">
<div id="progress_end"></div>
        <div id="progress_text">100% Complete</div>
    </div>


         </div>


        </div>

</body>

更新:Error_log被带回:

[Wed Aug 05 12:57:50 2015] [error] [client 173.58.203.164] PHP Warning:  require_once(vendor/autoload.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream: No such file or directory in /var/www/vhosts/myurl.com/httpdocs/guestquestionnaire/surveysubmit.php on line 113, referer: http://www.myurl.com/guestquestionnaire/
[Wed Aug 05 12:57:50 2015] [error] [client 173.58.203.164] PHP Fatal error:  require_once() [<a href='function.require'>function.require</a>]: Failed opening required 'vendor/autoload.php' (include_path='.:') in /var/www/vhosts/myurl.com/httpdocs/guestquestionnaire/surveysubmit.php on line 113, referer: http://www.myurl.com/guestquestionnaire/

Access_log带回来了:

404 7682 "http://www.myurl.com/" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"

2 个答案:

答案 0 :(得分:1)

$servername = “***”;
              ^---^

不要使用Microsoft Word来编辑代码。这些不是"引用。他们是“聪明的引语”,在编程世界里真的是“愚蠢无用的非引用”。

然后进入你的php.ini并打开display_errorserror_reporting。如果它们已经打开,那么你会被告知这些非引号导致的语法错误。

答案 1 :(得分:0)

此部分未正确关闭:

$mail->Body = "<i>

要正确关闭它,请在构建消息变量的部分下方移动该行,并在其中添加消息和结束标记:

$message .= "</body></html>";
$mail->Body = "<i>".$message."</i>";

删除该行:

composer require phpmailer/phpmailer

这是使用composer安装phpmailer的命令行,有关详细信息,请参阅this page

BTW,&#39; require_once&#34; vendor / autoload.php&#34;;&#39;只有在你使用PHPMailer的作曲家安装时才会起作用。

有关PHPMailer的替代安装,请参阅GitHub

您应该在绑定命令之前使用值赋值移动部件:

// insert a row
$date = date('Y-m-d H:i:s');
$choice = $_POST['choice'];
$expectations = $_POST['expectations'];
$res = $_POST['res'];
$res_information = $_POST['res_information'];
$res_staff = $_POST['res_staff'];
$further_comments1 = $_POST['further_comments1'];

$stmt->bindParam(':date_submitted', $date, PDO::PARAM_STR);
$stmt->bindParam(':choice', $choice);
$stmt->bindParam(':expectations', $expectations);
$stmt->bindParam(':res', $res);
$stmt->bindParam(':res_information', $res_information);
$stmt->bindParam(':res_staff', $res_staff);
$stmt->bindParam(':further_comments1', $further_comments1);

$stmt->execute();