0

I am trying to send an email using but unable to do it, whereas I am getting data into MySQL database.

I have posted my whole code, please check and let me know where's the mistake.

$objConnect = mysql_connect("localhost","username","pwd");
$objDB = mysql_select_db("database");

$strName = $_POST["name"];
$strEmail = $_POST["email"];

$to      = $strEmail;
$subject = 'the subject';
$message = 'hello';
$headers = 'From: mymail@gmail.com' . "\r\n" .
'Reply-To: mymail@gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

$strSQL = "insert into data (name, email) VALUES ('".$strName."','".$strEmail."')";
$objQuery = mysql_query($strSQL);

if(!$objQuery)
    {
        $arr['StatusID'] = "0"; 
        $arr['Message'] = "Cannot save data!";  
    }
    else
    {
        mail($to, $subject, $message, $headers);
        $arr['StatusID'] = "1"; 
        $arr['Message'] = "Data saved successfully";    
    }

    mysql_close($objConnect);

    echo json_encode($arr);
mega6382
  • 9,211
  • 17
  • 48
  • 69
Sun
  • 6,768
  • 25
  • 76
  • 131
  • Do `var_dump(mail($to, $subject, $message, $headers))` and see what it is returning. – Sougata Bose Dec 02 '15 at 06:20
  • are you doing the test locally? – jameshwart lopez Dec 02 '15 at 06:20
  • @Sougata getting Unable to upload – Sun Dec 02 '15 at 06:39
  • I am testing it on live server @jameshwartlopez – Sun Dec 02 '15 at 06:44
  • @Sun I think its worth checking if you have a php error and also check if your else code that contains your mail code has really been executed. – jameshwart lopez Dec 02 '15 at 06:54
  • Your primary mistake is calling `mail` yourself, and thus making yourself responsible for a very large amount of work to format your messages correctly and protect yourself from attack (which you're currently not doing at all). Don't do it; use a library like [PHPMailer](https://github.com/PHPMailer/PHPMailer) that you tagged this question with, and base your code on one of the examples provided. Meanwhile, read your mail server's log, usually in `/var/log/mail.log`. – Synchro Dec 02 '15 at 08:02

2 Answers2

0

This code is really working ...

<?php
    $to      = 'to_email@gmail.com';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: mymail@gmail.com' . "\r\n" .
    'Reply-To: mymail@gmail.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
    $errLevel = error_reporting(E_ALL ^ E_NOTICE);  // suppress NOTICEs
    if(!mail($to, $subject, $message, $headers)){
        error_reporting($errLevel);
    }
?>
0

You have not added mail() function in your code then how will it send the mail and to send email from localhost it should be properly configured. Below code will work on the server.

$objConnect = mysql_connect("localhost","username","pwd");
$objDB = mysql_select_db("database");
$strName = $_POST["name"];
$strEmail = $_POST["email"];
$to      = $strEmail;
$subject = 'the subject';
$message = 'hello';
$headers = 'From: mymail@gmail.com' . "\r\n" .
           'Reply-To: mymail@gmail.com' . "\r\n" .
           'X-Mailer: PHP/' . phpversion();
if(!mail($to, $subject, $message, $headers)){
  echo 'Mail cant be send';
}
else
{
$strSQL = "insert into data (name, email) VALUES ('".$strName."','".$strEmail."')";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
   {
    $arr['StatusID'] = "0"; 
    $arr['Message'] = "Cannot save data!";  
}
else
{
    mail($to, $subject, $message, $headers);
    $arr['StatusID'] = "1"; 
    $arr['Message'] = "Data saved successfully";    
}
echo json_encode($arr);
  }
   mysql_close($objConnect);
mega6382
  • 9,211
  • 17
  • 48
  • 69