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 office打开卡退(新电脑重装office系统后)
  • ¥300 FLUENT 火箭发动机燃烧EDC仿真
  • ¥15 【Hadoop 问题】Hadoop编译所遇问题hadoop-common: make failed with error code 2
  • ¥15 vb6.0+webbrowser无法加载某个网页求解
  • ¥15 RPA财务机器人采购付款流程
  • ¥15 计算机图形多边形及三次样条曲线绘制
  • ¥15 根据protues画的图用keil写程序
  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误
  • ¥20 cesm的气溶胶排放文件