Search code examples
phpwordpressemailmail-server

Sending text message through email address - Messages out of order


I have an app running in wordpress ( using $wpdb ) to send a text through email. They send to a 1234567890@vtext.com address, which texts their mobile phone.

I've noticed that some carriers treat long texts as a media message, so my code splits long messages into multiple emails. Here's that code:

for ( $i=0; $i<count($recipientEmail); $i++ ) {
        $to = $recipientEmail[$i];
        if ( strlen($message) > 115 ) {
            // ceil rounds any fractions up
            $numTexts = ceil(strlen($message) / 115);
            $zero = 0;
            $onetwentyfive = 115;
            $noRepeatconfirmations = '';
            for ( $n=0; $n<$numTexts; $n++ ) {
                $messagePart = substr($message, $zero, $onetwentyfive);
                $messagePart .= "\r\n".($n+1)." of ".$numTexts;
                if ($n > 0 ) {
                    $messagePart = '-'.$messagePart;
                }
                // if ( !filter_var( $to, FILTER_VALIDATE_EMAIL) ) {
                if ( !wp_mail($to, $subject, $messagePart, $headers) ) {
                    return false;
                } else {
                    if ( $noRepeatconfirmations !== $recipientEmail[$i] ) {
                        $total .= $recipientEmail[$i].", ";
                    }
                    $noRepeatconfirmations = $recipientEmail[$i];
                }
                $zero += 115;
                $onetwentyfive += 115;
            }
        } else {
            // if ( !filter_var( $to, FILTER_VALIDATE_EMAIL) ) {
            if ( !wp_mail($to, $subject, $message, $headers) ) {
                return false;
            } else {
                $total .= $recipientEmail[$i].", ";
            }
        }
    }

My problem is that when I send a long alert to myself it sends in the correct order, but when I'm sending a message to 150+ people it sends in a weird order (3rd, 1st, 2nd).

I was wondering if there's a method I could use in my code or a setting on my mail sever i can use to make the messages send in order.


Solution

  • There is no way to guarantee SMS, MMS, or email messages will be received in the order they were sent, short of spacing them out 10 seconds or so apart. Hopefully message A will be delivered before B is sent, and B is delivered before C, and so on.

    Even still this is not 100% as some mail servers will queue up messages before delivery (sender and receiving end points).

    It's just the nature of the beast.

    NOTE: I'm not 100% sure on MMS, but 100% positive on email, and about 75% on SMS.