SMTP with a Username and Password

Some servers require authentication. You can provide a username and password with setUsername() and setPassword().

To use a username and password with the SMTP Transport:

  1. Create the Transport with Swift_SmtpTransport::newInstance().
  2. Call the setUsername() and setPassword() methods on the Transport.

Your username and password will be used to authenticate upon first connect when send() or batchSend() are first used on the Mailer.

If authentication fails, an Exception of type Swift_Transport_TransportException will be thrown.

Note: If you need to know early whether or not authentication has failed and an Exception is going to be thrown, call the start() method on the created Transport.
<?php

require_once 'lib/swift_required.php';

//Create the Transport the call setUsername() and setPassword()
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
  ->setUsername('username')
  ->setPassword('password')
  ;

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

NOTE: Documentation for version 3 is in the wiki.