简体   繁体   English

如何使用php在同一时间从表向2个人发送电子邮件

[英]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. 我的php代码遇到了麻烦。 I have a table named: prof_ales with 3 columns: cnp, evaluat and evaluator. 我有一个名为:prof_ales的表,该表具有3列:cnp,evaluat和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". 当客户按下按钮时,我想向2个特定的电子邮件地址发送电子邮件,这些地址已经保存到我的表“ prof_ales”的“评估者”列中。

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. Tks。

You must put the code to send the email in the while loop. 您必须放入代码以 while循环中发送电子邮件。 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. 如前所述,您需要将mail()函数的调用放入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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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