Skip to content

Instantly share code, notes, and snippets.

@paulgibbs
Created March 19, 2018 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulgibbs/a33e1905a60eb1dc5b66421d59cebedb to your computer and use it in GitHub Desktop.
Save paulgibbs/a33e1905a60eb1dc5b66421d59cebedb to your computer and use it in GitHub Desktop.
* Use a Post Type for email templates.
* The intention is to use the standard wp-admin interface to allow people to change the contents of the email.
* Mustache-like token syntax.
* Use the WP Customiser to visually customise those emails.
* Use a Taxonomy to store the distinct types of emails we send (e.g. new_user, new_site, new_activity_mention, etc).
* Add a HTML email template for the post type.
Direct calls to `wp_mail()` replaced with custom function `bp_send_mail()` that, basically, wraps `wp_mail`:
* I surveyed all the mail replacement plugins I could find (like Mandrill's), and they all re-declare `wp_mail` to implement support for their custom service. Therefore, if `wp_mail()` is redeclared, *or* something has been filtered to get WP to send HTML emails, I'm assuming there's some dark voodoo at work -- and for reasons of trying to be compatible as possible, BP will treat emails the same way it does today (we'll pass it straight through to whatever `wp_mail()` is).
* `bp_send_mail` does three things; 1) the compatibility checks as described above, 2) creates a `BP_Email` object representing the email to be sent (the content, addresses, etc), 3) and sends the email.
* Sending is achieved by looking for a class that represents some kind of email delivery service.
* The idea is to split the message object away from the delivery implementation.
* Mandrill, for example, could create a new BP class in their plugin, filter something in BuddyPress, and they'd receive an email object, and it would be up to them to format it and send it to their delivery API -- but they wouldn't be stomping all over BP core's email implementation to do so.
Internals:
* Class `BP_Email` represents an email; the bits you see in your email client like the subject, message, recipients. It also encapsulates custom email headers and tokens.
* Tokens help personalise the content of an email. Analogous to MailChimp’s merge tags.
* Class `BP_Email_Recipient` represents an email recipient. It’s a simple class, and holds an email address and recipient name. If the class is instantiated with a WordPress user ID, then it also has a `WP_User` reference.
* Interface `BP_Email_Delivery` mandates a `bp_email()` function, which mail delivery services will use to implement BuddyPress support.
* Class `BP_PHPMailer` implements that interface for PHPMailer.
* Function `bp_send_email()` is the function we use to send emails.
Links to those internals:
https://github.com/buddypress/BuddyPress/blob/e1756249b3ab54c4ad0a217368f79bc378558135/src/bp-core/classes/class-bp-email-delivery.php
https://github.com/buddypress/BuddyPress/blob/e1756249b3ab54c4ad0a217368f79bc378558135/src/bp-core/classes/class-bp-email-recipient.php
https://github.com/buddypress/BuddyPress/blob/e1756249b3ab54c4ad0a217368f79bc378558135/src/bp-core/classes/class-bp-email.php
https://github.com/buddypress/BuddyPress/blob/e1756249b3ab54c4ad0a217368f79bc378558135/src/bp-core/bp-core-functions.php#L3087
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment