0

I have php code intended to send email from my website. However the email I got from my website is not complete{"phone,subject etc are missing"}. I can only see sender email and message they send.

I tried to fix the problem as i'm new to php i can't figure it out what is causing this problem.

$product = $_POST["product"];
$name = $_POST["name"];
$email= $_POST["email"];
$phone= $_POST["phone"];
$subject= $_POST["subject"];
$message= $_POST["message"];


$emailSubject="Emaill Subject" .$subject;
$txt = "You have recived Inquiry from:$name.\n".
        "Product: $product.\n".
        "Product: $product.\n".
        "Phone: $phone".
        "Message: $message.\n";
$to = 'info@badestrading.com'; 
$headers = "Mail From: $email \r\n" ;  // Sender's E-mail         

mail($to, $subject, $txt, $headers);


if (@mail($to, $subject, $txt, $headers))
{
    echo 'The message has been sent.';
}else{
    echo 'failed';
}

Email: xyz@gmail.com phone: Text: Testing contact form

This is the email I got.

  • Do a `var_dump($_POST);` at the very beginning of the script, and verify that all the values you are trying to access in there actually exist. – 04FS Jul 02 '19 at 10:41
  • Yes they actually exists in var_dump(); and gives output as follow: array(7) { ["product"]=> string(3) "asd" ["name"]=> string(16) "Abhinash Mainali" ["email"]=> string(25) "mainaliabhinash@gmail.com" ["Phone"]=> string(13) "+977528953223" ["subject"]=> string(7) "Saffron" ["message"]=> string(3) "asd" ["submit"]=> string(0) "" } Notice: Undefined index: phone in C:\xampp\htdocs\qoute.php on line 7 – Abhinash Mainali Jul 02 '19 at 11:30
  • `Phone` != `phone` The rest should work, if the data is actually what you shown. – 04FS Jul 02 '19 at 11:39
  • yes u phone index not matched here, not defined – devpro Jul 02 '19 at 11:40
  • _“This is the email I got.”_ - so you are not even seeing the “static” parts of the message, such as “You have recived Inquiry from”, in what you receive? What do you get when you make a debug output directly inside this script, `echo htmlspecialchars($txt);` …? – 04FS Jul 02 '19 at 11:42
  • Thank you @04FS but i have a doubt why i'm getting this warning: Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing in C:\xampp\htdocs\qoute.php on line 21 failed – Abhinash Mainali Jul 02 '19 at 11:45
  • Please go and _research_ error messages before you ask. https://stackoverflow.com/questions/28026932/php-warning-mail-sendmail-from-not-set-in-php-ini-or-custom-from-head, https://stackoverflow.com/questions/19243826/php-mail-results-in-error-header-missing – 04FS Jul 02 '19 at 11:47
  • Using debug echo htmlspecialchars($txt): i can see the msg as:You have recived Inquiry from:Abhinash Mainali. Product: asd. Product: asd. Phone: +971528953223Message: asdf. But in actual email it is not there. Should I try changing the email to different email address? – Abhinash Mainali Jul 02 '19 at 11:52

1 Answers1

0

Try This

$product = $_POST["product"];
$name = $_POST["name"];
$email= $_POST["email"];
$phone= $_POST["phone"];
$subject= $_POST["subject"];
$message= $_POST["message"];


$emailSubject="Emaill Subject" .$subject;
$txt = "You have recived Inquiry from:$name.\n".
        "Product: $product.\n".
        "Product: $product.\n".
        "Phone: $phone".
        "Message: $message.\n";
$to = 'info@badestrading.com'; 
$headers = "From: $email \r\n" ;  // Sender's E-mail         

mail($to, $subject, $txt, $headers);


if (@mail($to, $subject, $txt, $headers))
{
    echo 'The message has been sent.';
}else{
    echo 'failed';
}

I changed Mail From: to From:

Moshe Gross
  • 1,206
  • 1
  • 7
  • 14