{"id":133850,"date":"2014-11-01T08:00:56","date_gmt":"2014-11-01T12:00:56","guid":{"rendered":"http:\/\/premium.wpmudev.org\/blog\/?p=133850"},"modified":"2022-03-17T04:55:41","modified_gmt":"2022-03-17T04:55:41","slug":"email-notifications-wordpress","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/email-notifications-wordpress\/","title":{"rendered":"How to Set Up Email Notifications in WordPress"},"content":{"rendered":"<p>Transactional emails are one-time messages sent to a specific address, triggered by a user action.<\/p>\n<p>While the word &#8220;transactional&#8221; doesn&#8217;t sound all that exciting, it&#8217;s really not all that complicated. There are many examples in WordPress: When users register with a website they get an email, and when someone leaves a comment on a post the admin might receive an email notification. Then there are email notifications for password recoveries and so on.<\/p>\n<p>In this Weekend WordPress Project\u00a0we&#8217;ll take a look at how you can use the <code>wp_mail()<\/code> function to send a simple email based on a custom user action. Specifically, we&#8217;ll take a look at how we can notify authors that their work has been published.<\/p>\n<p>Continue reading, or jump ahead using these links:<\/p>\n<ul>\n<li><a href=\"#wp-mail\">Introducing wp_mail()<\/a><\/li>\n<li><a href=\"#sending-html-emails\">Sending HTML Emails<\/a><\/li>\n<li><a href=\"#common-pitfalls\">Common Pitfalls<\/a><\/li>\n<li><a href=\"#triggering-emails-on-user-actions\">Triggering Emails On User Actions<\/a><\/li>\n<\/ul>\n<h2 id=\"wp-mail\">Introducing <code>wp_mail()<\/code><\/h2>\n<p>Many people don&#8217;t know that WordPress has an easy-to-use function for sending email messages. The <code>wp_mail<\/code> function allows you to send email flexibly by specifying many relevant parameters, let&#8217;s take a look.<\/p>\n<p><span style=\"font-weight: 400;\"><div class=\"gist\" data-gist=\"0f594d5285249e235039935eab832aea\" data-gist-file=\"gistfile 1\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/0f594d5285249e235039935eab832aea.js?file=gistfile+1\">Loading gist 0f594d5285249e235039935eab832aea<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div><\/span><\/p>\n<ul>\n<li><strong>$to<\/strong>:<br \/>\nThe email of the receiver<\/li>\n<li><strong>$subject<\/strong>:<br \/>\nThe email subject<\/li>\n<li><strong>$message<\/strong>:<br \/>\nThe email&#8217;s message<\/li>\n<li><strong>$headers<\/strong>:<br \/>\nMail headers, separated by newlines (<code>\\r\\n<\/code>)<\/li>\n<li><strong>$attachments<\/strong>:<br \/>\nA file or array of files to attach<\/li>\n<\/ul>\n<p>A very simple text email can be sent by specifying just the first three parameters, here&#8217;s a quick example:<\/p>\n<p><span style=\"font-weight: 400;\"><div class=\"gist\" data-gist=\"8770508ce9d948b627790d43c5ca7889\" data-gist-file=\"basic php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/8770508ce9d948b627790d43c5ca7889.js?file=basic+php\">Loading gist 8770508ce9d948b627790d43c5ca7889<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div><\/span><\/p>\n<h2 id=\"sending-html-emails\">Sending HTML Emails<\/h2>\n<p>HTML emails can be sent by switching the content type of the email. This can be done via the <code>wp_mail_content_type<\/code> hook. Here&#8217;s an example of the same email as above but with a little additional formatting. Of course, this goes in your functions.php file:<\/p>\n<p><span style=\"font-weight: 400;\"><div class=\"gist\" data-gist=\"02c30855d6778665b45e663a98474a21\" data-gist-file=\"sending HTML emails\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/02c30855d6778665b45e663a98474a21.js?file=sending+HTML+emails\">Loading gist 02c30855d6778665b45e663a98474a21<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div><\/span><\/p>\n<p>Note that we add the filter before sending the email and then remove it right away afterward. I&#8217;ll explain this in the next section.<\/p>\n<h2 id=\"common-pitfalls\">Common Pitfalls<\/h2>\n<p>In my experience there are two issues people often wonder about with <code>wp_mail()<\/code>. The first one is all about that HTML filter we added earlier. If you add the filter anywhere in your <code>functions.php<\/code> file or your plugin, emails will always use the HTML content type.<\/p>\n<p>Unfortunately, this doesn&#8217;t play nice with some WordPress internals and password reset emails aren&#8217;t sent out properly. Whenever you want to send an HTML email, make sure to add the filter before and remove it right after so default WordPress emails continue to function.<\/p>\n<p>The second issue arises if the email function isn&#8217;t set up properly on your server. This happens on some hosts but it is more common to see this on localhost environments like MAMP and XAMPP. The\u00a0<a href=\"https:\/\/wordpress.org\/plugins\/wp-mail-smtp\/\" target=\"_blank\">WP Mail SMTP<\/a> plugin can help you out.<\/p>\n<p>These plugins allow you to send your emails through an SMTP server, like your Gmail account for example. This has worked just fine on all the local and remote hosts I&#8217;ve tried over the years.<\/p>\n<h2 id=\"triggering-emails-on-user-actions\">Triggering Emails On User Actions<\/h2>\n<p>To send a transactional email we&#8217;ll need to look into some action. As I mentioned earlier, let&#8217;s now look at how we can notify authors when their work has been published<\/p>\n<p>The <code>publish_post<\/code> action seems like a good fit. According to the <a title=\"Plugin API\/Action Reference\/publish post\" href=\"http:\/\/codex.wordpress.org\/Plugin_API\/Action_Reference\/publish_post\" rel=\"noopener\" target=\"_blank\">WordPress Codex<\/a>:<\/p>\n<blockquote><p>publish_post is an action triggered whenever a post is published, or if it is edited and the status is changed to publish.<\/p><\/blockquote>\n<p>Here&#8217;s all the code put together:<\/p>\n<p><span style=\"font-weight: 400;\"><div class=\"gist\" data-gist=\"b004259d9601d10831bbe5bea2bb2480\" data-gist-file=\"gistfile 1\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/b004259d9601d10831bbe5bea2bb2480.js?file=gistfile+1\">Loading gist b004259d9601d10831bbe5bea2bb2480<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div><\/span><\/p>\n<p>There shouldn&#8217;t be a whole lot of new code here. Let me explain what it does: I&#8217;ve defined the function which sets the content type and I&#8217;ve hooked a function into the <code>publish_post<\/code> action. When this action is triggered, I check that this is indeed a post (and not a page or some other custom post type). If it is a post, I go ahead and send the email, that&#8217;s all.<\/p>\n<h2>Final Thoughts<\/h2>\n<p>There is a whole lot more you can do here. From basic welcome emails to user notifications when a new post is published in a specific category, the options are endless.<\/p>\n<p>One thing to keep in mind is that your server is most probably not made to send hundreds upon hundreds of emails. If you want to send out bulk emails you should talk with your host and perhaps look into tying in the <a href=\"http:\/\/mailchimp.com\/\" target=\"_blank\">Mailchimp<\/a> or <a href=\"http:\/\/madmimi.com\" target=\"_blank\">Madmimi<\/a> API, otherwise you may get blocked.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Transactional emails are one-time messages sent to a specific address, triggered by a user action. While the word &#8220;transactional&#8221; doesn&#8217;t sound all that exciting, it&#8217;s really not all that complicated. There are many examples in WordPress: When users register with a website they get an email, and when someone leaves a comment on a post [&hellip;]<\/p>\n","protected":false},"author":344049,"featured_media":207209,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[263],"tags":[9798],"tutorials_categories":[],"class_list":["post-133850","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-weekend-wordpress-projects"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/133850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/344049"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=133850"}],"version-history":[{"count":6,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/133850\/revisions"}],"predecessor-version":[{"id":209985,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/133850\/revisions\/209985"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/207209"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=133850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=133850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=133850"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=133850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}