dplo59755 2017-05-20 05:07
浏览 56
已采纳

PhpMailer用于多个电子邮件地址

I'm new to phpmailer and I'm trying to send an email to a group of users. I've searched on Google and used also the phpmailer reference guide and this is what i've created so far.

$oggetto_email = 'Kondo Manager: nuova comunicazione condominio';
            // Creo il link per il login
            $link_login = BASE_URL . '/index.php';
            // Ottengo il tema html per il layout della email
            $message = file_get_contents(BASE_URL .'/email-templates/client-new-group-post.html');
            // Passo la variabile link a %linkripristino% che poi inserisco nel tema 
            $message = str_replace('%linklogin%', $link_login, $message);
            $message = str_replace('%titolo%', $group_message_subject, $message);

            $user_emails = mysqli_prepare($conn, "SELECT user_email FROM user_group_join LEFT OUTER JOIN users ON user_group_join . user_join_id = users . user_id WHERE group_join_id = ?");
            mysqli_stmt_bind_param($user_emails, 'i', $group_id);
            mysqli_stmt_execute($user_emails);
            mysqli_stmt_store_result($user_emails);
            mysqli_stmt_bind_result($user_emails, $tot_user_emails);
            $tot_users_emails = mysqli_stmt_num_rows($user_emails);
            // Inserisco le emails dentro ad un array
            $emails = array();

            if($tot_users_emails > 0){

                while(mysqli_stmt_fetch($user_emails)){

                    $emails[] = $tot_user_emails;

                }
            }

            foreach ($emails as $email) { //This iterator syntax only works in PHP 5.4+
                // uso la funzione smtpmailer per inviare la mail passando i parametri richiesti
                $send_mail = smtpmailer($email, $oggetto_email, $message);
                // controllo la presenza di possibil errori nell'invio
                        if($send_mail === 'fail') {

                            echo "manage errors here, to be developed";

                        }

            }

It seems to work fine but i've only tested with a group of three emails, i'm wondering if this is the right approach and if it will be still fine with a big group of emails 50-100. I believe it will take some time for the browser to load the page causing some issues to the final user. I read online I can put the emails in a queue and then send them later in background. I'm aware it can be done using RabbitMq but I'm thinking if I can use a mysql approach saving the messages into the database and then somehow running a background process to get all the emails from the database and process them using stmp phpmailer. If is it possible how can it be done? Also considering that the email will be the same for all the group's members do you think using addBcc will help to reduce the page loading time? Many thanks for your help

  • 写回答

1条回答 默认 最新

  • dpzbzp8728 2017-05-20 06:01
    关注

    According to the "mailing list" example from these PHPMailer examples, your general approach seems to be similar to the one given in the example. If you think sending out 50-100 emails in this process will be unacceptably slow for the user—at least for the user interface (UI) response time after the sending process has started, the following topics are a few possibilities for approaching your situation.

    Background Scripts

    Like you mentioned, it would be possible to store your "to-be-sent" mailing data into a database table. This gives you the option to schedule a cron job to run a different script to actually mail out these "to-be-sent" emails without getting in the way of the original user's experience.

    The downside here, however, might have to do with the added complexity of letting the user know if a mailing has been successful or not.

    Threads

    Though PHP might not be the easiest language in which to use threads, it might be time to look into working with threads in order to free up your UI when a time-intensive task like this needs to be performed.

    Basically, you would likely want to set up a thread for sending messages. This way, when a user goes to send a bunch of mailings, the actual sending task would be running in a thread apart from your UI, so the user would not necessarily have to wait for the task to complete in order to move on in your app. This article might get you started with threads in PHP, should you decide to try this route.

    Message Queues

    Like you also mentioned, a message queue could be used to send a task for later actions to be performed, but if your general use case does not go beyond something like 50-100 emails at a time, using a message queue might be more complicated than necessary for your particular situation.


    However, these approaches are more theoretical and may be more helpful in other contexts.

    For this type of mailing in PHP, you may want to try looking at mail queues such as emailqueue or Mail_Queue (as mentioned in this post), though I don't know if these tools are still up to date.

    Finally, in response to your question about Bcc, some people seem to think that using Bcc might send mail in a faster way, but you might want to try using Bcc versus not using Bcc in order to test that out for yourself.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵