コード例 #1
0
ファイル: EmailTest.php プロジェクト: taz77/sendgrid-php-ng
 public function testToWebFormatWithHeaders()
 {
     $email = new \SendGrid\Email();
     $email->addHeader('X-Sent-Using', 'SendGrid-API');
     $json = $email->toWebFormat();
     $headers = json_decode($json['headers'], TRUE);
     $this->assertEquals('SendGrid-API', $headers['X-Sent-Using']);
 }
コード例 #2
0
 /**
  * Repair data for send message
  *
  * @param array $args
  *
  * @return array
  */
 function generate_postdata($args = array())
 {
     $emailData = new SendGrid\Email();
     $is_force = aem_get_option('aem_force_header', FALSE);
     $header = $this->process_header($args['headers']);
     /**
      * generate postFile
      */
     $emailData->setSubject($args['subject']);
     $emailData->setText($args['subject']);
     //From email
     if (!$is_force && isset($header['from_name'])) {
         $from_email = $header['from_email'];
     } else {
         $from_email = AEM_Option()->get_from_email();
     }
     $from_email = apply_filters('wp_mail_from', $from_email);
     $emailData->setFrom($from_email);
     //From name
     if (!$is_force && isset($header['from_name'])) {
         $from_name = $header['from_name'];
     } else {
         $from_name = AEM_Option()->get_from_name();
     }
     $from_name = apply_filters('wp_mail_from_name', $from_name);
     $emailData->setFromName($from_name);
     //to
     if (!is_array($args['to'])) {
         $args['to'] = explode(',', $args['to']);
     }
     foreach ((array) $args['to'] as $recipient) {
         try {
             // Break $recipient into name and address parts if in the format "Foo <*****@*****.**>"
             $recipient_name = '';
             if (preg_match('/(.*)<(.+)>/', $recipient, $matches)) {
                 if (count($matches) == 3) {
                     $recipient_name = $matches[1];
                     $recipient = $matches[2];
                 }
             }
             $emailData->addTo($recipient, $recipient_name);
         } catch (phpmailerException $e) {
             continue;
         }
     }
     //cc
     if (!empty($header['cc'])) {
         //header cc not empty
         if (empty($args['cc'])) {
             //arg cc empty, override agr cc
             $args['cc'] = $header['cc'];
         } else {
             //arg cc not empty
             if (!is_array($args['cc'])) {
                 $args['cc'] = explode(',', $args['cc']);
             }
             //merge them
             $args['cc'] = array_merge($header['cc']);
         }
     }
     if (!empty($args['cc'])) {
         foreach ((array) $args['cc'] as $recipient) {
             try {
                 // Break $recipient into name and address parts if in the format "Foo <*****@*****.**>"
                 $recipient_name = '';
                 if (preg_match('/(.*)<(.+)>/', $recipient, $matches)) {
                     if (count($matches) == 3) {
                         $recipient_name = $matches[1];
                         $recipient = $matches[2];
                     }
                 }
                 $emailData->addCc($recipient);
             } catch (phpmailerException $e) {
                 continue;
             }
         }
     }
     //bcc
     if (!empty($header['bcc'])) {
         //header bcc not empty
         if (empty($args['bcc'])) {
             //arg bcc empty, override agr cc
             $args['bcc'] = $header['bcc'];
         } else {
             //arg bcc not empty
             if (!is_array($args['bcc'])) {
                 $args['bcc'] = explode(',', $args['bcc']);
             }
             //merge them
             $args['bcc'] = array_merge($header['bcc']);
         }
     }
     if (!empty($args['bcc'])) {
         foreach ((array) $args['bcc'] as $recipient) {
             try {
                 // Break $recipient into name and address parts if in the format "Foo <*****@*****.**>"
                 $recipient_name = '';
                 if (preg_match('/(.*)<(.+)>/', $recipient, $matches)) {
                     if (count($matches) == 3) {
                         $recipient_name = $matches[1];
                         $recipient = $matches[2];
                     }
                 }
                 $emailData->addBcc($recipient);
             } catch (Exception $e) {
                 continue;
             }
         }
     }
     if (!isset($header['content_type'])) {
         $content_type = 'text/plain';
     } else {
         $content_type = $header['content_type'];
     }
     $content_type = apply_filters('wp_mail_content_type', $content_type);
     if ($content_type == "text/html") {
         $emailData->setHtml($args['message']);
     }
     //custom header
     if (isset($args['headers']) && is_array($args['headers'])) {
         //extract header line
         $header_arr = explode("\r\n", $args['headers']);
         foreach ($header_arr as $header) {
             //extract header key:value
             $tmp = explode(':', $header);
             if (count($tmp) > 1) {
                 if ($tmp[1] != NULL) {
                     if (strtolower($tmp[0]) == 'from') {
                         if (aem_get_option('aem_force_header')) {
                             continue;
                         }
                     }
                     $emailData->addHeader($tmp[0], $tmp[1]);
                 }
             }
         }
     }
     //$args['attachments'] = array ( plugin_dir_path( AEM_PLUGIN_FILE )."/inc/ae/assets/img/slider.png" );
     if (!empty($args['attachments'])) {
         $emailData->setAttachments($args['attachments']);
     }
     $postData = apply_filters("aem_postdata", $emailData);
     return $postData;
 }