0

I have two domain name. For example https://example.com and https://example2.com.

I am sending test mail using mail function.

I am able to send the email from https://example.com and I am getting the success but the same code I am using for https://example2.com and I am not getting the email it's always calling the else part.

Test mail

<?PHP
$sender = 'xxx@xx.com';
$recipient = 'zzz@zz.com';

$subject = "php mail test";
$message = "php test message";
$headers = 'From: ' . $sender . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html;charset=UTF-8" . "\r\n";

if (mail($recipient, $subject, $message, $headers))
{
    echo "Message accepted";
}
else
{
echo "Error: Message not accepted";
$errorMessage = error_get_last()['message'];
echo $errorMessage;
}
?>
user9437856
  • 2,360
  • 2
  • 33
  • 92
  • 1
    `$headers .= "MIME-Version: 1.0" . "\r\n";` you forgot to concatenate $headers – Saeed M. Oct 16 '18 at 06:03
  • 1
    You're going to have to provide an error of some sort for us to help you. [Try this](https://stackoverflow.com/questions/3186725/how-can-i-get-the-error-message-for-the-mail-function). And the `From` bit is overwritten in your header string (not using `.=` for MIME) – rkeet Oct 16 '18 at 06:05
  • Yes, I updated that still same issue. @smoqadam thanks for the notice. – user9437856 Oct 16 '18 at 06:06
  • @rkeet, I update the code with $errorMessage = error_get_last()['message']; but it's not displaying any error. I am getting only echo "Error: Message not accepted"; – user9437856 Oct 16 '18 at 06:10
  • Have your try [this](http://fasterland.net/sending-email-via-php-centos-7-using-sendmail.html) – Ngoc Nam Oct 16 '18 at 06:10
  • @NgocNam, I am using Hostgator and GoDaddy for hosting. There is no issue with Hostgator hosting. I am not able to send from GoDaddy hosting – user9437856 Oct 16 '18 at 06:15
  • If everything is ok, then check with your hosting provider. – Gaurav Kandpal Oct 16 '18 at 06:26

3 Answers3

1

The From header is not valid as the next one is concatenated right after it:

$headers = 'From:' . $sender;
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

Should be something like:

// add \r\n
$headers = 'From: ' . $sender . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html;charset=UTF-8" . "\r\n";
jeroen
  • 91,079
  • 21
  • 114
  • 132
0

Check if mail function is enabled on example2.com by using below:

if ( function_exists( 'mail' ) )
{
    echo 'mail() is available';
}
else
{
   echo 'mail() has been disabled';
}

If disabled, enable it via php.ini or in cPanel.

Mike Doe
  • 16,349
  • 11
  • 65
  • 88
Twinkle
  • 1
  • 1
0

Try this simple php mail function

$to      = $email_id;
$subject = "";
$message = "Your mail Body Content. you also use here HTML Tags for better performence. ";
$headers = 'From: xyz@domainname.com' . "\r\n" .
  'Reply-To: xyz@domainname.com' . "\r\n" .
  'X-Mailer: PHP/' . phpversion();
$headers.= "MIME-Version: 1.0" . "\r\n";
$headers.= "Content-type:text/html;charset=UTF-8" . "\r\n";

if(mail($to, $subject, $message, $headers)){
   echo "Message accepted";
}else{
   echo "Error: Message not accepted";
}
Sachin
  • 789
  • 5
  • 18