0

I am sending mails using php mail function and when an email is sent, it says message sent successfully, but mail is not delivered. I have configured my Server with gmail pop3/SMTP/Imap settings. I am using Ubuntu and here is my Code:

<?php
    $name = "ali";
    $email ="hello";
    $message = "adasdasfasf";
    $from = 'From: from@gmail.com'; 
    $to = 'to@gmail.com'; 
    $subject = 'Customer Inquiry';
    $body = "From: $name\n E-Mail: $email\n Message:\n $message";
        $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html\r\n";
    $headers = 'From: From: from@gmail.com' . "\r\n" .
    'Reply-To: From: from@gmail.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();




            if (mail ($to, $subject, $body, $from)) { 
                echo '<p>Your message has been sent!</p>';
            } else { 
                echo '<p>Something went wrong, go back and try again!</p>'; 
            }

    ?>
Cœur
  • 37,241
  • 25
  • 195
  • 267
De Alion
  • 11
  • 2
  • 1
    What is in your mail server log? – Michael Hampton Nov 17 '15 at 09:26
  • phpMail Function i m using this is error log message Undefined variable: headers in /var/www/html/mailSend.php on line 10 – De Alion Nov 17 '15 at 09:47
  • @DeAlion That's an error from your webserver's error log (like nginx/apache/lighttpd). Take a look at your SMTP server error log. When using the common sendmail, it's usually located under `/var/log/mail.log` or `/var/log/maillog`. – Oldskool Nov 17 '15 at 10:03
  • Possible duplicate of [How to send an email using PHP?](http://stackoverflow.com/questions/5335273/how-to-send-an-email-using-php) – Diamond Nov 17 '15 at 11:50

2 Answers2

0

Your PHP code is bad:

$headers .= "MIME-Version: 1.0\r\n";

Here, first you append to a variable $headers which isn't set earlier (hence the error).

$headers .= "Content-type: text/html\r\n";

Now you extend it.

$headers = 'From: From: from@gmail.com' . "\r\n" .

And now you throw it all away again by overwriting its content, deleting the previously set headers.

So it's not sending because your PHP code yields incomplete headers.

This is more for StackOverflow, but the code should probably be:

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers .= 'From: from@gmail.com' . "\r\n" .
JayMcTee
  • 131
  • 7
  • can yiou write correct code please it will be very nice and helpful and also much more easy to understand – De Alion Nov 17 '15 at 10:08
0

The first time you use the $headers variable is:

$headers .= "MIME-Version: 1.0\r\n";

With .= it wants to add that to an earlier use of that variable, which you don't have. Change that line to:

$headers = "MIME-Version: 1.0\r\n";

to initialize it, and then please share the results.

Paul Russell
  • 281
  • 2
  • 6