How to send email in PHP

3 Answers

0 votes
$to      = 'recipient@email_server.com';             
$subject = 'Email Subject';                    
$message = "The email message";      
$headers = implode("\r\n", [
    'From: Virginia <virginia@example.com>',
    'Reply-To: no-reply@email_server.com',
    'X-Mailer: PHP/' . PHP_VERSION
]);

$result = mail($to, $subject, $message, $headers);

echo $result;

  
/*
run: 
 
1
   
*/

 



answered Sep 18, 2017 by avibootz
0 votes
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");

ini_set("mail.log", "/tmp/mail.log");
ini_set("mail.add_x_header", TRUE);

$to      = 'recipient@email_server.com';
$subject = 'Email Subject';
$message = 'The email message';
$headers = implode("\r\n", [
    'From: test@example.com',
    'Reply-To: test@example.com',
    'X-Mailer: PHP/' . PHP_VERSION
]);

$result = mail($to, $subject, $message, $headers);

if ($result) 
     echo "Email sent";
else 
    echo "Error: Email was not sent";
    

  
/*
run: 
 
Email sent
   
*/ 

 



answered Sep 18, 2017 by avibootz
0 votes
error_reporting(-1);
ini_set('display_errors', 'On');
echo "<pre>";
set_error_handler("var_dump");

ini_set("mail.log", "/tmp/mail.log");
ini_set("mail.add_x_header", TRUE);

$to      = 'test@email_server.com';
$subject = 'Email Subject';
$message = 'The email message';
$headers = implode("\r\n", [
    'From: test@example.com',
    'Reply-To: test@example.com',
    'X-Mailer: PHP/' . PHP_VERSION
]);

$result = mail($to, $subject, $message, $headers);
echo "<pre>";

if ($result) 
     echo "Email sent";
else 
    echo "Error: Email was not sent";
    

  
/*
run: 
 
int(2)
string(62) "mail(): SMTP server response: 550 A valid address is required."
string(38) "C:\xampp\htdocs\allonpage.com\test.php"
int(20)
array(10) {
  ["_GET"]=>
  array(1) {
    ["user"]=>
    string(3) "tim"
  }
  ["_POST"]=>
  array(0) {
  }
  ["_COOKIE"]=>
  array(2) {
    ["src"]=>
    string(1) "1"
    ["DBGSESSID"]=>
    string(2) "-1"
  }
  ["_FILES"]=>
  array(0) {
  }
  ["_SERVER"]=>
  array(78) {
    ["ALLUSERSPROFILE"]=>
    string(14) "C:\ProgramData"
    ["APPDATA"]=>
   ...
  }
  ["_ENV"]=>
  array(0) {
  }
  ["to"]=>
  string(21) "test@email_server.com"
  ["subject"]=>
  string(13) "Email Subject"
  ["message"]=>
  string(17) "The email message"
  ["headers"]=>
  string(72) "From: test@example.com
Reply-To: test@example.com
X-Mailer: PHP/5.5.13"
}
Error: Email was not sent
   
*/    

 



answered Sep 18, 2017 by avibootz
...