This is an outdated version of PHPasap. Please visit http://phpasap.com/docs for the latest version.

Mail

Mail class of PHPasap uses the very famous PHPMailer for sending mails.

Configuration

Before usign the mail class make sure you have configured the details in app/config/mail.php. If you wish to use any SMTP servers for sending mail then set is_smtp to true and fill in the smtp details

Below is an example config for using Gmail SMTP

"from" => "somegmailaddress@gmail.com", // or ["somegmailaddress@gmail.com", "Some Name"]
"use_smtp" => true,
"smtp_host" => "smtp.gmail.com",
"smtp_username" => "somegmailaddress",
"smtp_password" => "yourpassword",
"smtp_encryption" => "tls",
"smtp_port" => 587,

Note that you can set the from parameter to globally set the default from name of all mails sent through Mail class. You can optionally override this from field while sending mail. See below sections for more details.

Sending mail

Once the config is set then below is how you would send an email

// using just email
Mail::send("abc@somedomain.com", "This is the subject", "This is the message");

// using just email and name of recipient
Mail::send(["abc@somedomain.com", "Abc"], "This is the subject", "This is the message");

// sending to multiple recipient
Mail::send([["abc@somedomain.com", "Abc"], ["xyz@somedomain.com", "Xyz"]], "This is the subject", "This is the message");

Syntax and Usage

Below is the syntax of using the Mail class

Mail::send($to, $subject, $message, $options=[]);
  • $to is the recipient(s). It could be either a string containing email address or an array containing name and email or an array of array containing name and email of multiple recipients. So below is how this can be used.
    • 'somebody@somedomain.com'
    • ['somebody@somedomain.com', 'Some Name']
    • [ ['somebody@somedomain.com', 'Some Name'], ['somebodyelse@somedomain.com'], ['anothermail@somedomain.com', 'Another Name']]
  • $subject is the subject of the mail
  • $message is the message of the mail
  • $options can be used to override few of the config fields and for setting additional parameters for sending mail like adding cc and bcc. Below are the available options. Each parameter is optional.
    Parameter Description Type Example
    cc Adding CC mails
    • string (Email)
    • array (Email as first parameter and Name as second)
    • array of array (array of above array)
    • 'abc@somedomain.com'
    • ['abc@somedomain.com', 'Abc']
    • [
          ['abc@somedomain.com', 'Abc'],
          ['xyz@somedomain.com', 'Xyz'],
          ['lmn@somedomain.com']
      ]
    bcc Adding BCC mails
    • string (Email)
    • array (Email as first parameter and Name as second)
    • array of array (array of above array)
    • 'abc@somedomain.com'
    • ['abc@somedomain.com', 'Abc']
    • [
          ['abc@somedomain.com', 'Abc'],
          ['xyz@somedomain.com', 'Xyz'],
          ['lmn@somedomain.com']
      ]
    is_html Set to true if you want to send HTML mail. Default is true boolean true
    from Use this to override the default global from field set in the app/config/mail.php config file
    • string
    • array
    • 'abc@somedomain.com'
    • ['abc@somedomain.com', 'Abc']

Return type

send() function returns boolean true if successful else returns a boolean false.

Get error message if mail sending failed

echo Mail::get_last_error();