/**
  * 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;
 }
 /** 
  * Send OptIn email
  *  
  * @param  string $email      Email of subscribed user
  * @param  string $first_name First Name of subscribed user
  * @param  string $last_name  Last Name of subscribed user
  * @return bool
  */
 public static function send_confirmation_email($email, $first_name = '', $last_name = '', $from_settings = false)
 {
     $subject = Sendgrid_Tools::get_mc_signup_email_subject();
     $content = Sendgrid_Tools::get_mc_signup_email_content();
     $content_text = Sendgrid_Tools::get_mc_signup_email_content_text();
     if (false == $subject or false == $content or false == $content_text) {
         return false;
     }
     $subject = stripslashes($subject);
     $content = stripslashes($content);
     $content_text = stripslashes($content_text);
     $to = array($email);
     $token = Sendgrid_OptIn_API_Endpoint::generate_email_token($email, $first_name, $last_name);
     $transient = get_transient($token);
     if ($transient and isset($transient['email']) and !$from_settings) {
         return false;
     }
     if (false == set_transient($token, array('email' => $email, 'first_name' => $first_name, 'last_name' => $last_name), 24 * 60 * 60) and !$from_settings and $transient) {
         return false;
     }
     $confirmation_link = site_url() . '?__sg_api=1&token=' . $token;
     $headers = new SendGrid\Email();
     $headers->addSubstitution('%confirmation_link%', array($confirmation_link))->addCategory('wp_sendgrid_subscription_widget');
     add_filter('sendgrid_mail_text', function () use(&$content_text) {
         return $content_text;
     });
     $result = wp_mail($to, $subject, $content, $headers);
     return $result;
 }
 /**
  * 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');
 }