Jump to content

[SOLVED] Simple Mail Form-Need to break apart message


Boaz

Recommended Posts

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>");
?>

Link to comment
Share on other sites

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>");
   }
}
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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
   }
}
?>

Link to comment
Share on other sites

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
   }
}
?>

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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\">";}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.