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

NOTE: This page applies to version 3x of SwiftMailer

The SwiftMailer API changed significantly with the release of version 4.
The code below should still work on our webservers, but we encourage you to migrate your code to use the new SwiftMailer API.
Updated instructions for swiftmailer

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

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 these lines within your PHP code:
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";

Here is an example of sending a basic email using Swift.
You can find more examples, such as for sending HTML emails; on the Swift wikidocs 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 "
lib/Swift.php";
   require_once "
lib/Swift/Connection/SMTP.php";

   //Start Swift
   
$smtp = new Swift_Connection_SMTP($server, 587);
   
$smtp->setUsername($user);
   
$smtp->setpassword($pass);

   
$swift =& new Swift($smtp);

   
$body = "test test test";

   //Create the message
   
$message =& new Swift_Message("Swiftmailer test", $body);


   if (
$swift->send($message$to$from)) {
      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.