简体   繁体   中英

How to send an email to 2 persons from a table at the same time using php

I'm getting trouble with my php code. I have a table named: prof_ales with 3 columns: cnp, evaluat and evaluator.

And a button "Send Email". When the client press the button I want to send an email to 2 specific email adresses which are already saved into my table "prof_ales" in the column "evaluator".

Here is my code, and for now it's working, but the message is send only at the last email adress.

    <input class="buttom" name="submit" id="submit" tabindex="5" value="Send Email" type="submit">  



<?php
if(isset($_POST['submit'])) {
$interogare = ("SELECT * FROM prof_ales  WHERE cnp='".$_SESSION['sess_user']."'");
$rezultat = mysql_query($interogare);
while ($rand = mysql_fetch_assoc($rezultat)) {
$ev= $rand['evaluat'];
$to = $rand['evaluator'];
}

$message = "test email ";
$subject = "Received from $ev"; 
$body = <<<EMAIL
 Etc etc

$message

Bla Bla 

EMAIL;

$header = "From: quabits.ro";
}
if($_POST){
mail($to, $subject, $body, $header);
echo "Sent";
}
?>  

As I said I want to send to both(or more) email adresses any time when button is pressed. Tks.

You must put the code to send the email in the while loop. So it would be:

    <?php
    $connect = mysqli_connect("","","","");
    if(isset($_POST['submit'])) {
    $interogare = ("SELECT * FROM prof_ales  WHERE cnp='".$_SESSION['sess_user']."'");
    $rezultat = mysqli_query($interogare,$connect);
    while ($rand = mysqli_fetch_assoc($rezultat)) {
    $ev= $rand['evaluat'];
    $to = $rand['evaluator'];

    $message = "test email ";
    $subject = "Received from $ev"; 
    $body = <<<EMAIL
     Etc etc

    $message

    Bla Bla 

    EMAIL;

    $header = "From: quabits.ro";
    }
    if($_POST){
    mail($to, $subject, $body, $header);
    echo "Sent";
    }
    }
    ?> 

The code you wrote just meant:

select the first email from database and save it into a variable. Replace the value into the variable with the second email. Send a mail to the address in the variable.

The improved version means:

select the first email from database and save it into a variable. Send a mail to the address in the variable. Replace the value into the variable with the second email. Send a mail to the address in the variable.

As already said above you need to put the call of the mail() function inside the while() -loop. Using correct formating makes the code easier to read:

while ($rand = mysql_fetch_assoc($rezultat)) {
    $ev= $rand['evaluat'];
    $to = $rand['evaluator'];
    $message = "test email ";
    $subject = "Received from $ev"; 
    $body = "Test";
    $header = "From: quabits.ro";
    if($_POST) {
        mail($to, $subject, $body, $header);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM