function smtp_mail

6.x smtp.module smtp_mail($to, $subject, $msg, $bool_html = FALSE, $attachments = array(), $bool_string_attachment = FALSE)
5.x smtp.module smtp_mail($to, $subject, $msg)

For attachments, the array can be one of two methods:

(1) $arr["full_filename"] => "filename" or (2) $arr['filename'] = "string that makes up atachment"

For method #2, $bool_string_attachment must be set to TRUE.

2 calls to smtp_mail()
engagements_send_email_or_txt_form_submit in modules/engagements/engagements.module
fp_mail in includes/misc.inc
Send an email. Drop-in replacement for PHP's mail() command, but can use SMTP protocol if enabled.

File

modules/smtp/smtp.module, line 124
This is the main module file for SMTP, which will let us send emails using SMTP instead of PHP's mail() command.

Code

function smtp_mail($to, $subject, $msg, $bool_html = FALSE, $attachments = array(), $bool_string_attachment = FALSE) {

  $system_path = trim($GLOBALS ["fp_system_settings"]["file_system_path"]);
  require_once ("$system_path/libraries/PHPMailer/src/Exception.php");
  require_once ("$system_path/libraries/PHPMailer/src/PHPMailer.php");
  require_once ("$system_path/libraries/PHPMailer/src/SMTP.php");

  $msg = trim($msg);
  if ($msg == "") {
    if (count($attachments) == 0) {
      // This is a completely empty message!  Don't send anything.
      watchdog("smtp", "Not sending email to $to ($subject).  No message or attachments.", array(), WATCHDOG_DEBUG);
      return FALSE;
    }
    else {
      $msg = "   "; // We DO have attachments, so add some spaces so we have a string in the variable.
    }
  }


  $mail = new PHPMailer();

  // Settings
  $mail->IsSMTP();
  $mail->CharSet = 'UTF-8';

  $mail->Host = variable_get('smtp_host', '');
  $mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
  // 0 = no output, 1 = errors and messages, 2 = messages only.

  $mail->SMTPSecure = variable_get('smtp_secure', ''); // tls, ssl, and none are allowed.

  $mail->SMTPAuth = TRUE; // enable SMTP authentication
  $mail->Port = variable_get('smtp_port', ''); // set the SMTP port
  $mail->Username = variable_get('smtp_username', '');
  $mail->Password = variable_get('smtp_password', ''); // SMTP account password example

  // Content
  //$mail->isHTML(true);                                  // Set email format to HTML
  //$mail->Subject = 'Here is the subject';
  //$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
  //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

  // Content
  if ($bool_html) {
    $mail->isHTML(TRUE);
    $mail->msgHTML($msg);
  }
  else {
    $mail->Body = $msg;
  }

  $mail->Subject = $subject;



  $mail->setFrom(variable_get('smtp_from_email_address', 'noreply@yourdomain.com'), variable_get('smtp_from_email_name', 'NoReply - FlightPath'));


  //Recipients
  $temp = explode(',', $to);
  foreach ($temp as $line) {
    $line = trim($line);
    if ($line != "") {
      $mail->addAddress($line); // Add a recipient    
    }
  }


  //$mail->addAddress('ellen@example.com', 'Ellen Smith');               // Name is optional
  //$mail->addReplyTo('info@example.com', 'Information');
  //$mail->addCC('cc@example.com');
  //$mail->addBCC('bcc@example.com');  

  if (!$bool_string_attachment) {
    if (count($attachments) > 0) {
      foreach ($attachments as $full_filename => $filename) {
        $mail->addAttachment($full_filename, $filename);
      }
    }
  }
  else {
    // This IS a string attachment
    if (count($attachments) > 0) {
      foreach ($attachments as $filename => $string) {
        $mail->addStringAttachment($string, $filename);
      }
    }
  }

  $x = $mail->send();
  if (!$x) {
    fpm("PHPMailer error: " . $mail->ErrorInfo);
    watchdog("smtp", "PHPMailer error: " . $mail->ErrorInfo, array(), WATCHDOG_ERROR);
    return FALSE;
  }

  return TRUE;

}