Exemplo n.º 1
0
 /**
  * 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;
 }
Exemplo n.º 2
0
 function gethostname()
 {
     return AppIdentityService::getApplicationId();
 }
Exemplo n.º 3
0
 public function testGetApplicationId()
 {
     putenv("APPLICATION_ID=simple-app-id");
     $this->assertEquals("simple-app-id", AppIdentityService::getApplicationId());
     putenv("APPLICATION_ID=domain.com:domain-app-id");
     $this->assertEquals("domain.com:domain-app-id", AppIdentityService::getApplicationId());
     putenv("APPLICATION_ID=part~partition-app-id");
     $this->assertEquals("partition-app-id", AppIdentityService::getApplicationId());
     putenv("APPLICATION_ID=part~domain.com:display");
     $this->assertEquals("domain.com:display", AppIdentityService::getApplicationId());
 }
Exemplo n.º 4
0
 protected function callNotifier($endpoint, $params)
 {
     $credentials = file_get_contents(storage_path('notifier.json'));
     $credentials = json_decode($credentials);
     $auth = sprintf('%s:%s', $credentials->username, $credentials->password);
     $headers = [sprintf('Authorization: Basic %s', base64_encode($auth)), 'Content-type: application/x-www-form-urlencoded', sprintf('User-Agent: %s', AppIdentityService::getApplicationId())];
     $contextParams = ['http' => ['method' => 'POST', 'header' => implode("\r\n", $headers) . "\r\n", 'content' => http_build_query($params)]];
     $context = stream_context_create($contextParams);
     $response = file_get_contents($credentials->url . $endpoint, false, $context);
     //syslog(LOG_DEBUG, print_r($contextParams, true));
     //syslog(LOG_DEBUG, print_r($http_response_header, true));
     return $response;
 }
Exemplo n.º 5
0
 /**
  * Get the default email address for App Engine
  *
  * @param string $user User part of the email address (typically 'wordpress')
  * @return string Email address with the correct email domain
  */
 function get_default_email($user = '******')
 {
     // Let's build an email address for the app via the app identity api
     $service_account = new AppIdentityService();
     $id = $service_account->getApplicationId();
     if (empty($id)) {
         $service_account_name = $service_account->getServiceAccountName();
         $service_account_from_name = explode('@', $service_account_name);
         $id = $service_account_from_name[0];
     }
     return $user . '@' . $id . '.appspotmail.com';
 }