/** * Send an email. * * This is a re-implementation of PHP's mail() function using App Engine * mail API. The function relies on mailparse extension to parse emails. * * @param string $to Receiver, or receivers of the mail. * @param string $subject Subject of the email to be sent. * @param string $message Message to be sent. * @param string $additional_headers optional * String to be inserted at the end of the email header. * @param string $additional_parameters optional * Additional flags to be passed to the mail program. This arugment is * added only to match the signature of PHP's mail() function. The value is * always ignored. * @return bool * TRUE if the message is sent successfully, otherwise return FALSE. * * @see http://php.net/mail */ public static function sendMail($to, $subject, $message, $additional_headers = null, $additional_parameters = null) { $raw_mail = "To: {$to}\r\nSubject: {$subject}\r\n"; if ($additional_headers != null) { $raw_mail .= trim($additional_headers); } $raw_mail .= "\r\n\r\n{$message}"; $mime = mailparse_msg_create(); mailparse_msg_parse($mime, $raw_mail); $root_part = mailparse_msg_get_part_data($mime); // Set sender address based on the following order // 1. "From" header in $additional_headers // 2. "sendmail_from" ini setting // 3. Default address "mailer@<app-id>.appspotmail.com $from = ini_get('sendmail_from'); if (isset($root_part['headers']['from'])) { $from = $root_part['headers']['from']; } if ($from === false || $from == "") { $from = sprintf(self::DEFAULT_SENDER_ADDRESS_FORMAT, AppIdentityService::getApplicationId()); syslog(LOG_WARNING, "mail(): Unable to determine sender's email address from the " . "'sendmail_from' directive in php.ini or from the 'From' " . "header. Falling back to the default {$from}."); } $email = new Message(); try { $email->setSender($from); $email->addTo($root_part['headers']['to']); if (isset($root_part['headers']['cc'])) { $email->AddCc($root_part['headers']['cc']); } if (isset($root_part['headers']['bcc'])) { $email->AddBcc($root_part['headers']['bcc']); } $email->setSubject($root_part['headers']['subject']); $parts = mailparse_msg_get_structure($mime); if (count($parts) > 1) { foreach ($parts as $part_id) { $part = mailparse_msg_get_part($mime, $part_id); self::parseMimePart($part, $raw_mail, $email); } } else { if ($root_part['content-type'] == 'text/plain') { $email->setTextBody($message); } else { if ($root_part['content-type'] == 'text/html') { $email->setHtmlBody($message); } } } $email->send(); } catch (\Exception $e) { trigger_error('mail(): ' . $e->getMessage(), E_USER_WARNING); return false; } return true; }