Using Gmail SMTP to send email with PHP
Asked Answered
F

4

4

I have a problem I have been working on for about a week and can't find an answer. As a preface to this all, I have searched the internet for all sorts of things. There are a lot of answers for this problem, but none seem to be helping me.

I am somewhat new to PHP and a lot of the stuff I am asking for (been using it over the past few months). Let me get to the base of the problem:

I am on a school network with my own server set up in my dorm room. I am creating a website where I need to verify a user's email, but the basic PHP mail() function does not work. I have been told that I will need to use SMTP. So I decided the easiest and cheapest way was with Gmail SMTP. I created an account on Gmail called [email protected] for this reason. Here is the code.

echo "starting mail sending";
             require_once("pear/share/pear/Mail.php");

echo "1";

$from = "PersonA `<[email protected]`>";   $to = "`<[email protected]`>";   $subject = "Activate your account";   $body = "Hey";  

$host = "ssl://smtp.gmail.com";   $port = "465"; //also tried 587   $username = "[email protected]";   $password = "password";  

echo "2";

$headers = array ('From' => $from,    'To' => $to,    'Subject' => $subject);

echo "3";

$mailer_params['host'] = $host;   $mailer_params['port'] = $port;   $mailer_params['auth'] = true;   $mailer_params['username'] = $username;   $mailer_params['password'] = $password;                                              
                 $smtp = Mail::factory('smtp', $mailer_params);

echo "4";

error_reporting(E_ALL);

echo "5";

if (PEAR::isError($smtp)) {   die("Error : " . $smtp->getMessage()); }

echo "6";

$mail = $smtp->send($to, $headers, $body) or die("Something bad happened"); 

echo "7";

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

So basically the code just stops at the line:

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

I have tried printing errors, but I just have no idea what to do now. Any tips and help is appreciated. Thanks!!

Farceur answered 22/4, 2011 at 19:50 Comment(0)
I
3

Use this class: http://www.phpclasses.org/package/14-PHP-Sends-e-mail-messages-via-SMTP-protocol.html

The sample code I use:

require("smtp/smtp.php");
require("sasl/sasl.php");
$from = '[email protected]';
$to = '[email protected]';

$smtp=new smtp_class;
$smtp->host_name="smtp.gmail.com";
$smtp->host_port='465';
$smtp->user='[email protected]';
$smtp->password='XXXXXXXXX';
$smtp->ssl=1;
$smtp->debug=1;       //0 here in production
$smtp->html_debug=1; //same

$smtp->SendMessage($from,array($to),array(
"From: $from",
"To: $to",
"Subject: Testing Manuel Lemos' SMTP class",
"Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z")
),
"Hello $to,\n\nIt is just to let you know that your SMTP class is working just fine.\n\nBye.\n"));
Innovate answered 23/4, 2011 at 7:26 Comment(2)
Yeah that was stupid of me. Anyways, where do I get the sasl.php file? Only smtp.php is included on the page that you linked. Or is it on my server already? I have SASL enabled and used a Ubuntu tutorial to enable SMTP and SASL authorization for my postfix server. Thanks!Farceur
The link to SASL package was somewhere on that page. Here is the direct link: phpclasses.org/package/…Innovate
A
0

If you are you using Linux I recommend setting up postfix on your local machine and they asking that to relay the email forwards via an external SMTP service, in your case Gmail.

http://bookmarks.honewatson.com/2008/04/20/postfix-gmail-smtp-relay/

The reason is that your PHP script can timeout incase there's a delay contact Gmail. So you would use Postfix to queue the email on the local server, let the PHP script execution die and trust Postfix to send the email via Gmail.

If you are using Windows, I am sure you can find an equivalent SMTP relay application (should be built as a rough guess).

Assail answered 22/4, 2011 at 19:54 Comment(0)
V
0

Many public networks block connections on the SMTP port to remote machines to stop spammers inside their network.

You have two choices:

  1. Find a smtp server that uses a different port than 25
  2. Use the official SMTP server of your school's network. There is always one since people need to send mails out.
Veiled answered 22/4, 2011 at 20:46 Comment(0)
S
0

I always use this to send mails using gmail as SMTP server.
Very Simple to configure

Snapback answered 23/4, 2011 at 7:32 Comment(2)
When I try to unzip the download from the link you provided, it says it is a bad zip file because it has an extra byte in it or something like that. I am using Ubuntu 10.10.Farceur
I just tried it on my Mac and it worked fine. No idea why it didn't work on my server.Farceur

© 2022 - 2024 — McMap. All rights reserved.