1

I am using first time mail function in php i get a code from online and it implemented to my program but its not working. Can any please help me to correct this ?

  <?php
     $to = "ganesh.sunraise@gmail.com";
     $subject = "This is subject";

     $message = "<b>This is HTML message.</b>";
     $message .= "<h1>This is headline.</h1>";

     $header = "From:lokesh.sunraise111@gmail.com ";

     $retval = mail ($to,$subject,$message,$header);

     if( $retval == true ) {
        echo "Message sent successfully...";
     }else {
        echo "Message could not be sent...";
     }
  ?>
L. Ganesh
  • 11
  • 1
  • Is your server set up to send emails? Without something installed a server won't just send mail – treyBake May 18 '18 at 06:03
  • Please you can use PHPMailer through this you can send mail easily – Boni May 18 '18 at 06:05
  • 2
    Possible duplicate of [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) –  May 18 '18 at 06:09
  • this is run on a remote server, not locally? –  May 18 '18 at 06:10
  • @Boni PHPMailer isn't 'easier' - the mail function is so easy to use ... – treyBake May 18 '18 at 07:45

3 Answers3

0

First, please tell, are you getting "Message sent successfully.." message.

There are many reasons for mail is not working:

  1. Your server does not support send mail.

Please try below method:

 <?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);
 ?>

or use:

PHPmailer

Therichpost
  • 1,759
  • 2
  • 14
  • 19
0

You need to setup the email server locally, or you can use 3-rd party email server to send email from your web server. Added the following lines to your php.ini to send email using gmail.

smtp_server=smtp.gmail.com
smtp_port=465
smtp_ssl=auto
error_logfile=error.log
auth_username=your-gmail-username@gmail.com
auth_password=your-gmail-password
0

You can use SMTP as the others suggested, and this can be done using the PHPMailer library.

However, there's another suggestion and this is the one that I prefer (because SMTP may contain some problems that you've to search about them and fix them).

The other suggestion is to use a PHP mailling API like SendGrid or Mailgun - (I prefer SendGrid).

Basic SendGrid integration with PHP:

  1. Download SendGrid from here https://github.com/sendgrid/sendgrid-php/releases/download/v6.2.0/sendgrid-php.zip - or follow the installation instructions for another ways for installation.

  2. Unzip the SendGrid-PHP library and upload it to your server.

  3. Use this sample code (edit it for your needs): https://github.com/sendgrid/sendgrid-php#quick-start

Mario
  • 1,374
  • 6
  • 22
  • 48