Boaz Posted January 19, 2009 Share Posted January 19, 2009 I've been using a handy script I found on the net somewhere, it has been working great, until my cell provider started limiting my message length. I need to modify this so that it will break up the message every 160 characters and send additional emails until the whole message is processed. Any help is greatly appreciated! Thanks. <?php // get posted data into local variables // replace John Doe's with your own name and address $EmailFrom = Trim($_POST[EmailFrom]); $EmailTo = "Test <test@test.com>"; $Subject = "Test"; $Message = Trim($_POST[Message]); // validation $validationOK=true; if (Trim($EmailFrom)=="") $validationOK=false; if (Trim($Message)=="") $validationOK=false; if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=no.htm\">"; exit;} // prepare email body text // tells you that the mail is sent from this form // $Body .= "Test Form:\n"; $Body = "$Message"; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); // redirect to success page if ($success){print "<meta http-equiv=\"refresh\" content=\"0;URL=yes.htm\">";} else{print "<meta http-equiv=\"refresh\" content=\"0;URL=no.htm\">";} // send copy of email to visitor // replace John Doe's with your own name and address // mail("$Name <$EmailFrom>", "John Doe has received your message '$Subject'", // "You wrote: $Message", "From: John Doe <johndoe@dot.com>"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/141391-solved-simple-mail-form-need-to-break-apart-message/ Share on other sites More sharing options...
MadTechie Posted January 19, 2009 Share Posted January 19, 2009 Try this <?php preg_match_all('/.{0,160}/sim', $Message, $parts); $parts = $parts[0]; foreach($parts as $part) { if(!empty($part)) { $Body = "$part"; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/141391-solved-simple-mail-form-need-to-break-apart-message/#findComment-740138 Share on other sites More sharing options...
MadTechie Posted January 19, 2009 Share Posted January 19, 2009 Sorry replace the regex with $parts=str_split($Message,160); ie (its quicker) <?php $parts=str_split($Message,160); foreach($parts as $part) { if(!empty($part)) { $Body = "$part"; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/141391-solved-simple-mail-form-need-to-break-apart-message/#findComment-740140 Share on other sites More sharing options...
Boaz Posted January 19, 2009 Author Share Posted January 19, 2009 Awesome! Man I wish I could code. Thanks for helping! The first one worked, but the second one didn't. The problem I ran into was that the messages processed so fast, they arrived out of order. Is there a way to wait between each mail send? Quote Link to comment https://forums.phpfreaks.com/topic/141391-solved-simple-mail-form-need-to-break-apart-message/#findComment-740150 Share on other sites More sharing options...
MadTechie Posted January 19, 2009 Share Posted January 19, 2009 updated.. this should work <?php $parts=str_split($Message,160); $count = count($parts); foreach($parts as $K=>$part) { if(!empty($part)) { $Body = "$part"; // send email $PSubject = $Subject." (".($K+1)." of $count)"; $success = mail($EmailTo, $PSubject, $Body, "From: <$EmailFrom>"); sleep(1); //wait 1 second } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/141391-solved-simple-mail-form-need-to-break-apart-message/#findComment-740155 Share on other sites More sharing options...
Boaz Posted January 19, 2009 Author Share Posted January 19, 2009 Hrm, still didn't come back to the success page or send the messages? Quote Link to comment https://forums.phpfreaks.com/topic/141391-solved-simple-mail-form-need-to-break-apart-message/#findComment-740158 Share on other sites More sharing options...
MadTechie Posted January 19, 2009 Share Posted January 19, 2009 Okay use preg_match_all, i assume your on PHP4.. as str_split is PHP5 <?php preg_match_all('/.{0,160}/sim', $Message, $parts); $parts = $parts[0]; $count = count($parts); foreach($parts as $K=>$part) { if(!empty($part)) { $Body = "$part"; // send email $PSubject = $Subject." (".($K+1)." of $count)"; $success = mail($EmailTo, $PSubject, $Body, "From: <$EmailFrom>"); sleep(1); //wait 1 second } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/141391-solved-simple-mail-form-need-to-break-apart-message/#findComment-740162 Share on other sites More sharing options...
Boaz Posted January 19, 2009 Author Share Posted January 19, 2009 Okay use preg_match_all, i assume your on PHP4.. as str_split is PHP5 Indeed, you are right again, sorry I didn't realize it wasn't updated until I just checked. Thank you very much kind sir, you are the man! Quote Link to comment https://forums.phpfreaks.com/topic/141391-solved-simple-mail-form-need-to-break-apart-message/#findComment-740167 Share on other sites More sharing options...
MadTechie Posted January 19, 2009 Share Posted January 19, 2009 Your welcome Oh i should also say you can change sleep to usleep.. for microseconds ie sleep(1); = usleep(1000000); == both = 1 second but your probably need to do some testing for how long you should wait before sending the next message personally i try not to use sleep (in life or programming) as you may get timeouts see here to extend it or just add set_time_limit(0); at the start of the script If thats solved it the problem then please click topic solved Quote Link to comment https://forums.phpfreaks.com/topic/141391-solved-simple-mail-form-need-to-break-apart-message/#findComment-740170 Share on other sites More sharing options...
Boaz Posted January 19, 2009 Author Share Posted January 19, 2009 PS- In case anyone else uses this cool tool, update the message count, it was off by 1. $PSubject = $Subject." (".($K+1)." of ".($count-1).")"; Quote Link to comment https://forums.phpfreaks.com/topic/141391-solved-simple-mail-form-need-to-break-apart-message/#findComment-740186 Share on other sites More sharing options...
MadTechie Posted January 19, 2009 Share Posted January 19, 2009 Ahh should of checked that but you should change it here $count = count($parts)-1; saves a repeated calculation Quote Link to comment https://forums.phpfreaks.com/topic/141391-solved-simple-mail-form-need-to-break-apart-message/#findComment-740187 Share on other sites More sharing options...
Boaz Posted January 19, 2009 Author Share Posted January 19, 2009 I switched to PHP5, the version check shows it's active, but the str_split still hangs. Odd, I'll just use the v4. // send email // $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); $parts=str_split($Message,160); $count = count($parts)-1; foreach($parts as $K=>$part) { if(!empty($part)) { $Body = "$part"; // send email $PSubject = $Subject." (".($K+1)." of $count)"; $success = mail($EmailTo, $PSubject, $Body, "From: <$EmailFrom>"); sleep(1); //wait 1 second } } // redirect to success page if ($success){print "<meta http-equiv=\"refresh\" content=\"0;URL=yes.htm\">";} else{print "<meta http-equiv=\"refresh\" content=\"0;URL=no.htm\">";} Quote Link to comment https://forums.phpfreaks.com/topic/141391-solved-simple-mail-form-need-to-break-apart-message/#findComment-740191 Share on other sites More sharing options...
Boaz Posted January 19, 2009 Author Share Posted January 19, 2009 EDIT: Nevermind, it does work and it is faster. Also, when using the str_split the total message number calculates correctly, so no need for the -1 I'm done now...promise Quote Link to comment https://forums.phpfreaks.com/topic/141391-solved-simple-mail-form-need-to-break-apart-message/#findComment-740193 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.