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

时间:2015-12-04 12:54:39

标签: php email

我正在使用我在教程中找到的联系表单脚本,它运行正常。主题和消息联系人通过,但发件人电子邮件不会。 发件人姓名和电子邮件不会通过。当我检查从表单收到的电子邮件时,它在收件箱中显示“未知发件人”,当我查看该邮件时,它说它是通过主机发送的。

任何人都可以看到我的错误吗?我对联系表单没有太多经验,需要很快完成客户端站点。

表格的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,发送电子邮件说明来自{{1}这是我的提供商的问题吗?

目前的代码:

me@webhost.com

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

试试这个:

dateWithString:formatString: