doumi1884 2015-12-04 12:54
浏览 52

电子邮件表单不发送“发件人电子邮件”

I am using a contact form script I found on a tutorial, and it's working fine. The subject and message contact goes through, however the sender email doesn't. The sender name and email do not come through. When I check the email received from the form it says 'unknown sender' in the inbox, and when I view the message it says it was sent via the host.

Can anyone see where I am going wrong with this? I don't have much experience with contact forms and need it finished shortly for a client site.

HTML for the form is here:

<form action="#" id="form" method="post" name="form">
   <input name="vname" placeholder="Your Name" type="text" value="">
    <input name="vemail" placeholder="Your Email" type="text" value="">
   <input name="sub" placeholder="Subject" type="text" value="">
   <label>Your Suggestion/Feedback</label>
   <textarea name="msg" placeholder="Type your text here...">
    </textarea>
    <input id="send" name="submit" type="submit" value="Send Feedback">
</form>
<h3><?php include "secure_email_code.php" ?>
</h3>

PHP code here:

<?php
if(isset($_POST["submit"])){
    // Checking For Blank Fields..
    if($_POST["vname"]=="" || $_POST["vemail"]=="" || 
       $_POST["sub"]=="" || $_POST["msg"]=="")
    {
        echo "Fill All Fields..";
    }else{
        // Check if the "Sender's Email" input field is filled out
        $email=$_POST['vemail'];
        // Sanitize E-mail Address
        $email =filter_var($email, FILTER_SANITIZE_EMAIL);
        // Validate E-mail Address    
        $email= filter_var($email, FILTER_VALIDATE_EMAIL);
        if (!$email){
            echo "Don't forget to include your email adress! Otherwise we can't get back to you.";
        }
        else{
            $subject = $_POST['sub'];
            $message = $_POST['msg'];
            $headers = 'From:'. $email2 . "
"; // Sender's Email
            $headers .= 'Cc:'. $email2 . "
"; // Carbon copy to Sender
            // Message lines should not exceed 70 characters (PHP rule), so wrap it
            $message = wordwrap($message, 70);
            // Send Mail By PHP Mail Function
            mail("marc@example.com", $subject, $message, $headers);
            echo "Thanks for getting in touch! We'll get back to you ASAP.";
        }
    }
}
?>

If I hardcode the headers with something like email@provider.com from the entry, the sent mail replaces it with @webhost.com Example: I enter me@gmail.com and the send email says it's from me@webhost.com Is this an issue with my provider?

Code at the moment:

<?php
if(isset($_POST["submit"])){
// Checking For Blank Fields..
if($_POST["vname"]==""||$_POST["vemail"]==""||$_POST["sub"]==""||$_POST["msg"]==""){
echo "Fill All Fields.";
}else{
// Check if the "Sender's Email" input field is filled out
$email=$_POST['vemail'];
// Sanitize E-mail Address
$email =filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email= filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email){
echo "Don't forget to include your email adress! Otherwise we can't get back to you.";
}
else{
$subject = $_POST['sub'];
$message = $_POST['msg'];
$headers =  'From:' . 'Ross@gmail.com' . "
"; // Sender's Email
$headers .= 'Cc: chad' . "
"; // Carbon copy to Sender
$from = $headers;
// Message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// Send Mail By PHP Mail Function
mail("marc.murray.92@gmail.com", $subject, $message, $headers);
echo "Thanks for getting in touch! We'll get back to you ASAP.";
}
}
}
?>
  • 写回答

2条回答 默认 最新

  • dongmi5015 2015-12-04 13:15
    关注

    try phpMailer: https://github.com/PHPMailer/PHPMailer

    <?php
    $contact = "email@gmail.com";
        $msg = ob_get_clean();
        $subject = "example";
        $mail = new PHPMailer();
    
    
        $mail->CharSet = 'UTF-8';
        $mail->IsSMTP();
        //$mail->SMTPDebug = 2; this will show you a debug
        $mail->SMTPAuth = true;           // enable SMTP authentication
        $mail->SMTPSecure = "ssl";        // sets the prefix to the servier
        $mail->Host = "smtp.gmail.com";   // sets GMAIL as the SMTP server
        $mail->Port = 465;                // set the SMTP port
        $mail->IsHTML(true);
        //$mail->SMTPDebug = 2;
        //$mail->Timeout = 15;
        $mail->Username = "example@gmail.com";  // GMAIL username
        $mail->Password = "yourpassword";            // GMAIL password
    
        $mail->SetFrom = "example@gmail.com";
        $mail->Subject = $subject;
        $mail->AltBody = "your client doesn't accept HTML";
        $mail->Body = $msg;
    
        $mail->AddAddress($contact, $subject);
        //$mail->MsgHTML($msg);
        // send as HTML
        if (!$mail->Send()) {
            echo "Mail Error" . $mail->ErrorInfo;
        };
        //Pretty error messages from PHPMailer
        unset($mail);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。