Email

Contents...

Email handling functions used by the Webasyt framework are implemented with the help of the Swift Mailer library (it is included in the standard framework distribution). The following example of creating and sending an email message illustrates all basic methods of this library:

  // Creating a message object with recipient $to, sender $from,
  // subject $subject, and message text $body
  $mail_message = new waMailMessage($subject, $body);
  
  // By default messages are sent in HTML.
  // If a text/plain message is necessary, specify it as shown below:
  $mail_message = new waMailMessage($subject, $body, 'text/plain');
    
  // Adding the sender
  $mail_message->setFrom('noreply@webasyst.com', 'Webasyst Robot');
    
  // Specifying the recipient   
  $mail_message->setTo('john.doe@webasyst.com', 'John Doe');
  // You can also specify several recipients in the form of an array:
  $mail_message->setTo(array('john.doe@webasyst.com' => 'John Doe'));  
    
  // Adding other recipients
  $mail_message->addTo('big.brother@webasyst.com', 'Big Brother');
  $mail_message->addCc('santa.claus@webasyst.com', 'Santa Claus');
  $mail_message->addBcc('ben.johnson@webasyst.com', 'Ben Johnson');
  
  
  // Adding an attachment
  // $path: full path to a file on the server
  // $filename: optional parameter, the filename to be displayed in the message
  $mail_message->addAttachment($path, $filename);
  // add one more attachment
  $mail_message->addAttachment($path2, $filename2);
  
  // Sending message
  $mail_message->send();
  
  

Transport setup (SMTP, sendmail)

By default email messages are sent by the mail() function. If you would like to use other transports; e.g., SMTP, you should create file wa-config/mail.php. You can set up use of different transports depending on the current sender (From field).

  <?php
  return array(
      // Specify an email address, a domain name, or default for default setup
      'default' => array(
          'type' => 'smtp',
          'host' => 'YOUR SMTP SERVER',
          'port' => 25,
          //'encryption' => 'ssl',
          //or tls — depending on SMTP server requirements
      ),
      // Setup for all @webasyst.com based mail boxes
      'webasyst.com' => array(
          'type' => 'mail'
      ),
      // Setup for a certain mail box
      'user@webasyst.com' => array(
          'type' => 'smtp',
          'host' => 'YOUR SMTP SERVER',
          'port' => 25,
          'login' => 'LOGIN NAME',
          'password' => 'PASSWORD',
          //'encryption' => 'ssl',
          //or tls — depending on SMTP server requirements
      )
  );
  

For sending of email messages the appropriate transport will be automatically selected based on the current sender.