/**
  * Method that processes the subscription params
  *
  * @param   mixed   $params   array of parameters from $_POST
  *
  * @return  void
  */
 private function process_subscription($params)
 {
     $email_split = explode("@", $_POST['sendgrid_mc_email']);
     if (isset($email_split[1])) {
         $email_domain = $email_split[1];
         try {
             $Punycode = new Punycode();
             $email_domain = $Punycode->decode($email_split[1]);
         } catch (Exception $e) {
         }
         $email = $email_split[0] . '@' . $email_domain;
     } else {
         $email = $_POST['sendgrid_mc_email'];
     }
     // Bad call
     if (!isset($email) or !Sendgrid_Tools::is_valid_email($email)) {
         return self::INVALID_EMAIL_ERROR;
     }
     if ('true' == Sendgrid_Tools::get_mc_opt_req_fname_lname() and 'true' == Sendgrid_Tools::get_mc_opt_incl_fname_lname()) {
         if (!isset($_POST['sendgrid_mc_first_name']) or empty($_POST['sendgrid_mc_first_name'])) {
             return self::ERROR_EMAIL_SEND;
         }
         if (!isset($_POST['sendgrid_mc_last_name']) or empty($_POST['sendgrid_mc_last_name'])) {
             return self::ERROR_EMAIL_SEND;
         }
     }
     if (isset($_POST['sendgrid_mc_first_name']) and isset($_POST['sendgrid_mc_last_name'])) {
         Sendgrid_OptIn_API_Endpoint::send_confirmation_email($email, $_POST['sendgrid_mc_first_name'], $_POST['sendgrid_mc_last_name']);
     } else {
         Sendgrid_OptIn_API_Endpoint::send_confirmation_email($email);
     }
     return self::SUCCESS_EMAIL_SEND;
 }
Пример #2
0
 private static function send_test_email($params)
 {
     $to = $params['sendgrid_to'];
     if (!Sendgrid_Tools::is_valid_email($to)) {
         return array('message' => 'Email address in field "To" is not valid.', 'status' => 'error', 'error_type' => 'sending');
     }
     $subject = stripslashes($params['sendgrid_subj']);
     $body = stripslashes($params['sendgrid_body']);
     $headers = $params['sendgrid_headers'];
     if (!Sendgrid_Tools::valid_emails_in_headers($headers)) {
         return array('message' => 'One or more email addresses in field "headers" are not valid.', 'status' => 'error', 'error_type' => 'sending');
     }
     if (preg_match('/content-type:\\s*text\\/html/i', $headers)) {
         $body_br = nl2br($body);
     } else {
         $body_br = $body;
     }
     $sent = wp_mail($to, $subject, $body_br, $headers);
     if (true === $sent) {
         return array('message' => 'Email was sent.', 'status' => 'updated');
     }
     return array('message' => 'Email wasn\'t sent.', 'status' => 'error', 'error_type' => 'sending');
 }
 /**
  * Uploads a contact using the parameters specified in the settings page
  *
  * @param  mixed   $params    array of parameters from $_POST
  *
  * @return mixed              response array with message and status
  */
 private static function send_contact_upload_test($params)
 {
     $email = $params['sendgrid_test_email'];
     if (!Sendgrid_Tools::is_valid_email($email)) {
         return array('message' => 'Email address provided is invalid.', 'status' => 'error', 'error_type' => 'upload');
     }
     switch (Sendgrid_Tools::get_auth_method()) {
         case 'apikey':
             $apikey = Sendgrid_Tools::get_api_key();
             if (!Sendgrid_Tools::check_api_key($apikey, true)) {
                 return array('message' => 'API Key used for mail send is invalid or without permissions.', 'status' => 'error', 'error_type' => 'upload');
             }
             break;
         case 'credentials':
             $username = Sendgrid_Tools::get_username();
             $password = Sendgrid_Tools::get_password();
             if (!Sendgrid_Tools::check_username_password($params['sendgrid_username'], $params['sendgrid_password'], true)) {
                 return array('message' => 'Credentials used for mail send are invalid.', 'status' => 'error', 'error_type' => 'upload');
             }
             break;
         default:
             return array('message' => 'An error occured when trying to check your transactional credentials. Please check that they are correct on the General Settings tab.', 'status' => 'error', 'error_type' => 'upload');
     }
     if (false == Sendgrid_OptIn_API_Endpoint::send_confirmation_email($email, '', '', true)) {
         return array('message' => 'An error occured when trying send the subscription email. Please make sure you have configured all settings properly.', 'status' => 'error', 'error_type' => 'upload');
     }
     return array('message' => 'Subscription confirmation email was sent.', 'status' => 'updated');
 }
 /**
  * Returns true if all the emails in the headers are valid, false otherwise
  *
  * @param   mixed  $headers   string or array of headers
  *
  * @return  bool
  */
 public static function valid_emails_in_headers($headers)
 {
     if (!is_array($headers)) {
         // Explode the headers out, so this function can take both
         // string headers and an array of headers.
         $tempheaders = explode("\n", str_replace("\r\n", "\n", $headers));
     } else {
         $tempheaders = $headers;
     }
     // If it's actually got contents
     if (!empty($tempheaders)) {
         // Iterate through the raw headers
         foreach ((array) $tempheaders as $header) {
             if (false === strpos($header, ':')) {
                 continue;
             }
             // Explode them out
             list($name, $content) = explode(':', trim($header), 2);
             // Cleanup crew
             $name = trim($name);
             $content = trim($content);
             switch (strtolower($name)) {
                 // Mainly for legacy -- process a From: header if it's there
                 case 'from':
                     if (false !== strpos($content, '<')) {
                         $from_email = substr($content, strpos($content, '<') + 1);
                         $from_email = str_replace('>', '', $from_email);
                         $from_email = trim($from_email);
                     } else {
                         $from_email = trim($content);
                     }
                     if (!Sendgrid_Tools::is_valid_email($from_email)) {
                         return false;
                     }
                     break;
                 case 'cc':
                     $cc = explode(',', $content);
                     foreach ($cc as $key => $recipient) {
                         if (!Sendgrid_Tools::is_valid_email(trim($recipient))) {
                             return false;
                         }
                     }
                     break;
                 case 'bcc':
                     $bcc = explode(',', $content);
                     foreach ($bcc as $key => $recipient) {
                         if (!Sendgrid_Tools::is_valid_email(trim($recipient))) {
                             return false;
                         }
                     }
                     break;
                 case 'reply-to':
                     if (!Sendgrid_Tools::is_valid_email($content)) {
                         return false;
                     }
                     break;
                 case 'x-smtpapi-to':
                     $xsmtpapi_tos = explode(',', trim($content));
                     foreach ($xsmtpapi_tos as $xsmtpapi_to) {
                         if (!Sendgrid_Tools::is_valid_email($xsmtpapi_to)) {
                             return false;
                         }
                     }
                     break;
                 default:
                     break;
             }
         }
     }
     return true;
 }