简体   繁体   中英

E-mail form not sending 'sender email'

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 . "\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.";
        }
    }
}
?>

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' . "\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.";
}
}
}
?>

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

Try this:

  <?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.";
}
}
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM