从数据库中获取数据并在php的邮件函数中使用

从数据库中获取数据并在php的邮件函数中使用,php,mysql,phpmailer,Php,Mysql,Phpmailer,我是php新手,目前正在做一个项目 我使用表单将数据存储在数据库中 我有一个邮件功能,当点击提交按钮时,一封邮件被发送到著名的电子邮件id。我使用的是phpmailer库 这是我的邮件功能 class mail { public function sendMail(){ require 'vendor/autoload.php'; $mail = new PHPMailer(); // Passing `true` enables exceptions

我是php新手,目前正在做一个项目

我使用表单将数据存储在数据库中

我有一个邮件功能,当点击提交按钮时,一封邮件被发送到著名的电子邮件id。我使用的是phpmailer库

这是我的邮件功能

class mail 
{

    public function sendMail(){

    require 'vendor/autoload.php';

        $mail = new PHPMailer();  // Passing `true` enables exceptions or null without exception


    //Server settings
    $mail->SMTPDebug = 0;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'xyz@gmail.com';                 // SMTP username
    $mail->Password = 'xyz123';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('xyz@gmail.com', 'xyz');
    $mail->addAddress('abc@gmail.com', 'abc');

    $mail->addCC('zxc@gmail.com');


    //Attachments
    $mail->addAttachment('lob.png', 'sample.png');         // Add attachments
       // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Report';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';


    if (!$mail->send()) {
            return "Error sending message" . $mail->ErrorInfo;
        } else {
            return "Message sent!";
        }


}

}
这是我的html表单

<form class="quality" method="POST" action="send.php">
<label for="name">Name</label>
<input type = "text" class = "form-quality" name="name" value="" / >
<label for="age">Age</label>
<input type="text" class ="form-quality" name="age" value="" / >
<label for="quality">Quality</label>
<input type="ratio" class ="form-quality" name="quality" value="E" / >
<input type="ratio" class ="form-quality" name="quality" value="P" / >
</form>

名称
年龄
品质
1) 如何从数据库中获取数据?
2) 在邮件功能中,以前将电子邮件发送到一个或两个id,但当我从比率按钮中选择“value=E”时,它应该发送到一封电子邮件,如果我选择“value=P”,它应该根据存储在数据库中的用户选择的值发送到另一封电子邮件


如果你说你正在做一个项目是在教育环境中进行的,而不是出于专业目的,那么对我来说,任何有最佳答案的人都需要更多的帮助:

当您在
if(isset($\u POST['submit'])上从表单中获取数据时,
将这些变量作为参数发送到sendMail函数,然后在函数内部,您可以使用参数创建一个变量,以构建格式良好的消息,您可以在
$mail->Body
中发送该消息

在该函数中,您还可以检查您在单选按钮输入中收到的是“E”还是“p”,并给出不同的值(收件人电子邮件)并将其传递到您的addAddress

如果这不是一个学校项目,你必须非常小心地发送或插入到数据库中。发送前验证表单上收到的所有内容,然后继续检查有关SQL注入的注释中的链接


这本应该是一条评论,而不是回复,但我没有足够的声誉对此发表评论,所以我很抱歉。

要从数据库中检索数据,可以使用一个简单的SELECT查询。对于你的问题,只要在谷歌上搜索一下,你就会发现很多关于php的教程,以及如何通过php从单选按钮获取数据。接下来,编写解决方案代码,如果发现一些错误,请在此处询问
<form class="quality" method="POST" action="send.php">
<label for="name">Name</label>
<input type = "text" class = "form-quality" name="name" value="" / >
<label for="age">Age</label>
<input type="text" class ="form-quality" name="age" value="" / >
<label for="quality">Quality</label>
<input type="ratio" class ="form-quality" name="quality" value="E" / >
<input type="ratio" class ="form-quality" name="quality" value="P" / >
</form>