PHP邮件程序 - 附加字段

时间:2015-02-20 07:01:41

标签: php phpmailer

我搜索过并研究过高低,我仍然无法找到答案。

我使用的是带有我们购买的主题的PHP Mailer(版本5.2.1)。我想弄清楚如何捕获电话号码并将其与发送的电子邮件中的其他数据一起发送。

表格:

<form method="post" action="#" id="contactForm">
                <div class="input-prepend" style="margin-left:0px;margin-right:0px">
                    <input class="input-xlarge required" id="name" type="text" placeholder="Name (Required)" />
                </div>
                <div class="input-prepend" style="margin-left:0px">
                    <input class="input-xlarge required" id="email" type="text" placeholder="Email (Required)" />
                </div>
                <div class="input-prepend" style="margin-left:0px">
                    <input class="input-xlarge required" id="phone" name="phone" type="text" placeholder="Phone (Required)" />
                </div>
                <div class="input-prepend" style="margin-left:0px">
                    <input class="input-xlarge required" id="subject" type="text" placeholder="Subject (Required)" />
                </div>
                <div class="input-prepend" style="margin-left:0px">
                    <input class="input-xlarge required" id="children-3-7" type="text" placeholder="Number of Children (Age 3 - 7) (Required)" />
                </div>
                <div class="input-prepend" style="margin-left:0px">
                    <input class="input-xlarge required" id="children-7-14" type="text" placeholder="Number of Children (Age 7 - 14) (Required)" />
                </div>
                <div class="input-prepend" style="margin-left:0px">
                    <input class="input-xlarge required" id="adults" type="text" placeholder="Number of Adults (Required)" />
                </div>
                <div class="input-prepend" style="margin-left:0px">
                    <textarea name="message-input" id="message" class="span12 required" cols="5" rows="5" placeholder="Message (Required)" style="margin-left:0px"></textarea>
                </div>

                <div class="clearfix" style="margin:10px 0px 0px 0px">
                    <a href="#" class="ignore btn resetButton"><span>Reset Form</span></a>
                    <a href="#" class="ignore btn submitButton"><span>Send Message</span></a>

                    <!-- Processing data trick -->
                    <div id="loadingForm">
                        <img src="images/normal/ajax-small.gif" alt="ajax-small" />
                    </div>
                    <!-- Processing data trick -->
                </div>

                <br />
                <div class="clearfix">
                <!-- contact notice -->
                <div class="alert alert-block fade" id="contact-notice">
                    <span><strong>Warning!</strong> All fields above are required to send the message properly.</span>
                </div>
                <!-- contact notice -->
            </div>
            </form>

PHP

<?php

require_once("class.phpmailer.php");

send();

function send(){

 $mailer = new PHPMailer();
 $mailer->IsSMTP();
 $mailer->Host = 'host.com';
 $mailer->SMTPAuth = true;
 $mailer->SMTPSecure = "tls";
 $mailer->Port = 587;
 $mailer->CharSet="utf-8";
 $mailer->IsHTML(true);

 $mailer->Username = 'mail-bot@mail.com';
 $mailer->Password = 'PASSWORD';
 $mailer->FromName = $_POST['name'];
 $mailer->From = $_POST['email'];
 $mailer->AddAddress('ADDRESS','NB=AME');
 $mailer->Subject = $_POST['subject'];
 $mailer->MsgHTML = $_POST['message'];

 $phone = $_POST['phone'];
 /**$mailer->Age1 = $_POST['children-3-7'];
 //$mailer->Age2 = $_POST['children-7-14'];
 //$mailer->Age3 = $_POST['adults'];**/

 $mailer->Body = "From : ".$_POST['name']."<br /> E-mail : ".$_POST['email']."<br /> Subject : ".$_POST['subject']."<br />Message : ".$_POST['message']."<br />Phone : $phone";

 if(!$mailer->Send()){

 echo "error^Message could not be sent.<br />";
 echo "Mailer Error: " . $mailer->ErrorInfo;
 exit;

 }
 else{

 echo "ok^Message sent successfully!";

 }

 }

?>

如您所见,我正在尝试使用

$phone = $_POST['phone'];

提取电话号码,然后使用

将其添加到邮件正文中
$mailer->Body = "From : ".$_POST['name']."<br /> E-mail : ".$_POST['email']."<br /> Subject : ".$_POST['subject']."<br />Message : ".$_POST['message']."<br />Phone : $phone";

注入电话号码

我也尝试过以下方式注射

$mailer->Body = "From : ".$_POST['name']."<br /> E-mail : ".$_POST['email']."<br /> Subject : ".$_POST['subject']."<br />Message : ".$_POST['message']."<br />Phone: ".$_POST['phone'];

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

看起来好像在我们的主.js文件中有一个辅助验证,它在通过ajax发布它们之前验证空字段。在发布之前需要在其中添加电话号码,我现在可以成功捕获数据。

感谢您的帮助!