Sending email with PHP mail()

拥有回忆 提交于 2019-12-12 09:20:58

问题


I'm trying to send automated emails with mail(). It sends some emails but not all, around 50%. To test I'm using the same email address for all emails, and still only some get delivered.

I'm using localhost XAMPP.

Here's the code:

if ($_POST['sendEmail'] == "SEND Email") {

    ob_start();

    $buffer = str_repeat(" ", 4096);
    $buffer. = "\r\n some HTML \r\n";

    set_time_limit(0);
    $noEmails = $last - $first + 1;
    echo "Emails sent (of $noEmails):";
    for ($index = $first; $index <= $last; $index++) {
        $to = $email["$index"];
        $subject = "Hey {$firstName["
        $index "]}!";
        $message = "$emailMessage";
        $headers = 'From: my_account@gmail.com'."\r\n".'Reply-To: my_account@gmail.com'."\r\n".'X-Mailer: PHP/'.phpversion();
        sleep(1);
        mail($to, $subject, $message, $headers);
        echo $buffer.$index;
        ob_flush();
        flush();
    }
    ob_end_flush();
}​

Please give your suggestions.


回答1:


For sending bulk emails you can set a cron job which may run after 5 or 10 minutes that will send only a small quantity of emails at a time. Create a reference in a table that so you can have record of which addresses have been emailed. The whole bulk will be divided into little chunks and as it will be running in background it will not put a huge load on your SMTP server. You should use PHP Mailer or SwiftMailer libraries.

You can have a look at this question to select which php mailing library is best:

PhpMailer vs. Swiftmailer?

Hope this helps.



来源:https://stackoverflow.com/questions/10117872/sending-email-with-php-mail

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!