Precisium Logo
PRECISIUM
Pricing      Policies      Support/FAQ      Contact      Payments      Webmail
Phone:   +61 2 9966 8228
Fax:   +61 2 9901 3027
Email:   info@precisium.com.au

Sending email from PHP pages using SwiftMailer

The PHP SwiftMailer library (http://www.swiftmailer.org) allows you to send email from a webserver using an Authenticated SMTP account.

The SwiftMailer API changed significantly with the release of version 4.
The old swiftmailer example is still available, but we recommend that you upgrade your code to use the new API as demonstrated below.

Why Swift?

Precisium's webservers are configured to disallow the basic PHP mail() function, so that in the event of your email forms being abused by spammers, the problem can be more specifically traced and dealt with.

The Swift library allows your website to communicate directly with the SMTP submission server to send email more quickly and efficiently.

The webservers have the Swift libraries pre-installed - so to get started, simply include this line within your PHP code:
require_once 'swift-mailer/lib/swift_required.php';

Here is an example of sending a basic email using Swift.
You can find more examples on the Swift Mailer library documentation pages

<?php
    $server 
'smtp.accountid.junctionworld.com';
    
$user 'accountid.website';
    
$pass 'password';
    
$from 'website@example.com';
    
$to 'someone@somewhere-else.example.net';

    
//Load in the files we'll need
    
require_once 'swift-mailer/lib/swift_required.php';

    
//Create a Swift transport and mailer
    
$transport Swift_SmtpTransport::newInstance($server587)
        ->
setUsername($user)
        ->
setPassword($pass);
   
    
$mailer Swift_Mailer::newInstance($transport);
   

    
$body "test test test";
    
$html "<p>Optionally, <b>send an HTML version of the message</b></p><pre>$body</pre>";
    
    
//Create the message
    
$message Swift_Message::newInstance()
        ->
setSubject('Swiftmailer (lib version 4x) test')
        ->
setFrom(array('website@example.com' => 'test script on webserver'))
        ->
setTo(array(
            
'someone@somewhere-else.example.net' => 'Fred Nerk',
            
'anothersomeone@somewhere-else.example.net' => "Someone Else"
            
))
        ->
setBody($body)
        ->
addPart($html'text/html')
    ;
    
//You can leave out the 'addPart' line if you only want to send a plain text email.
    

    
if ($mailer->send($message)) {
        echo 
"Sent";
    } else {
        echo 
"Failed";
    }
?>

Tips and suggestions

  • You can use any of your existing Precisium mail accounts as the SMTP user for sending mail - but it often makes sense to use a specific mailbox and address for this purpose. e.g website@example.com
    Because the username and password for the mailbox are embedded in your PHP code; it makes sense to use a mailbox that is unlikely to have the password changed by a user who doesn't realize the webserver needs to access the mailbox for sending.
  • It is ok to use a 'from address' belonging to a different username than the one Swift is configured to send with.
    e.g - you may have a mailbox username and password specifically implemented for the webserver, with addresses such as: website@example.com, but you could still use sales@example.com as the apparent from address.
  • It is strongly recommended that you do not use domain names you do not own in the 'from address'.
    It may appear to work - but is more likely to be blocked or marked as spam at the receiving end.
    Use other mechanisms such as the 'reply to' address if you need responses to go to an address at an external domain name.
  • Get in the habit of specifying port 587 as the SMTP port where possible for mail submission. Whilst port 25 will often work, especially between the webservers and email servers of a single company; port 587 is the preferred mail submission port for Authenticated SMTP - and is less likely to be blocked by any intermediate systems.