Search code examples
phpgmailswiftmailer

Swift Mailer does not work and gives no error messages


I try to send emails via Swift-Mailer. According to the documentation, it should work like this, but I neither get a mail nor an error message.

Here is the code of the PHP Swift file:

<?php
    require_once '/composer/vendor/autoload.php';

    // Create the Transport
    $transport = (new Swift_SmtpTransport('smtp.gmail.com', 465, 'ssl'))
    ->setUsername('*******@gmail.com')
    ->setPassword('password')
    ;

    // Create the Mailer using your created Transport
    $mailer = new Swift_Mailer($transport);

    // Create a message
    $message = (new Swift_Message('Wonderful Subject'))
    ->setFrom(['[email protected]' => 'John Doe'])
    ->setTo(['[email protected]', '[email protected]' => 'A name'])
    ->setBody('Here is the message itself')
    ;

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

And here is the composer.json:

{
    "require": {
        "swiftmailer/swiftmailer": "^6.0"
    }
}

Solution

  • Check again with this syntax and make sure the email or the password are spelt correctly:

    $trp = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
            ->setUsername('*********@gmail.com')
            ->setPassword('password');
    
    $mailer = Swift_Mailer::newInstance($trp);
    
    $message = Swift_Message::newInstance('Wonderful Subject')
        ->setFrom(['[email protected]' => 'John Doe'])
        ->setTo(['[email protected]', '[email protected]' => 'A name'])
        ->setBody('Here is the message itself');
    
    $mailer->send($message);
    

    EDIT: worked successfully with SwiftMailer version 5.4