php.ini configure to send email from gmail

dmontgom
dmontgom used Ask the Experts™
on
I am a PHP newbie.

How do I configure the php.ini do send email from gmail.

Please show the exact code.

Is this portion correct?

sendmail_path = /usr/sbin/sendmail -i -t

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
HI

I'm not sure if you can do that, but using something like PHPMAiler Class in your scripts, you can very easily send mail through any SMTP server, including gmail.

settings regarding mail in the php.ini are relvant to the built in php mail() function only, and that function is very limited. Have a look at PHPMailer class, much more flexible and does not require too much change in your scripts.

http://sourceforge.net/projects/phpmailer/
 
Kailash AgheraTeam Lead

Commented:
You can't send email from Gmail without SMTP authentication. You can download PHPmailer class files and upload them on your domain. Sample SMTP authenticated script is available at http://forums.webhostingdiscussion.net/index.php/topic,11.0.html

Adjust your port and SMTP as per google's setting. I think they are using 465 port as well as they require SSL connection.

Author

Commented:
I installed PHPmailer using sudo apt-get install libphp-phpmailer.  I restarted apache.

Here is my code.

It does not work.  I suspect the required statement but I am a php newbie.

         require("class.phpmailer.php");

          $mail = new PHPMailer();
         
          $mail->Mailer = "smtp";  
          $mail->Host = "ssl://smtp.gmail.com";  
          $mail->Port = 465;  
          $mail->SMTPAuth = true; // turn on SMTP authentication  
          $mail->Username = "username@gmail.com"; // SMTP username  
          $mail->Password = "password"; // SMTP password  
       
         
          $mail->From     = "yyyy@xxxxx.com";
          $mail->FromName = "yyyyy";
          $mail->AddAddress("xxxxx@xxxxx.com","xxxxxx");
          $mail->AddReplyTo("xxxx@xxxxxxx.com","yyyy");
     
         
          $mail->Subject  =  "Here is the subject";
          $mail->Body     =  "This is the ";
     

          if(!$mail->Send())
          {
             echo "Message was not sent <p>";
             echo "Mailer Error: " . $mail->ErrorInfo;
             exit;
          }

          echo "Message has been sent";
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Can you post exact error message you receive?

Author

Commented:
The error message did not show....but then page rendered fine.  
So neither the "Message was not sent", NOR "Message has been sent" was echoed to screen?

Please post the ENTIRE code of the page

Author

Commented:
<?php include('common/include/header_getting_started_results.php.inc'); ?>

<div class="content_box getting_started">

  <?php include('common/include/navigation.php.inc'); ?>
  <!--[if IE 8]><br /><![endif]-->

  <div class="body">
    <h1>Thank You!</h1>
    <p>Thank you for taking the time to get started with us. </p>
  </div>
 
   Welcome <?php echo $_REQUEST["first_name"]; ?>!<br />
   
   <?php
           require("class.phpmailer.php");

          $mail = new PHPMailer();
         
          $mail->Mailer = "smtp";  
          $mail->Host = "ssl://smtp.gmail.com";  
          $mail->Port = 465;  
          $mail->SMTPAuth = true; // turn on SMTP authentication  
          $mail->Username = "cron@xxxxx.com"; // SMTP username  
          $mail->Password = "xxxxx"; // SMTP password  
       
         
          $mail->From     = "cron@xxxxi.com";
          $mail->FromName = "Cron";
          $mail->AddReplyTo("ddddd@dddd.com","Dy");
     
         
          $mail->Subject  =  "Here is the subject";
          $mail->Body     =  "This is the body ";
     

          if(!$mail->Send())
          {
             echo "Message was not sent <p>";
             echo "Mailer Error: " . $mail->ErrorInfo;
             exit;
          }
 
           
   
   
   


          echo "Message has been sent";
   

   
        $message = "First Name: ".$_REQUEST["first_name"]." Last Name: ".$_REQUEST["last_name"]." Email: ".$_REQUEST["email"]
        ." \n Phone: ".$_REQUEST["phone"]." Company: ".$_REQUEST["company"]." City: ".$_REQUEST["city"]." State: "
        .$_REQUEST["state"]." Website: ".$_REQUEST["URL"]." Source: ". $_REQUEST["source"]
        ." \nMessage: ".$_REQUEST["message"];
       
       
       
       
 
        echo $message;

   ?>
   
   
  <div class="clear"></div>
 
</div>

<?php include('common/include/footer.php.inc'); ?>
Please try to ONLY use the code below in a separate file to test phpmailer and your gmail setting first before you incorporate into your code ( which may cause/introduce other, non-related errors).

<?php
           require("class.phpmailer.php");

          $mail = new PHPMailer();
         
          $mail->Mailer = "smtp";  
          $mail->Host = "ssl://smtp.gmail.com";  
          $mail->Port = 465;  
          $mail->SMTPAuth = true; // turn on SMTP authentication  
          $mail->Username = "cron@xxxxx.com"; // SMTP username  
          $mail->Password = "xxxxx"; // SMTP password          
          $mail->From     = "cron@xxxxi.com";
          $mail->FromName = "Cron";
          $mail->AddReplyTo("ddddd@dddd.com","Dy");        
          $mail->Subject  =  "Here is the subject";
          $mail->Body     =  "This is the body ";
     
          if(!$mail->Send())
          {
             echo "Message was not sent <p>";
             echo "Mailer Error: " . $mail->ErrorInfo;
             exit;
          }
 
            echo "Message has been sent";
   

   ?>
   

If it works, you will see "Message has been sent" echoed on screen, and an e-mail will be sent with the test subject and body.

If not, you should see "Message was not sent" as well as a more verbose SMTP error.

Please post all output if you receive errors.
PS - you will also need to add a recipient address, else you are not going to get far...

$mail->AddAddress("myfriend@example.net");

You can add this anywhere in obove example BEFORE hte if statement, but AFTER the new statement, typically, after the " $mail->From     = "cron@xxxxi.com";" line...

Author

Commented:
ran the code. nothing appeared  No errors....nothing.

created a file called test.php put code in and rand from server.


Then I did a echo before and after require("class.phpmailer.php");

echo "1";
require("class.phpmailer.php");
echo "2";

THe first echo showed.  The second did not.
ok, so it seems PHP may have some issues finding the class.

Im not familiar with the latest versions of phpmailer - the version I still use simply entailed copying the 2 class files in the root folder of your web and including them in your code where you want to send mail from.

I don;t have access to a webserver to test the install of the latest version. It's difficult to say what could be causing your problems - may be a simple php.ini setting wrt accessible folders, or OS filesystem permissions...

I will check as soon as I get home later today.

In the mean time, you might try to locate the mailer class files and place them directly in your "wwwroot" or whatever your root folder is called and then include the file and see if that puts you closer to a working solution.

Author

Commented:
When I moved the classes to root dir.it worked.  Yeup...php is not finding the classes.

Author

Commented:
Thanks for the help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial