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 Mac虚拟机安装Windows,Windows中用idea跑项目,报错process finished with exit code -1073741819(0xc0000005) 如何解决?
  • ¥15 关于python爬虫返回js数据结构的处理
  • ¥15 ubuntu所用stm32标准库开发烧录没反映
  • ¥15 运维工程师(在职人员回答)
  • ¥15 Kmeans的趋势分析有会的吗
  • ¥20 R语言单细胞测序 对seurat对象进行标准化时出现问题
  • ¥15 请问如何在matlab里使用raven工具?
  • ¥100 关于ios手游充值到一定金额,再点充值提示公众号的问题。
  • ¥15 求一个stm32串口控制程序
  • ¥20 Windows 驱动开发版本疑问相关