简体   繁体   中英

Cannot send an email using SwiftMailer and Symfony 2

I'm trying to send emails using SwiftMailer and Symfony2.

Before I had a Can't connect to smtp.gmail.com issue, but know, I haven't any error, but the message is still not sent.

Here is my config.yml :

swiftmailer:
    transport: mail
    encryption: ssl
    host: smtp.gmail.com
    port: 465
    auth_mode: login
    username:   myUsername@gmail.com
    password:   myPassword

config_test.yml :

swiftmailer:
    disable_delivery: false

controller to send :

$message = \Swift_Message::newInstance(null)
   ->setSubject('Test')
   ->setFrom('test@gmail.com')
   ->setTo('test@gmail.com')
   ->setBody('Test test test !!');

$this->get('mailer')->send($message);

I tried many fix I found on internet, but nothing fixed it :/

I'm trying to send it using Wamp, Locally

EDIT :

I've set the transport to SMTP and now, when I use port 443, I got a timeout, if I use 465, I just get "can't connect" again.

EDIT 2:

I tried to use "Transport: gmail", but still "can't connect" message Here is my config:

transport: gmail
username:  'myEmail@gmail.com'
password:  'myPassword'

For sending mail with Gmail you can use transport: gmail

If you need more information: http://symfony.com/doc/current/cookbook/email/gmail.html

If it's well configured and doesn't work, check about your security environment (firewall, ...)

in App/config/config.yml your to write this:

swiftmailer:
transport: smtp
encryption: ssl     
auth_mode:  login   
host: "%mailer_host%"
username:  "%mailer_user%"
password:  "%mailer_password%"
spool:     { type: memory }

in App/config/parameters.yml:

 mailer_transport: gmail
    mailer_host: smtp.gmail.com
    mailer_user: yourmail@gmail.com
    mailer_password: your_mail_password

In your Controller:

public function sendMailAction() { 

      $Request= $this ->getRequest();

      if($Request ->getMethod()== "POST"){

          $name= $Request -> get("name");
          $email = $Request -> get("email");
          $sujet = $Request -> get("subject");
          $message = $Request -> get("message");

          $mailer = $this->container->get('mailer');
          $transport = \Swift_SmtpTransport::newInstance('smtp.gmail.com',465,'ssl')
                  ->setUsername('******')
                  ->setPassword('******');

           $sms = \Swift_Message::newInstance('Test')
                   ->setSubject($sujet)
                   ->setFrom('your_mail_here@gmail.com')
                   ->setTo($email)
                   ->setBody($message);

$spool = $mailer->getTransport()->getSpool();
$transport = $this->get('swiftmailer.transport.real');

$spool->flushQueue($transport);
           $this->get('mailer')->send($sms);

      }
      return $this->render('SwiftMailSwiftMailBundle:Mail:contact.html.twig');   

      }

Ask Me to send you the full project if you want.Good Luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM