Synopsis: new Swift_Connection_SMTP(string remote_host[, int remote_port [, int encryption_level]])
The SMTP connection is probably the most heavily used connection, and almost certainly the most consistent and portable. This connection opens up a socket with TCP and speaks “SMTP” to a remote SMTP server. You need to have a SMTP server which is capable of relaying mail from the domain of your web server for this to work. Some hosts provide a SMTP server for you as part of your package. You may also make use of Gmail’s SMTP server if you have a Gmail account; however, they do impose a maximum limit of 50 emails in any one go.
require_once "lib/Swift.php"; require_once "lib/Swift/Connection/SMTP.php"; //Connect to localhost on port 25 $swift =& new Swift(new Swift_Connection_SMTP("localhost")); //Connect to an IP address on a non-standard port $swift =& new Swift(new Swift_Connection_SMTP("217.147.94.117", 419)); //Connect to Gmail (PHP5) $swift = new Swift(new Swift_Connection_SMTP( "smtp.gmail.com", Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_TLS)); //Connect to Gmail (PHP4) $swift =& new Swift(new Swift_Connection_SMTP( "smtp.gmail.com", SWIFT_SMTP_PORT_SECURE, SWIFT_SMTP_ENC_TLS));
Note that to use encryption in PHP5 you should use the class constants ENC_OFF, ENC_SSL or ENC_TLS. In PHP4, there is no such thing as Class Constants so instead, there are provided global constants SWIFT_SMTP_ENC_OFF, SWIFT_SMTP_ENC_TLS and SWIFT_SMTP_ENC_SSL.
The SMTP connection may also use authentication (such as when using Gmail). In such case you will need to provide a username and password to use.
$smtp =& new Swift_Connection_SMTP("some-host.tld", 25); $smtp->setUsername("user"); $smtp->setpassword("pass"); $swift =& new Swift($smtp);
See the documentation page on using SMTP authentication for more information about SMTP authentication in Swift.
The default timeout to wait for a connection, or to wait for a response with the SMTP connection is 15 seconds. This can be adjusted by using the setTimeout() method.
$smtp =& new Swift_Connection_SMTP("some-host.tld", 25); $smtp->setTimeout(2); $swift =& new Swift($smtp);
TIP: If you’re using PHP5, try wrapping try/catch constructs around the areas where you create a connection. It’s possible that a Swift_Connection_Exception might be thrown if the server is down.