PHPmailer设置从电子邮件发送到电子邮件

时间:2013-11-20 13:58:14

标签: php email phpmailer

我正在使用PHP邮件程序来构建联系表单,请记住我是PHP的新手。我发送的表单很好,但是,在我收到的电子邮件中,发件人地址与用户输入联系表单的电子邮件不匹配,而是显示与收件人相同的电子邮件。我百分百肯定我正在输入正确的电子邮件到实时联系表格。我用$$$$$$$$$

替换了我的敏感信息
   <?php 


    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $name = trim($_POST["name"]);
        $email = trim($_POST["email"]);
        $message = trim($_POST["message"]);

        if ($name == "" OR $email == "" OR $message == "") {

            $error_message = "You must specify a value for name, email address, and message.";
        }

        if (!isset($error_message)) {
            foreach( $_POST as $value ){
                if( stripos($value,'Content-Type:') !== FALSE ){
                    $error_message = "There was a problem with the information you entered.";
                }
            }
        }

        if (!isset($error_message) && $_POST["address"] != "") {
            $error_message = "Your form submission has an error.";
        }

        require_once("../inc/phpmailer/PHPMailerAutoload.php");
        $mail = new PHPMailer();

        if (!isset($error_message) && !$mail->ValidateAddress($email)){
            $error_message = "You must specify a valid email address.";
        }

            $mail->IsSMTP();
            $mail->SMTPAuth = true;
            $mail->Host = "smtp.gmail.com";
            $mail->Port = 587;
            $mail->Username = "$$$$$$$$$$$$$";
            $mail->Password = "$$$$$$$$$$$$$";
            $mail->SMTPSecure = 'tls';

        if (!isset($error_message)) {

            $mail->From =  $email;
            $mail->FromName = $name;
            $mail->addAddress('$$$$$$$$$$$$$$', '$$$$$$$$$$l');

            $mail->isHTML(true);

            $mail->Subject = '$$$$$$$$$$| ' . $name;
            $mail->Body = $message;
            $mail->AltBody = $message;

            if($mail->Send()) {
                header("Location: ../contact/?status=thanks");
                exit;
            } else {
              $error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
            }

        }
    }

    require_once("../inc/header.php"); ?>

        <div class="section page">

            <div class="wrapper">

                <div class="row">

                    <div class="col-md-4 col-md-offset-4">
                        <div class="center-block">
                            <h1>Contact</h1>
                        </div>

                        <?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
                            <div class="alert alert-success">
                            <p>Thanks for the email! I&rsquo;ll be in touch shortly!</p>
                            </div>
                        <?php } else { ?>

                            <?php
                                if (!isset($error_message)) {

                                    echo '<p>I&rsquo;d love to hear from you! Complete the form to send me an email.</p>';

                                } else {
                                    ?><div class="alert alert-warning"><?php
                                    echo '<p>' . $error_message . '</p>';
                                    ?></div><?php
                                }
                            ?>

                    </div>

                </div>

                    <form class="form-horizontal" method="post" role="form">
                          <div class="form-group">
                            <label for="name" class="col-sm-4 control-label">Name</label>
                            <div class="col-sm-4">
                              <input type="text" class="form-control" id="name" name="name" placeholder="Name" value="<?php if (isset($name)) { echo htmlspecialchars($name); } ?>">
                            </div>
                          </div>
                          <div class="form-group">
                            <label for="email" class="col-sm-4 control-label">Email</label>
                            <div class="col-sm-4">
                              <input type="email" class="form-control" id="email" name="email" placeholder="Email" value="<?php if(isset($email)) { echo htmlspecialchars($email); } ?>">
                            </div>
                          </div>
                          <div class="form-group">
                            <label for="message" class="col-sm-4 control-label">Message</label>
                            <div class="col-sm-4">
                            <textarea class="form-control" id="message" name="message" rows="3"><?php if (isset($message)) { echo htmlspecialchars($message); } ?></textarea>
                            </div>
                          </div>
                          <div class="form-group" style="display:none;">
                            <label for="address" class="col-sm-4 control-label">Address</label>
                            <div class="col-sm-4">
                              <input type="text" class="form-control" id="address" name="address" placeholder="Name">
                              <p>Humans: please leave this field blank.</p>
                            </div>
                          </div>
                          <div class="form-group">
                            <div class="col-sm-offset-4 col-sm-">
                              <button type="submit" class="btn btn-primary">Submit</button>
                            </div>
                          </div>
                    </form>


                <?php } ?>

            </div>

        </div>

    <?php require_once("../inc/footer.php"); ?>

3 个答案:

答案 0 :(得分:0)

更改此

$mail->From =  $email;
$mail->FromName = $name;
$mail->addAddress('$$$$$$$$$$$$$$', '$$$$$$$$$$l');

$mail->setFrom($email, $name);
$mail->addAddress($EMAIL THAT ITS GOING TO, $NAME THATS ITS GOING TO);

另请参阅使用GMAIL发送FROM其他地址的此链接

https://support.google.com/mail/answer/22370?hl=en

答案 1 :(得分:0)

看起来您忘了调用Sender变量:

public $Sender = '';

/**
 * The Return-Path of the message.
 * If empty, it will be set to either From or Sender.
 * @type string
 */

https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php

答案 2 :(得分:0)

感谢您的回复。我做了一些外部研究,结果证明这是gmail本身的一个问题。他们不允许您使用与您的Gmail帐户不同的发件人地址的smtp服务。我认为这是因为垃圾邮件。