0

I read all topics related to this question in stackoverflow and whole internet and cant find working sollution... Each owner has his item and when someone buy his item, owner gets an confirmation email, but when in cart is few same owner items, he gets several same email letters, so I need to remove dublicated array entries. I have tried to use DISTINCT and array_uniques functions but no luck. Any advices?

I have an array and function to send mail..

function email($mail_array) {
  foreach(array_unique($mail_array) as $field => $value) {
    $result = mysql_query("select email from users where $field='$value'");
    $row = mysql_fetch_array($result);


       $maail = mysql_real_escape_string($row['email']);
}
        $email_to = "".$maail."";



    // rest of mail formatting code

    // create email headers
    $headers = 'From: '.$email_from."\r\n" .
               'Reply-To: '.$email_from."\r\n" .
               'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);
}


for ($i = 0; $i < $max; $i++) {
  $pid = $_SESSION['cart'][$i]['productid'];
  $owner = get_owner($pid);
  $mail_array = array(
    'name' => $owner
  );
  email($mail_array)  //call function to send mail          
}
4
  • What is the content of $mail_array? Post it Here Commented Mar 23, 2012 at 11:18
  • 1
    I don't see any per-user call here that I would expect to be unique. In what way should sending emails be unique? You are just passing array( 'name' => $owner ) and trying to find user by name Commented Mar 23, 2012 at 11:20
  • It's probably just a copy/paste error but this code does not parse - it is missing a } to close the foreach in the function. Please make sure your code examples are correct and please space your code sensibly - as soon as I reformatted the code the parse error became immediately obvious. Commented Mar 23, 2012 at 11:24
  • print_r($mail_array); Array ( [name] => Administratorius ) Array ( [name] => Vaidas ) Array ( [name] => Vaidas ) (in this case I've choosen three items to the shopping cart) Commented Mar 23, 2012 at 11:25

4 Answers 4

1

You are passing $mail_array with a single value to mail function,so multiple mails are being sent. Instead, make $owner as array() and push all owners name in it.But before that check whether the array has the same owner already using in_array(). If owner is not there in the array then push it in the array otherwise do next iteration. And pass this array to mailing function.

    $mail_array = array();
    for ($i = 0; $i < $max; $i++) {
    $pid = $_SESSION['cart'][$i]['productid'];
    $owner = get_owner($pid);
    $mail_array = array_push($mail_array,$owner);    
    }
    email($mail_array)  //call function to send mail   
Sign up to request clarification or add additional context in comments.

Comments

1

You need to move your mail function outside of your loop :

$mail_array = array();
for($i=0;$i<$max;$i++){
    $pid=$_SESSION['cart'][$i]['productid'];
    $owner=get_owner($pid);
?>
<?
    $mail_array[] = $owner;
}
email($mail_array)  //call function to send mail

... you'll have to change the mail function accordingly since you're not passing the name key this way. Or add the name key back to the array and change the array processing in the mail function to handle an array of arrays.

Comments

0

Create an empty array where you will store email addresses you already sent a message to:

$arrSent = array();

Before you send an email check if it exists in your array:

if (!in_array($email_to, $arrSent))
{
    //send the email
}

Then when you sent a mail add the address to this array:

@mail($email_to, $email_subject, $email_message, $headers);
$arrSent[] = $email_to;

Comments

0

Since you are only ever using the name field, it makes more sense to statically code this in to the query and just pass an indexed array of names. The array_unique will work correctly.

E.g.

function email($mail_array) {
  foreach(array_unique($mail_array) as $name) {
    $result = mysql_query("SELECT email FROM users WHERE name = '".mysql_real_escape_escape_string($name)."'");
    // ...
  }
}

$mail_array = array();
for ($i = 0; $i < $max; $i++) {
  $pid = $_SESSION['cart'][$i]['productid'];
  $owner = get_owner($pid);
  $mail_array[] = $owner;
  email($mail_array)  //call function to send mail          
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.