• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 523
  • Last Modified:

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
0
jlazanowski
Asked:
jlazanowski
  • 6
  • 2
1 Solution
 
jlazanowskiAuthor Commented:
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
0
 
nesnemisCommented:
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
0
 
Diablo84Commented:
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Diablo84Commented:
Another problem may be this:

switch ($action) {

it relys on register globals being on, should be:

switch ($_POST['action']) {
0
 
Diablo84Commented:
A couple of other things to note:

1) instead of looping throught the mails you can send one mail to multiple addresses just by seperating the addresses by a comma, eg:

mail("address1@domain.com,address2@domain.com,address3@domain.com,address4@domain.com","subject","message");

See a similar example here:

http://us2.php.net/manual/en/function.mail.php

2) When using the mail function you would save yourself a lot of trouble by just using phpmailer, a php class.

http://phpmailer.sourceforge.net/

Example based on one from site:

#####################
require("class.phpmailer.php");

$mail = new PHPMailer();

$mail->From     = "from@email.com";
$mail->FromName = "Mailer";
$mail->AddAddress("josh@site.com","Josh Adams");
$mail->AddAddress("ellen@site.com");               // optional name
$mail->AddReplyTo("info@site.com","Information");

$mail->Subject  =  "Here is the subject";
$mail->Body     =  "This is the <b>HTML body</b>";
$mail->AltBody  =  "This is the text-only body";

if(!$mail->Send())
{
   echo "Message was not sent <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
#####################

It is much easier then going through what you are doing.

Some more examples on their site: http://phpmailer.sourceforge.net/extending.html

For help with getting it working see my latter comment in this thread: http:Q_21141127.html
0
 
jlazanowskiAuthor Commented:
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).

0
 
Diablo84Commented:
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");
0
 
Diablo84Commented:
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>
0
 
Diablo84Commented:
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now