Axel Lessio
Axel Lessio

Reputation: 21

PHP - Problems sending a mail through the gmail SMTP using SwiftMailer

I'm working on a form: I need to send a mail with a gmail account once the form is submitted. I've set up SwiftMailer, it seems that everything has ben set correctly, but nothing happens. I have set the "action" of the form to this php file:

    <?php
    require_once '/assets/swift-mailer/lib/swift_required.php';

    // variables
    $visitor_email = $_POST['email'];
    $visitor_phone = $_POST['phone'];
    $visitor_page = $_POST['site'];


    $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
      ->setUsername('[email protected]')
      ->setPassword('mypassword');

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

    $message = Swift_Message::newInstance('New submission')
      ->setFrom('[email protected]')
      ->setTo('[email protected]')
      ->setBody("New submission from this site: \n
               $visitor_page \n
               e-mail address: $visitor_email \n
               phone number: $visitor_phone");

    $result = $mailer->send($message);
    ?>

I've double checked the version of PHP and it's ok in the server. Php_openssl is enabled. The server is running correctly. The form in the html file is ok, it has "method = post" associated. The libraries are correctly placed in the server folder. I really don't understand what's wrong.

Upvotes: 2

Views: 853

Answers (1)

Rajib Ghosh
Rajib Ghosh

Reputation: 640

Maybe there is many reasons to didn't sending mail...Would you like to check apache error_log and browser network..or you can set error_reporting(E_ALL) in your script.Maybe it was google smtp problem or others.at first you have to do your problem clarify after that you can do anything.If you upload your error then anyone can help you.

You can try this but i am not sure it's useful for you or not

   $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls');

instead of

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

Upvotes: 2

Related Questions