0

I have an existing email.PHP that sends emails based on certain functions with in my website, similar to forum notifications.

However its very hit n miss with sending them, worked well for two days then just stopped.

My question is is there a simple way to add smtp instructions to the existing script so that it will use Gmail rather than PHP mail()

I can supply current script if required.

Thanks in advance.

  • Why not use [PHPMailer](https://code.google.com/a/apache-extras.org/p/phpmailer/)? – Kermit Feb 23 '13 at 22:51
  • there is topic on that already http://stackoverflow.com/questions/712392/send-email-using-gmail-smtp-server-from-php-page – mkungla Feb 23 '13 at 22:58

1 Answers1

0

You need to install PEAR Mail. Then you can do it like this.

<?php

       require_once "Mail.php";

        $from = "<from@gmail.com>";
        $to = "<to@yahoo.com>";
        $subject = "Hi!";
        $body = "Hi,\n\nHow are you?";

        $host = "ssl://smtp.gmail.com";
        $port = "465";
        $username = "myaccount@gmail.com";  //<> give errors
        $password = "password";

        $headers = array ('From' => $from,
          'To' => $to,
          'Subject' => $subject);
        $smtp = Mail::factory('smtp',
          array ('host' => $host,
            'port' => $port,
            'auth' => true,
            'username' => $username,
            'password' => $password));

        $mail = $smtp->send($to, $headers, $body);

        if (PEAR::isError($mail)) {
          echo("<p>" . $mail->getMessage() . "</p>");
         } else {
          echo("<p>Message successfully sent!</p>");
         }

    ?>
Ares
  • 5,905
  • 3
  • 35
  • 51
  • Turns out the issue I was having was trying to send emails from my domain to my googleapps account which was the same address ...but thank you for the feedback. – SugaComa - Lead Idiot Feb 28 '13 at 15:37