15

I am trying to use PHP's mail() function to send a test mail.

$to = "****@gourab.me";
$sub = "Php Mail";
$msg = "Test Message From PHP";

mail($to, $sub, $msg, "From: **********@gmail.com");

When I try to debug it through step in phpdbg, it shows the message:

[PHP Warning: mail(): " sendmail_from" not set in php.ini or custom "From:" header 
missing in C:/xampp/htdocs/tinyProj/mail.php on line 4]

I cannot understand why?

Ikari
  • 3,176
  • 3
  • 29
  • 34

4 Answers4

41

It seems your From header is not correctly formatted. Try this instead:

$headers =  'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'From: Your name <info@address.com>' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

mail($to, $subject, $body, $headers);
Mohammad Masoudian
  • 3,483
  • 7
  • 27
  • 45
fillobotto
  • 3,698
  • 5
  • 34
  • 58
  • 2
    I tried that but now phpdbg is showing `[PHP Warning: mail(): Failed to connect to mailserver at localhost port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()` – Ikari Jan 19 '15 at 14:45
  • Are you on a live server or just your PC? – fillobotto Jan 19 '15 at 15:05
  • I'm on my PC with XAMPP – Ikari Jan 19 '15 at 15:45
  • 1
    That's the problem, sendmail/postfix service is not running – fillobotto Jan 19 '15 at 15:50
  • It's off the main topic, you should open different question. Try the code on a shared/dedicated server or virtual machine or whatever and see it works. – fillobotto Jan 19 '15 at 16:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69145/discussion-between-gourab-nag-and-fillobotto). – Ikari Jan 19 '15 at 17:52
5

Bro it seems that you're using you own PC/localhost/127.0.0.1 server that's why you can't connect to SMTP server. You can only send mail from live server using similar coding with some amendments :) i.e. add one parameter "Header/From".

mail("You@me.com","Answer","Hope You Vote My Answer Up","From: me@you.com");
Mohammad Hani
  • 459
  • 5
  • 14
0
<?php
$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";

mail($to,$subject,$txt,$headers);
?>
Whirlwind
  • 14,286
  • 11
  • 68
  • 157
0
<?php
if(isset($_POST['send'])){
     $from =  $_POST['femail'];
     $phoneno = $_POST['phoneno'];
     $message = $_POST['message'];
     $carrier = $_POST['carrier'];
     if(empty($from)){
       echo("enter the email");
       exit();

     } 
else if(empty($phoneno)){
   echo("enter the phone no");
     exit();
   }
 elseif(empty($carrier)){
   echo("enter the specific carrier");
   exit();
    }
 else if(empty($message)){
  echo("enter the message");
  exit();
  }
  else{
     $message = wordwrap($message, 70);
     $header = $from;
     $subject = 'from submission';
     $to = $phoneno.'@'.$carrier;
     $result = mail($to, $subject, $message, $header);
     echo("message sent to".$to);

  }

  }
?>
  • Dear Yusuph, when answering this kind of question you should try to be more direct, the question says it is receiving an error message but it seems to be alright with code. – Alessandro Oliveira Sep 06 '17 at 21:38