提问者:小点点

电子邮件表单不发送'发件人电子邮件'


我正在使用一个我在教程中找到的联系人表单脚本,它工作得很好。主题和消息联系人可以通过,但发件人电子邮件不能通过。发件人姓名和电子邮件不能通过。当我检查从表单收到的电子邮件时,它在收件箱中显示“未知发件人”,当我查看邮件时,它显示它是通过主机发送的。

有人能看出我在这方面出了什么问题吗?我没有太多的接触形式的经验,需要它完成一个客户端网站很快。

表单的HTML如下所示:

<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代码在这里:

<?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 . "\r\n"; // Sender's Email
            $headers .= 'Cc:'. $email2 . "\r\n"; // 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.";
        }
    }
}
?>

如果我用email@provider.com从条目中,已发送邮件将其替换为@webhost。com示例:我输入me@gmail.com发送的电子邮件说它来自me@webhost.com这是我的提供商的问题吗?

当前代码:

<?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' . "\r\n"; // Sender's Email
$headers .= 'Cc: chad' . "\r\n"; // 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个答案

匿名用户

请尝试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);
}

匿名用户

试试这个:

  <?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

if (filter_var($email, FILTER_VALIDATE_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:'. $email . "\r\n"; // Sender's Email
$headers .= 'Cc:'. $email . "\r\n"; // 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.";
}
}
}
?>