Page 1 of 1

Just can't get it to work with Gmail

Posted: Fri Apr 11, 2008 9:03 pm
by wizo
Hello. I am a total newbie at this, but from what I have read, this is a great piece of software.

I have been having trouble getting it to work for my gmail smtp. At first I tried the smoke tests using that same Gmail SMTP and they all work fine; however when I try to enable it on my server it just won't work. Sometimes it says it can't connect, then I move something around and I get a different error. I spent the whole afternoon reading the forums and the docs trying to find the different problems that might be arising and I can't seem to find and answer.

I am using google apps for my domain. Gmail works great and, as I mentioned earlier, the SMTP smoke tests went out perfect.

Here is what I a trying to do: I set up a website for my practice (I am a physician, which explains my problems with php!) in which the patients request a free consultation. The form validation is working fine and I already have all the resulting strings in variables, but I need to send three separate emails. One to the patient for feedback so he/she knows that we received his/her request, one for my secretary, which includes all information, so she can call and set the appointment, and the third one, containing all the information as well, for me so I can keep track of how many people are requesting consults.

Now, I have not been able to send even the first of those three emails!! I would really appreciate your help. I am running PHP 5.2.5 and swift 3.3.2 for php5

This is the whole code for swift that I have so far.

Code: Select all

// Al haber pasado la validación, inicia Swift
                        require_once "swift/Swift.php";
                        require_once "swift/Swift/Connection/SMTP.php";
                        require_once "swift/Swift/Authenticator/LOGIN.php";
                
                // Iniciar sesión en el SMTP       
                        $smtp = new Swift_Connection_SMTP("smtp.gmail.com", SWIFT_SMTP_PORT_SECURE, ENC_TLS);
                        $smtp->setUsername("myemail@freyja.com.mx");
                        $smtp->setPassword("password");
                        
                        $conn =& new Swift_Connection_SMTP("smtp.gmail.com");
                        $conn->attachAuthenticator(new Swift_Authenticator_LOGIN());
                        
                        $swift = new Swift(new Swift_Connection_SMTP("smtp.gmail.com", Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_TLS));
                                                
                //Crear el mensaje a enviar
                        $body = "¡Hola $nombre!<br><br>Hemos recibido tu solicitud para una cita en el área de $area_cita. Mencionaste que te podemos hablar $dia_contacto a las $horario_contacto.<br><br> Si tienes cualquier duda al respecto, no dudes en ponerte en contacto con nosotros al 5253.26.85 ó en la dirección de correo info@freyja.com.mx.<br><br>No respondas directamente a este correo ya que no tenemos manera de recibir el mensaje.<br><br>!Que tengas un buen día!";
                        $sender = new Swift_Address($email, $nombre);
                        $message = new Swift_Message("Solicitud de cita en: " . $area_cita);
                        $message->attach(new Swift_Message_Part($body));
                    
                //Try sending the email
                        $sent = $swift->send($message, "citas@freyja.com.mx", $sender);
                
                //Disconnect from SMTP, we're done
                        $swift->disconnect();
As I am completing the site, I am uploading to an alternate server, so you can currently check out the form I am trying to send at http://www.freyja.org.mx/cita2.php

Any help will be greatly appreciated. Thank you.

Re: Just can't get it to work with Gmail

Posted: Fri Apr 11, 2008 9:17 pm
by wizo
This is the error I am currently getting with the above code:

Fatal error: Uncaught exception 'Swift_BadResponseException' with message 'Expected response code(s) [250] but got response [530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://mail.google.com/support/bin/answ ... swer=14257 8sm6113399ywg.8]' in /home/erngut/public_html/freyjaorg/swift/Swift.php:250 Stack trace: #0 /home/erngut/public_html/freyjaorg/swift/Swift.php(310): Swift->assertCorrectResponse(Object(Swift_Events_ResponseEvent), 250) #1 /home/erngut/public_html/freyjaorg/swift/Swift.php(373): Swift->command('MAIL FROM: <ern...', 250) #2 /home/erngut/public_html/freyjaorg/cita2.php(132): Swift->send(Object(Swift_Message), 'mymail@freyja.co...', Object(Swift_Address)) #3 {main} thrown in /home/erngut/public_html/freyjaorg/swift/Swift.php on line 250



Thanks for your help!!

Re: Just can't get it to work with Gmail

Posted: Fri Apr 11, 2008 9:20 pm
by John Cartwright

Code: Select all

$smtp = new Swift_Connection_SMTP("smtp.gmail.com", SWIFT_SMTP_PORT_SECURE, ENC_TLS);
$smtp->setUsername("myemail@freyja.com.mx");
$smtp->setPassword("password");
                       
$conn =& new Swift_Connection_SMTP("smtp.gmail.com");
$conn->attachAuthenticator(new Swift_Authenticator_LOGIN());
                       
$swift = new Swift(new Swift_Connection_SMTP("smtp.gmail.com", Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_TLS));
Notice you've created 3 Swift_Connection_SMTP objects, which basically means your oppening 3 connections. Now look at your error, the connection your using you are not providing a username/password to connect with. Try:

Code: Select all

$smtp = new Swift_Connection_SMTP("smtp.gmail.com", SWIFT_SMTP_PORT_SECURE, ENC_TLS);
$smtp->setUsername("myemail@freyja.com.mx");
$smtp->setPassword("password");
 
$swift = new Swift($smtp);

Re: Just can't get it to work with Gmail

Posted: Fri Apr 11, 2008 10:59 pm
by wizo
Wow! Thanks for the quick response!

I replaced my old code with the one you provided, but now I am getting this Fatal Error:

Fatal error: Uncaught exception 'Swift_ConnectionException' with message 'The SMTP connection failed to start [smtp.gmail.com:0]: fsockopen returned Error Number 0 and Error String 'Failed to parse address "smtp.gmail.com"'' in /home/erngut/public_html/freyjaorg/swift/Swift/Connection/SMTP.php:309 Stack trace: #0 /home/erngut/public_html/freyjaorg/swift/Swift.php(216): Swift_Connection_SMTP->start() #1 /home/erngut/public_html/freyjaorg/swift/Swift.php(101): Swift->connect() #2 /home/erngut/public_html/freyjaorg/cita2.php(120): Swift->__construct(Object(Swift_Connection_SMTP)) #3 {main} thrown in /home/erngut/public_html/freyjaorg/swift/Swift/Connection/SMTP.php on line 309

Any ideas? Once again, thanks for your time and help; it is greatly appreciated.

Re: Just can't get it to work with Gmail

Posted: Sat Apr 12, 2008 12:39 am
by Chris Corbyn
Jcart's code use contants which are from PHP4... you need this:

Code: Select all

$smtp = new Swift_Connection_SMTP("smtp.gmail.com", Swift_Connection_SMTP::PORT_SECURE,
  Swift_Connection_SMTP::ENC_TLS);
$smtp->setUsername("myemail@freyja.com.mx");
$smtp->setPassword("password");
 
$swift = new Swift($smtp);

Re: Just can't get it to work with Gmail

Posted: Sat Apr 12, 2008 11:28 am
by wizo
The script is working now!

I just need to write the code for the other two emails I want generated, but that shouldn't be much harder.

Thank you!