SimpleEmailServiceMessage::validate PHP Method

validate() public method

This does not guarantee the message will arrive, nor that the request will succeed; instead, it makes sure that no required fields are missing. This is used internally before attempting a SendEmail or SendRawEmail request, but it can be used outside of this file if verification is desired. May be useful if e.g. the data is being populated from a form; developers can generally use this function to verify completeness instead of writing custom logic.
public validate ( ) : boolean
return boolean
    public function validate()
    {
        // at least one To: destination is required
        if (count($this->to) == 0) {
            return false;
        }
        // sender is required
        if ($this->from == null || strlen($this->from) == 0) {
            return false;
        }
        // subject is required
        if ($this->subject == null || strlen($this->subject) == 0) {
            return false;
        }
        // message is required
        if ((empty($this->messagetext) || strlen((string) $this->messagetext) == 0) && (empty($this->messagehtml) || strlen((string) $this->messagehtml) == 0)) {
            return false;
        }
        return true;
    }

Usage Example

 /**
  * Given a SimpleEmailServiceMessage object, submits the message to the service for sending.
  *
  * @param SimpleEmailServiceMessage $sesMessage An instance of the message class
  * @param boolean $use_raw_request If this is true or there are attachments to the email `SendRawEmail` call will be used
  * @param boolean $trigger_error Optionally overwrite the class setting for triggering an error (with type check to true/false)
  * @return array An array containing the unique identifier for this message and a separate request id.
  *         Returns false if the provided message is missing any required fields.
  *  @link(AWS SES Response formats, http://docs.aws.amazon.com/ses/latest/DeveloperGuide/query-interface-responses.html)
  */
 public function sendEmail($sesMessage, $use_raw_request = false, $trigger_error = null)
 {
     if (!$sesMessage->validate()) {
         $this->__triggerError('sendEmail', 'Message failed validation.');
         return false;
     }
     $rest = new SimpleEmailServiceRequest($this, 'POST');
     $action = !empty($sesMessage->attachments) || $use_raw_request ? 'SendRawEmail' : 'SendEmail';
     $rest->setParameter('Action', $action);
     if ($action == 'SendRawEmail') {
         // echo $sesMessage->getRawMessage();return;
         $rest->setParameter('RawMessage.Data', $sesMessage->getRawMessage());
     } else {
         $i = 1;
         foreach ($sesMessage->to as $to) {
             $rest->setParameter('Destination.ToAddresses.member.' . $i, $sesMessage->encodeRecipients($to));
             $i++;
         }
         if (is_array($sesMessage->cc)) {
             $i = 1;
             foreach ($sesMessage->cc as $cc) {
                 $rest->setParameter('Destination.CcAddresses.member.' . $i, $sesMessage->encodeRecipients($cc));
                 $i++;
             }
         }
         if (is_array($sesMessage->bcc)) {
             $i = 1;
             foreach ($sesMessage->bcc as $bcc) {
                 $rest->setParameter('Destination.BccAddresses.member.' . $i, $sesMessage->encodeRecipients($bcc));
                 $i++;
             }
         }
         if (is_array($sesMessage->replyto)) {
             $i = 1;
             foreach ($sesMessage->replyto as $replyto) {
                 $rest->setParameter('ReplyToAddresses.member.' . $i, $sesMessage->encodeRecipients($replyto));
                 $i++;
             }
         }
         $rest->setParameter('Source', $sesMessage->encodeRecipients($sesMessage->from));
         if ($sesMessage->returnpath != null) {
             $rest->setParameter('ReturnPath', $sesMessage->returnpath);
         }
         if ($sesMessage->subject != null && strlen($sesMessage->subject) > 0) {
             $rest->setParameter('Message.Subject.Data', $sesMessage->subject);
             if ($sesMessage->subjectCharset != null && strlen($sesMessage->subjectCharset) > 0) {
                 $rest->setParameter('Message.Subject.Charset', $sesMessage->subjectCharset);
             }
         }
         if ($sesMessage->messagetext != null && strlen($sesMessage->messagetext) > 0) {
             $rest->setParameter('Message.Body.Text.Data', $sesMessage->messagetext);
             if ($sesMessage->messageTextCharset != null && strlen($sesMessage->messageTextCharset) > 0) {
                 $rest->setParameter('Message.Body.Text.Charset', $sesMessage->messageTextCharset);
             }
         }
         if ($sesMessage->messagehtml != null && strlen($sesMessage->messagehtml) > 0) {
             $rest->setParameter('Message.Body.Html.Data', $sesMessage->messagehtml);
             if ($sesMessage->messageHtmlCharset != null && strlen($sesMessage->messageHtmlCharset) > 0) {
                 $rest->setParameter('Message.Body.Html.Charset', $sesMessage->messageHtmlCharset);
             }
         }
     }
     $rest = $rest->getResponse();
     if ($rest->error === false && $rest->code !== 200) {
         $response = array('code' => $rest->code, 'error' => array('Error' => array('message' => 'Unexpected HTTP status')));
         return $response;
     }
     if ($rest->error !== false) {
         if ($this->__trigger_errors && $trigger_error !== false || $trigger_error === true) {
             $this->__triggerError('sendEmail', $rest->error);
             return false;
         }
         return $rest;
     }
     $response = array('MessageId' => (string) $rest->body->{"{$action}Result"}->MessageId, 'RequestId' => (string) $rest->body->ResponseMetadata->RequestId);
     return $response;
 }