9951 explained code solutions for 126 technologies


php-swiftmailerHow to use Swiftmailer with IMAP?


Swiftmailer can be used with IMAP to send and receive emails. To use Swiftmailer with IMAP, you need to create a Transport instance with the IMAP host, port, username, and password.

$transport = (new Swift_SmtpTransport('imap.example.com', 993, 'ssl'))
  ->setUsername('[email protected]')
  ->setPassword('yourpassword');

The code above creates a Transport instance with the IMAP host, port, username, and password.

Once the Transport instance is created, you can use it to send and receive emails.

$mailer = new Swift_Mailer($transport);

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

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

The code above creates a Mailer instance with the Transport instance and sends an email.

Helpful links

Edit this code on GitHub