Link to home
Start Free TrialLog in
Avatar of jlazanowski
jlazanowski

asked on

PHP and sendmail in a loop



I have a problem.

I am trying to create a form that will send out e-mail via send mail to a database of users that have signed up for it. (This is not SPAM this is for legitimate users)

I am using an example out of a book that I modified for my purposes (I am a PHP newbie)

Here is the code that I am using
<?php

function mail_header()
{
?>
<html>
<head>
<title>Mailer</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
}
function mail_footer()
{
?>
</BODY>
</HTML>
<?php
}


  function send_mail()
  {
          $mail_to = $mail_message["mail_to"];
      $mail_from = $mail_message["mail_from"];
      $mail_reply_to = $mail_message["reply_to"];
      $mail_subject = $mail_message["mail_subject"];
      $mail_body = $mail_message["mail_body"];
      
      //error handle any blank items
      if (empty($mail_to)) error_message("You did not address the e-mail");
      if (empty($mail_from)) error_message("The e-mail does not have a from address");
      if (empty($mail_subject)) error_message("The e-mail has no subject");
      if (empty($mail_body)) error_message("The e-mail has no body");
      
      
      //remove a semicolon as the delimter to the e-mail and replace with a , for sendmail
      
      $mail_to = str_replace(";", ",", $mail_to);
      
      
      $mail_headers =  '';
      if (!empty($mail_from)) $mail_headers .= "From: $mail_from\n";
      if (!empty($mail_reply_to)) $mail_headers .= "Reply-To: $mail_reply_to\n";
      
      $mail_subject = stripslashes($mail_subject);
      $mail_body = stripslashes($mail_body);
      
      
      return mail($mail_to, $mail_subject, $mail_body, $mail_headers);
      
}
function error_message($msg)
{
echo "<script>alert(\"$msg\");history.go(-1)</SCRIPT>";
exit;
}

function user_message($msg)
{
echo "<script>alert(\"$msg\");history.go(-1)</script>";
}

function mail_form()
{
?>
<form method="POST" enctype="multipart/form-data" action="<?php echo $PHP_SELF ?>">
<input type="hidden" name="action" value="send_mail">
<br>
<br>
<input type="submit" value="Process" name="Process">
</form>

<?php
}



function create_mail()
{
      global $mail_to, $mail_cc, $mail_bcc, $mail_from, $mail_reply_to;
      global $mail_body, $mail_subject, $mail_message;
      
      
      
      
      for($i=0; $i < 5; $i++)
      {
            $mail_message["mail_to"] = "justin@xyz.com";
            $mail_message["mail_from"] = "someone";
            $mail_message["mail_reply_to"] = "justin@xyz.com";
            $mail_message["mail_subject"] = "Just testing it out";
            $mail_message["mail_body"] = "test";
            
            
            
            if(send_mail($mail_message))
                  echo "Sent e-mail to : " '$mail_to' ";
            else error_message("problem");
            
      
      
            
      }
}


      
switch ($action)
{
      case "send_mail":
            mail_header();
            create_mail();
            mail_footer();
            break;
      default:
            mail_header();
            mail_form();
            mail_footer();
            break;
}
?>


If the code executes like this. I get no return from send mail, nothing I don't get an error, I don't get the echo that the mail sent I just get nothing.

If I modify the function send_mail() and insert values into the $mail_to, $mail_from etc.. It will work and it will loop though it four five times (which is what it is supposed to do).


Any ideas why the variables are not passing to the send_mail function?

Thanks,

Justin
Avatar of jlazanowski
jlazanowski

ASKER

ugh... never mind I think I just caught it. The send_mail function isn't taking any arguments so it's not passing the $mail_message array.

Let me know if there is anything else that you experts can catch.

Thanks,
Justin
Hi jlazanowski,

>  $mail_message["mail_to"] = "justin@xyz.com";
>           $mail_message["mail_from"] = "someone";
>           $mail_message["mail_reply_to"] = "justin@xyz.com";
>           $mail_message["mail_subject"] = "Just testing it out";
>           $mail_message["mail_body"] = "test";
When you use this you get the same message twice and when you don't you get nothing, am I right??

Assuming that's the case:
    where do you populate your $mail_message-array when you donæt use this code, it looks like this array contains nothing

nesnemis
I haven't checked the script completely yet but there is an error on this line:

echo "Sent e-mail to : " '$mail_to' ";

If you are just looking to display something like: Sent e-mail to: name
then it only needs to be:

echo "Sent e-mail to : $mail_to";

If you want those single quote around the name add them back in, it was the extra " causing the problem.
Another problem may be this:

switch ($action) {

it relys on register globals being on, should be:

switch ($_POST['action']) {
ASKER CERTIFIED SOLUTION
Avatar of Diablo84
Diablo84

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
well the problem I have is that we may have 1000, 10,000, 100,000 e-mail addresses sign up (not sure how many yet)

So if I stick them all in on e-mail then I am worried that it will fail with to many addresses. That is why I am sending it one by one, besideds the addresses should not be shared. I don't want customer A to know customer B's address because they were included in the same e-mail (if that make sense).

Yes completely understandable, i will take another look through your script.

You may still want to consider phpmailer as it has the option to add a BCC (blind carbon copy) so you still only send one mail wit hall the addresses but they will only be able to se their own address.

$mail->addBCC ("address@domain.com","Name");
As for your script you could do something like: (untested currently)

<html>
<head>
<title>Mailer</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<?php
function send_mail($mail) {    
 //error handle any blank items
 if (empty($mail['to'])) error_message("You did not address the e-mail");
 if (empty($mail['from'])) error_message("The e-mail does not have a from address");
 if (empty($mail['subject'])) error_message("The e-mail has no subject");
 if (empty($mail['body'])) error_message("The e-mail has no body");
     
 //remove a semicolon as the delimter to the e-mail and replace with a , for sendmail
 $mail['to'] = str_replace(";", ",", $mail['to']);
       
 $mail_headers =  '';
 if (!empty($mail['from'])) $mail_headers .= "From: {$mail['from']}\n";
 if (!empty($mail['reply_to'])) $mail_headers .= "Reply-To: {$mail['reply_to']}\n";
     
 $mail['subject'] = stripslashes($mail['subject']);
 $mail['body'] = stripslashes($mail['body']);
 
 return mail($mail['to'],$mail['subject'],$mail['body'],$mail_headers);  
}

function error_message($msg) {
 echo "<script>alert(\"$msg\");history.go(-1)</SCRIPT>";
 exit;
}

function user_message($msg) {
 echo "<script>alert(\"$msg\");history.go(-1)</script>";
}

function mail_form() {
 echo '
 <form method="POST" enctype="multipart/form-data" action="'.$_SERVER['PHP_SELF'].'">
 <input type="hidden" name="action" value="send_mail">
 <br>
 <br>
 <input type="submit" value="Process" name="Process">
 </form>';
}

function create_mail() {
 for($i=0;$i<5;$i++) {
  $mail['to'] = "justin@xyz.com";
  $mail['from'] = "someone";
  $mail['reply_to'] = "justin@xyz.com";
  $mail['subject'] = "Just testing it out";
  $mail['body'] = "test";
         
  if (!send_mail($mail)) error_message("problem");
  echo "Sent e-mail to: $mail_to<br>\n";
 }
}

if (isset($_POST['action']) && $_POST['action'] == "send_mail") {
 create_mail();
}
else {
 mail_form();
}
?>

</body>
</html>
missed a line change:

echo "Sent e-mail to: $mail_to<br>\n";

to:

echo "Sent e-mail to: {$mail['to']}<br>\n";

other then that seems to work fine.