开发者

php error with mail() need clarification

im new to php and i need a bit of clarification. ive searched google but tooo many results, none clearly identify whats wrong with mine. since i dont get any errors im not sure whats happening.

the error im getting is this:

Warning: mail() expects parameter 1 to be string, array given in

so longstory short, this is a small bit of my code:

while( $sn_rowSelect = mysqli_fetch_array($sn_queryResult) ) { 
    mail($to,$subject,$mssg,'from:xyz');
}

if(mail($to,$subject,$mssg,'from:xyz')) {
echo "An e-mail was sent to $to with the subject: $subject";
} else {
echo "There was a problem sending the mail. Check your code and make sure that the e-mail address $to is valid";
}

what im trying to do is, i have a small database with names etc, with this loop above, its suppose to say that while theres a name in $sn_rowSelect, to feed it the message, essentially emailing the whole lot of 开发者_运维技巧emails.

the error says "expects string, array given' im assuming means the name gotten from the DB is gotten as an assosiative array value, but, isnt that what its supposed to do?

thanks in advance.

EDIT***

$to = $sn_rowSelect;
$from = $_POST['sender'];
$mssg = $_POST['message'];

from and mssg, are filled out via a form on another page.


No, you're supposed to get the actual users name as a string, not an array.

Something like $row['name'] - do a var_dump of your value to find out how the data is stored, or a print_r.


You must set the $to parameter inside the while loop, and you need to set it to a string. You will also need to move the if statement inside the while loop if you want it to make sense, and you can't call mail() twice since that will create redundant emails.

Here's a fixed code for you:

$from = $_POST['sender'];
$mssg = $_POST['message'];

while($sn_rowSelect = mysqli_fetch_array($sn_queryResult) ) {
    $to = sn_rowSelect['email']; //change 'email' to the name of column in db
    $mailstatus = mail($to, $subject, $mssg, 'from:xyz');

    if($mailstatus) {
        echo "An e-mail was sent to $to with the subject: $subject\n";
    }else{
        echo "There was a problem sending the mail. Check your code and make sure that the e-mail address $to is valid\n";
    }
}


It seems like you are trying to input an array that contains multiple e-mails directly into $to.

You can input multiple e-mails into the mail function, but they have to be separated by comma's.

So change this:

mail($to,$subject,$mssg,'from:xyz');

to:

$to = implode(",",$to);
mail($to,$subject,$mssg,'from:xyz');

Also be careful, you are doing a check after you are sending the mail like this:

if(mail($to,$subject,$mssg,'from:xyz')) {

This will actually send the e-mail again. If you want to check if your e-mail function is working, add the if statement to the original code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜