Using Gmail SMTP

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

Using Gmail SMTP

Post by sleepydad »

I am a Go Daddy customer, and trying to use gmail's smtp to send emails captured from a form using Swift Mailer. I've been beating my head against the wall for three days now to make this work, and have been unsuccessful. I found the following example script online, but can't get it to work. I get the following error:

"Warning: fsockopen() [function.fsockopen]: unable to connect to smtp.gmail.com:465 "

I've tried smtp.gmail.com:465, smtp.gmail.com:25, smtp.gmail.com:26, smtp.gmail.com:587, smtp.gmail.com:2525 -- no success

I've tried adding 'ssl://' to all of the above -- no success

Assume my:

gmail username: sleepydad@gmail.com
gmail password: sleepydadpassword
from: customer@yahoo.com
to: sleepydad@sleepydad.com

How would I get this code to work ... ?

Code: Select all

<?php

require_once 'lib/swift_required.php';

 

// create the mail transport using the 'newInstance()' method

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465)

->setUsername('myaddress@gmail.com')

->setPassword('mygmailpassword');

 

// create the mailer using the 'newInstance()' method

$mailer = Swift_Mailer::newInstance($transport);

 

// create a simple message using the 'newInstance()' method

$message = Swift_Message::newInstance()

// specify the subject of the message

->setSubject('Testing Swift Mailer')

// specify the From argument

->setFrom(array('us@yahoo.com' => 'us'))

// specify the To argument

->setTo(array('them@sbcglobal.net' =>' them'))

// specify the Cc argument

//->setCc(array('mary@domain.com' => 'Mary Jackson'))

// build the body part of the message

->setBody('Hey, how are you? I am sending you a message with the cool Swift Mailer library');

 

// send the email message

if ($mailer->send($message))

{

echo 'The message was sent successfully!';

}

else

{

echo 'Error sending email message';

}
?> 
My kingdom for a solution. Well, not really but I'd be much obliged!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Using Gmail SMTP

Post by Weirdan »

Code: Select all


    <?php

    require_once 'lib/swift_required.php';

    // create the mail transport using the 'newInstance()' method

    $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'sslv3')
        ->setUsername('sleepydad@gmail.com')
        ->setPassword('sleepydadpassword');



    // create the mailer using the 'newInstance()' method
    $mailer = Swift_Mailer::newInstance($transport);


    // create a simple message using the 'newInstance()' method
    $message = Swift_Message::newInstance()
        ->setSubject('Testing Swift Mailer')
        ->setFrom(array('us@yahoo.com' => 'us'))
        ->setTo(array('sleepydad@sleepydad.com' =>' them'))
        ->setBody('Hey, how are you? I am sending you a message with the cool Swift Mailer library');

    if ($mailer->send($message)) {
        echo 'The message was sent successfully!';
    } else {
        echo 'Error sending email message';
    }
works for me.

Obviously I replaced addresses and login info with my own while testing, but also I had to set encryption kind (third argument to Swift_SmtpTransport::newInstance() as I mentioned in the other thread) to sslv3 (sslv2 seems to be no longer supported at the gmail's end).

Another thing to note is that yahoo from address will be replaced by the address you use to access gmail - and for a good reason.
sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

Re: Using Gmail SMTP

Post by sleepydad »

Thanks as always, Weirdan. I'll give it a try.
sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

Re: Using Gmail SMTP

Post by sleepydad »

Getting:

Warning: fsockopen() [function.fsockopen]: unable to connect to sslv3://smtp.gmail.com:465 (Connection timed out) in /home/content/s/l/e/sleepydad/html/kasebergkronies/lib/classes/Swift/Transport/StreamBuffer.php on line 233

On another note, the reason that I'm jumping through all these hoops it because Go Daddy has blocked certain addresses from being used in From: in PHP headers. The solution seems to be (and I've tested across the board successfully):

Code: Select all

$headers .= "From: Somebody;
$headers .= "<a_godaddy_address.@name.com>"."\r\n"; //Set up a godaddy address and place that here 
$headers .= "Reply-To: anyAddressYouWant@anyHostYouWant.com"\r\n"; //Accepts @yahoo, @msn, etc
$headers .= "Return-Path: anyAddressYouWant@anyHostYouWant.com"\r\n"; //Accepts @yahoo, @msn, etc;
The return address will show as what's in brackets in the header, but hitting REPLY in your email client will call up what's in the Reply-To:

Is this essentially what Swiftmailer is going to do? If so, I don't need to pursue this anymore because I may have resolved it outside of using Swiftmailer.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Using Gmail SMTP

Post by Weirdan »

Warning: fsockopen() [function.fsockopen]: unable to connect to sslv3://smtp.gmail.com:465 (Connection timed out)
This means tcp connection could not be established - most likely blocked. From my search on google it seems godaddy indeed do not allow outgoing connections (except http and https, but that won't help in your case).

You can still use SwiftMailer if you like it though, by using Sendmail or Mail transport instead of SMTP
Post Reply