如何在php中安排电子邮件通知 [英] How to schedule email notifications in php

查看:68
本文介绍了如何在php中安排电子邮件通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用PHPmail发送电子邮件.但是,如果我需要发送大量电子邮件怎么办?

I know how to send email using PHPmail. But what if I need to send lots of emails?

这可能是在发布新消息时向站点订阅者的通知".直接在页面处理程序中执行此操作会严重影响性能,并使浏览器加载速度太慢.所以我需要在后台"完成这项工作.

This could be a "notification" to site subscribers when a new message is posted. Doing that directly in page handler will seriously affect performance and make browser loading too slow. So I need to have this job done in the "background".

在我的情况下,如何在PHP中安排"某些时间-发送电子邮件?例如,在C ++中,我将创建一个单独的线程,但是AFAIK没有PHP中的多线程之类的东西.

How can I "schedule" something in PHP, in my case - sending emails? In C++ for example I would create a separate thread but AFAIK there is no such thing as multithreading in PHP.

推荐答案

创建CRON作业以发送您已保存在数据库中的电子邮件队列:

Create CRON job that sends out email-queue you've saved in a database:

  1. 将包含所有信息的电子邮件保存到数据库中.

  1. Save an email with all info into a db.

使用CRON作业定期(如每半小时左右-取决于您的托管服务提供商和您要发送的电子邮件数量)从队列中提取电子邮件并将其发送出去.然后在数据库中的电子邮件上设置一个 sent 标志,添加所需的发送信息(例如时间,错误,标头等).

Use a CRON job to periodically (like every half hour or so - depends on your hosting provider and the number of emails you're sending out) grab emails from the queue and send them out. Then set a sent flag on the email in the database, add sending info you want (e.g. time, errors, header, ...).

请确保将电子邮件分解成几块,并暂停发送,以避免某些电子邮件服务器的反垃圾邮件和防洪保护问题.一些邮件程序库为此提供了插件(例如 SwiftMailer ).

Be sure to break up the emails to chunks and send out them with pauses to avoid problems with anti-spam and anti-flood protections of some email servers. Some mailer libraries have plugins for this (like SwiftMailer).

这篇关于如何在php中安排电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆