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 = "test@gmail.com";
$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: <xyz@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\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
  • check [this](http://stackoverflow.com/questions/35932835/simple-php-form-doesnt-seem-to-be-working/35932962?noredirect=1#comment59565312_35932962) – Yash Mar 14 '16 at 04:57
  • in $to I have passed emails. But I did not show it here. and in mail function I remove the $form and tried it but still I am not able to send email @Anant – Azad Chauhan Mar 14 '16 at 05:01
  • @Anant According to the `mail()` documentation, `mail()` accepts 5, the fifth being `additional_parameters` *(optional)* though I don't know if that 5th parameter is correctly used here...hard to say. – Rasclatt Mar 14 '16 at 05:01
  • @AzadChauhan Have you tried using basic input for the mail function to see if that fails? `echo mail('my@email.com','Subject','Message','From: my@email.com');` for example (`my@email.com` being your email of course)? That probably should have been the first thing to try. – Rasclatt Mar 14 '16 at 05:05
  • please check now its full code of send email @Anant – Azad Chauhan Mar 14 '16 at 05:15
  • @Anant ok I am checking – Azad Chauhan Mar 14 '16 at 05:27
  • Ok, check please and let us know. Also do everything what suggested. – Alive to die - Anant Mar 14 '16 at 05:27
  • @Anant nothing work bro :( – Azad Chauhan Mar 14 '16 at 05:35

4 Answers4

0

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

 <?php
    $to      = 'nobody@example.com';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: webmaster@example.com' . "\r\n" .
               'Reply-To: webmaster@example.com' . "\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 youremail@gmail.com

Then try following code.

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

$to = "test@gmail.com";
$subject = "Property Posted";
$message="TestMessage";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";

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 = "abc@gmail.com";
$mail_from = "efg@gmail.com";

//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      = 'nobody@example.com';
 $subject = 'the subject';
 $message = 'hello';
 $headers = 'From: abc@example.com' . "\r\n" .
       'Reply-To: abc@example.com' . "\r\n" .
       'X-Mailer: PHP/' . phpversion();

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