2

My company handover me a existing project develop by somebody else. I have to fix some issues in it but in one of the issue I got stuck. Emails are not sending from the website. I am not sure why this is happening. The code is just simple php email function which sent email. But still it is not working anybody can guess what I am missing?

$to = "[email protected]";
$subject = "Property Posted";

$message="TestMessage"
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$form="www.xxxxxxxxxxxxxxxxxxxxx.com";

// More headers    
$headers .= 'From: <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";

$checkmail = mail($to,$subject,$message,$headers,$form);
if($checkmail) {
    echo "email sent";
} else {
    echo failed"
}

Its always hits the else, though I don't know why.

Priyanka Maurya
  • 385
  • 1
  • 10

4 Answers4

0

You didn't define $to and $subject. Please check below code:

 <?php
    $to      = '[email protected]';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: [email protected]' . "\r\n" .
               'Reply-To: [email protected]' . "\r\n" .
               'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);
  ?>
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
Dhaval Patel
  • 1,076
  • 6
  • 19
0

Test server mail service by executing following command on command line.

echo "message" | mail -s subject [email protected]

Then try following code.

error_reporting(E_ALL);
ini_set('display_errors',1);

$to = "[email protected]";
$subject = "Property Posted";
$message="TestMessage";
$headers = "From: [email protected]" . "\r\n" .
"CC: [email protected]";

mail($to,$subject,$message,$headers);
Navid
  • 894
  • 7
  • 14
0

some times email will go to spam so Another possibility is to install mail and net_smtp through pear.

pear install Mail
pear install Net_Smtp

then you have the possibility to send mail with SMTP authentication to another server:

require_once "Mail.php";

$body = "messages in body part\n";
$subject = "Subject of email message";
$mail_to = "[email protected]";
$mail_from = "[email protected]";

//SMTP Configuration
$host = "smtp.server.com""; // Or IP address";
$username = "username";
$password = "password";

$smtp = Mail::factory('smtp',
 array (
 'host' => $host,
 'auth' => true,
 'username' => $username,
 'password' => $password
));

$headers = array (
 'From' => $mail_from,
 'To' => $mail_to,
 'Subject' => $subject
);
$mail = $smtp->send($mail_to, $headers, $body);

if (PEAR::isError($mail)) {
 echo "Cound not seend the message";
}
Nitheesh K P
  • 1,090
  • 8
  • 17
-1

Using PHP's mail() function it's possible. Remember mail function will not work in Local server

<?php
 $to      = '[email protected]';
 $subject = 'the subject';
 $message = 'hello';
 $headers = 'From: [email protected]' . "\r\n" .
       'Reply-To: [email protected]' . "\r\n" .
       'X-Mailer: PHP/' . phpversion();

 mail($to, $subject, $message, $headers);
 ?>
yogesh chatrola
  • 429
  • 4
  • 11