我如何使主题和正文消息从 phpmailer 中的输入中给出

How can i make subject and body message to be given from inputs in phpmailer

这是index.php 现在我所能做的就是给 $mail->Subject 和 $mail->body 赋值,但我希望它是动态的,例如 在这里,我想为主题和消息提供 2 个输入,并将其传递给 $mail->Subject 和 $mail->Body 由于 post 方法启用 ajax 我无法将两个值从输入字段传递到 send_mail.php 任何帮助将不胜感激

<?php
    //index.php
    
    $connect = new PDO("mysql:host=localhost;dbname=testing", "root", "");
    $query = "SELECT * FROM customer ORDER BY customer_id";
    $statement = $connect->prepare($query);
    $statement->execute();
    $result = $statement->fetchAll();
    
    ?>
    <!DOCTYPE html>
    <html>
    <head>
       
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </head>
    <body>
    <br />
    <div class="container">
      
        <br />
        <div class="table-responsive">
    
            <table class="table table-bordered table-striped">
                <tr>
                    <th>Customer Name</th>
                    <th>Email</th>
                    <th>Select</th>
                    <th>Action</th>
                </tr>
                <?php
                $count = 0;
                foreach($result as $row)
                {
                    $count = $count + 1;
                    echo '
                        <tr>
                            <td>'.$row["customer_name"].'</td>
                            <td>'.$row["customer_email"].'</td>
                            <td>
                                <input type="checkbox" name="single_select" class="single_select" data-email="'.$row["customer_email"].'" data-name="'.$row["customer_name"].'" />
                            </td>
                            <td>
                            <button type="button" name="email_button" class="btn btn-info btn-xs email_button" id="'.$count.'" data-email="'.$row["customer_email"].'" data-name="'.$row["customer_name"].'" data-action="single">Send Single</button>
                            </td>
                        </tr>
                        ';
                }
                ?>
                <tr>
                    <td colspan="3"></td>
                    <td><button type="button" name="bulk_email" class="btn btn-info email_button" id="bulk_email" data-action="bulk">Send Bulk</button></td></td>
                </tr>
            </table>
        </div>
    </div>
    </body>
    </html>
    
    <script>
        $(document).ready(function(){
            $('.email_button').click(function(){
                $(this).attr('disabled', 'disabled');
                var id  = $(this).attr("id");
                var action = $(this).data("action");
                var email_data = [];
                if(action == 'single')
                {
                    email_data.push({
                        email: $(this).data("email"),
                        name: $(this).data("name")
                    });
                }
                else
                {
                    $('.single_select').each(function(){
                        if($(this).prop("checked") == true)
                        {
                            email_data.push({
                                email: $(this).data("email"),
                                name: $(this).data('name')
                            });
                        }
                    });
                }
    
                $.ajax({
                    url:"send_mail.php",
                    method:"POST",
                    data:{email_data:email_data},
                    beforeSend:function(){
                        $('#'+id).html('Sending...');
                        $('#'+id).addClass('btn-danger');
                    },
                    success:function(data){
                        if(data == 'ok')
                        {
                            $('#'+id).text('Success');
                            $('#'+id).removeClass('btn-danger');
                            $('#'+id).removeClass('btn-info');
                            $('#'+id).addClass('btn-success');
                        }
                        else
                        {
                            $('#'+id).text(data);
                        }
                        $('#'+id).attr('disabled', false);
                    }
                })
    
            });
        });
    </script>

这是send_mail.php

<?php
//send_mail.php

if(isset($_POST['email_data']))
{
    require 'class/class.phpmailer.php';
    $output = '';
    foreach($_POST['email_data'] as $row)
    {
        $mail = new PHPMailer;
        $mail->IsSMTP();                                
        $mail->Host = 'smtp.gmail.com';     
        $mail->Port = '587';                                
        $mail->SMTPAuth = true;                         
        $mail->Username = 'xx';                 
        $mail->Password = 'xx';                 
        $mail->SMTPSecure = 'tls';                          
        $mail->From = 'xx';         
        $mail->FromName = 'xx';                 
        $mail->AddAddress($row["email"], $row["name"]); 
        $mail->WordWrap = 50;                           
        $mail->IsHTML(true);                            
        $mail->Subject = 'i want input to be passed here'; 
        //An HTML or plain text message body
        $mail->Body = 'and the body message to be passed here';

        $mail->AltBody = '';

        $result = $mail->Send();                        //Send an Email. Return true on success or false on error

//        if($result["code"] == '400')
//        {
//            $output .= html_entity_decode($result['full_error']);
//        }

    }
    if($output == '')
    {
        echo 'ok';
    }
    else
    {
        echo $output;
    }
}

?>

我假设您想为每个电子邮件数据添加主题和消息的两个输入。

您可以添加 2 个字段并在 ajax 中将其与电子邮件数据一起设置并将其传递给 send_mail.php。

JS代码

<script>
    $(document).ready(function(){
        $('.email_button').click(function(){
            $(this).attr('disabled', 'disabled');
            var id  = $(this).attr("id");
            var action = $(this).data("action");
            var email_data = [];
            if(action == 'single')
            {
                email_data.push({
                    email: $(this).data("email"),
                    name: $(this).data("name"),
                    subject: $(this).data("subject"), //or can be grab from input
                    message: $(this).data("message")
                });
            }
            else
            {
                $('.single_select').each(function(){
                    if($(this).prop("checked") == true)
                    {
                        email_data.push({
                            email: $(this).data("email"),
                            name: $(this).data('name'),
                            subject: $(this).data("subject"), //or can be grab from input
                    message: $(this).data("message")
                        });
                    }
                });
            }

            $.ajax({
                url:"send_mail.php",
                method:"POST",
                data:{email_data:email_data},
                beforeSend:function(){
                    $('#'+id).html('Sending...');
                    $('#'+id).addClass('btn-danger');
                },
                success:function(data){
                    if(data == 'ok')
                    {
                        $('#'+id).text('Success');
                        $('#'+id).removeClass('btn-danger');
                        $('#'+id).removeClass('btn-info');
                        $('#'+id).addClass('btn-success');
                    }
                    else
                    {
                        $('#'+id).text(data);
                    }
                    $('#'+id).attr('disabled', false);
                }
            })

        });
    });
</script>

在 send_mail.php 中替换以下行

$mail->Subject = 'i want input to be passed here'; 
//An HTML or plain text message body
$mail->Body = 'and the body message to be passed here';

有了这个

$mail->Subject = $row["subject"]; 
$mail->Body = $row["message"];

希望它能回答您的问题。

首先: 仅发送带有 ajax 的用户 ID(最好使用 fetch() 来处理 js 中的请求) 然后从数据库中获取文件 (send_mail.php) 中的用户数据(用户验证)!!!

如果不是通过会话,则需要通过检查数据库进行用户验证。

请参阅 composer class 以使用 phpmailer 发送电子邮件: https://github.com/MoovFun/Xo-Mail