├── .gitignore ├── CHANGELOG.md ├── LICENSE.txt ├── README.md ├── assets ├── css │ └── admin.css ├── images │ └── logo.png └── js │ └── admin.js ├── includes └── class-postmark-debug.php ├── page-settings.php ├── postmark-settings.php ├── postmark.php ├── readme.txt └── wp-mail.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 5 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 6 | 7 | ## [1.20.0] 8 | - Convert newlines to line breaks when forcing HTML 9 | 10 | ## [1.19.1] 11 | - Fix warning when logging sends with a null To address 12 | 13 | ## [1.19.0] 14 | - Support adding metadata to messages 15 | 16 | ## [1.18.1] 17 | - Update Status tab to use new Postmark API. 18 | - Tested up to WordPress 6.2. 19 | 20 | ## [1.18.0] 21 | - Add support for tags. 22 | 23 | ## [1.17.2] 24 | - Fix Site Health constants usage. 25 | 26 | ## [1.17.1] 27 | - Fix Site Health when some settings not set. 28 | 29 | ## [1.17.0] 30 | - Add ActiveCampaign Postmark for WordPress to Site Health. 31 | 32 | ## [1.16.1] 33 | - Obfuscate API Token in Settings page. 34 | 35 | ## [1.16.0] 36 | - Added a Status tab to the plugin's Settings page for quickly checking [Postmark's Status](https://status.postmarkapp.com/). 37 | 38 | ## [1.15.7] 39 | 40 | - ActiveCampaign branding update. 41 | 42 | ## [1.15.6] 43 | 44 | - Do wp_mail_succeeded action after successful sends (introduced in WordPress v5.9). 45 | 46 | ## [1.15.5] 47 | 48 | - Honour pre_wp_mail filters. 49 | 50 | ## [1.15.4] 51 | 52 | - Fix notice when overriding force_from setting. 53 | 54 | ## [1.15.3] 55 | 56 | - Ensure Postmark plugin is loaded before attempting to load settings from it after upgrading. 57 | 58 | ## [1.15.2] 59 | 60 | - Use correct admin.js file version. 61 | 62 | ## [1.15.1] 63 | 64 | - Bugfix for using Force From setting. 65 | 66 | ## [1.15.0] 67 | 68 | - Adds new Force From setting to allow preventing override of From address using headers, if desired. 69 | 70 | ## [1.14.0] 71 | 72 | - Adds ability to override settings for environment specific Postmark plugin settings. 73 | 74 | ## [1.13.7] 75 | 76 | - Fix limit of 500 sending logs deleted per day. 77 | 78 | ## [1.13.6] 79 | 80 | - Even better handling of apostrophes in email address From names. 81 | 82 | ## [1.13.5] 83 | 84 | - Handle apostrophes in email address From names. These are sometimes used in site titles, which can be the default From address name with other plugins. 85 | 86 | ## [1.13.4] 87 | 88 | - Handle special characters in site titles for test emails. 89 | 90 | ## [1.13.3] 91 | 92 | - Additional bugfix for using wp_mail_from_name filter. 93 | 94 | ## [1.13.2] 95 | 96 | - Fixes error when upgrading by ensuring $postmark is set before trying to load settings. 97 | 98 | ## [1.13.1] 99 | 100 | - Fixes error from using incorrect filter name and mailparse_rfc822_parse_addresses function. 101 | 102 | ## [1.13.0] 103 | 104 | - Adds support for using the wp_mail_from_name filter to specify a from_name when sending. 105 | 106 | ## [1.12.5] 107 | 108 | - Fixes 'POSTMARK_DIR is undefined' warning when upgrading other plugins via the CLI. 109 | 110 | ## [1.12.4] - 2020-05-11 111 | 112 | - Fixes potential collation mismatch errors from date comparisons during old sending logs deletion. 113 | 114 | ## [1.12.3] 115 | 116 | - Uses count() for check of logs query result count. 117 | 118 | ## [1.12.2] 119 | 120 | - Corrects SQL for deletion of log entries older than 7 days. 121 | 122 | ## [1.12.1] 123 | 124 | - Checks if stream_name is set in settings before determining which stream to use. 125 | 126 | ## [1.12] 127 | 128 | - Adds support for message streams. 129 | 130 | ## [1.11.6] 131 | 132 | - Updates server API token location hint in plugin settings. 133 | 134 | ## [1.11.5] 135 | 136 | - Allows using POSTMARK_API_TEST in the plugin settings for generating test send requests that aren't actually delivered. 137 | 138 | ## [1.11.4] 139 | 140 | - Fixes handling of situation where call to Postmark API results in WP_Error instead of array for response, such as during incidents of the API being offline and not returning a response. 141 | 142 | ## [1.11.3] 143 | 144 | - Fixes log page display of From/To addresses including the From/To names. Only email addresses will now appear in logs page, to avoid confusion, while also preserving the sanitation of email addresses before inserting into db. 145 | 146 | ## [1.11.2] - 2019-02-05 147 | 148 | - Fixes no index error with track links check in wp_mail. 149 | 150 | ## [1.11.1] 151 | 152 | - Fixes call of non-global load_settings function during upgrade. 153 | 154 | ## [1.11.0] - 2018-12-13 155 | 156 | - Adds link tracking support. 157 | - Fixes send test with HTML/open tracking option not being honored in sent test email. 158 | 159 | ## [1.10.6] 160 | 161 | - Fixes undefined index error. 162 | - Adds Upgrade Notice 163 | 164 | ## [1.10.5] 165 | 166 | - Corrects logs deletion cron job unscheduling issue. 167 | 168 | ## [1.10.4] 169 | 170 | - Removes index on logs table. 171 | 172 | ## [1.10.3] 173 | 174 | - Corrects version mismatch in constructor. 175 | 176 | ## [1.10.1] 177 | 178 | - Adds a new logging feature that can be enabled to store logs for send attempts. Logs include Date, From address, To address, Subject, and Postmark API response. Logs are displayed in a Logs tab in the plugin setting once enabled. 179 | - Switch loading of JS/CSS to use enqueue() 180 | 181 | ## [1.9.6] 182 | 183 | - Resolves issue when saving settings in UI. 184 | - Falls attachment Content-Type back to 'application/octet-stream' when other methods fail. 185 | 186 | ## [1.9.5] 187 | 188 | - Update javascript to fix settings update issue. 189 | 190 | ## [1.9.4] 191 | 192 | - Added `postmark_error` and `postmark_response` actions to the plugin, to intercept API results after calling wp_mail. You can register callbacks for these using `add_action` (more info here: https://developer.wordpress.org/reference/functions/add_action/) 193 | 194 | ## [1.9.3] 195 | 196 | - Interface cleanup 197 | - Minor code restructuring 198 | 199 | ## [1.9.2] 200 | 201 | - Make the errors available in the PHP variable `Postmark_Mail::$LAST_ERROR` if `wp_mail()` returns false, examine this variable to find out what happened. 202 | - When doing a test send, if an error occurs, it will be printed in the settings page header. 203 | 204 | ## [1.9.1] 205 | 206 | - Fix case where 'From' header is specified as a literal string, instead of in an associative array. 207 | 208 | ## [1.9] - 2016-04-01 209 | 210 | - Allow the 'From' header to override the default sender. 211 | - Don't send TextBody when the user has specified the 'Content-Type' header of 'text/html' 212 | - Allow individual messages to opt-in to Track-Opens by including a header of 'X-PM-Track-Opens' and a value of `true` 213 | 214 | ## [1.8] 215 | 216 | - Modernization of codebase. 217 | 218 | ## [1.7] 219 | 220 | - Support headers for cc, bcc, and reply-to 221 | 222 | ## [1.6] 223 | 224 | - Added open tracking support. 225 | 226 | ## [1.5] 227 | 228 | - Fix issue with new WordPress HTTP API Integration. 229 | 230 | ## [1.4] 231 | 232 | - New option to force emails to be sent as HTML. Previously just detected Content-Type. 233 | - Now uses the WordPress HTTP API. 234 | 235 | ## [1.3] 236 | 237 | - Resolved error with handing arrays of recipients 238 | 239 | ## [1.2] 240 | 241 | - Arrays of recipients are now properly handled 242 | - HTML emails and Text Emails are now handled by checking the headers of the emails sent, and sends via Postmark appropriately. 243 | - Optional "Powered by Postmark" footer of sent emails. "Postmark solves your WordPress email problems. Send transactional email confidently using https://postmarkapp.com" 244 | - Add license to README and PHP file 245 | 246 | ## [1.0.0] 247 | 248 | - First Public release. 249 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | 7 | Everyone is permitted to copy and distribute verbatim copies 8 | of this license document, but changing it is not allowed. 9 | 10 | Preamble 11 | 12 | The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. 13 | 14 | When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. 15 | 16 | To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. 17 | 18 | For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. 19 | 20 | We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. 21 | 22 | Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. 23 | 24 | Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. 25 | 26 | The precise terms and conditions for copying, distribution and modification follow. 27 | 28 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 29 | 30 | 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". 31 | 32 | Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 33 | 34 | 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. 35 | 36 | You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 37 | 38 | 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: 39 | 40 | a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. 41 | 42 | b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. 43 | 44 | c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) 45 | These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. 46 | 47 | Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. 48 | 49 | In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 50 | 51 | 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: 52 | 53 | a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, 54 | 55 | b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, 56 | 57 | c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) 58 | The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. 59 | 60 | If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 61 | 62 | 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 63 | 64 | 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 65 | 66 | 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 67 | 68 | 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. 69 | 70 | If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. 71 | 72 | It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. 73 | 74 | This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 75 | 76 | 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 77 | 78 | 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. 79 | 80 | Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 81 | 82 | 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. 83 | 84 | NO WARRANTY 85 | 86 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 87 | 88 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 89 | 90 | END OF TERMS AND CONDITIONS -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | Postmark Logo 3 | 4 | 5 | # ActiveCampaign Postmark for WordPress 6 | 7 | If you’re still sending email with default SMTP, you’re blind to delivery problems! ActiveCampaign Postmark for WordPress enables sites of any size to deliver and track WordPress notification emails reliably, with minimal setup time and zero maintenance. 8 | 9 | If you don’t already have an ActiveCampaign Postmark account, you can get one in minutes, sign up at https://postmarkapp.com. 10 | 11 | ## Installation 12 | 13 | 1. Upload postmark directory to your /wp-content/plugins directory or install from plugin marketplace 14 | 2. Activate plugin in WordPress admin 15 | 3. In WordPress admin, go to **Settings** then **ActiveCampaign Postmark**. You will then want to insert your ActiveCampaign Postmark details. If you don’t already have an ActiveCampaign Postmark account, get one at https://postmarkapp.com. 16 | 4. Verify sending by entering a recipient email address you have access to and pressing the “Send Test Email” button. Enable logging for troubleshooting and to check the send result. 17 | 5. Once everything is verified as working, check **Send emails using ActiveCampaign Postmark** and Save, to override `wp_mail` to send using the ActiveCampaign Postmark API instead. 18 | 19 | ## FAQ 20 | 21 | ### What is ActiveCampaign Postmark? 22 | ActiveCampaign Postmark is a hosted service that expertly handles all delivery of transactional webapp and web site email. This includes welcome emails, password resets, comment notifications, and more. If you've ever installed WordPress and had issues with PHP's `mail` function not working right, or your WordPress install sends comment notifications or password resets to spam, ActiveCampaign Postmark makes all of these problems vanish in seconds. Without ActiveCampaign Postmark, you may not even know you're having delivery problems. Find out in seconds by installing and configuring this plugin. 23 | 24 | ### Will this plugin work with my WordPress site? 25 | 26 | The ActiveCampaign Postmark for WordPress plugin overrides any usage of the `wp_mail` function. Because of this, if any 3rd party code or plugins send mail directly using the PHP mail function, or any other method, we cannot override it. Please contact the makers of any offending plugins and let them know that they should use `wp_mail` instead of unsupported mailing functions. 27 | 28 | ### TLS Version Requirements/Compatibility 29 | 30 | The ActiveCampaign Postmark API requires TLS v1.1 or v1.2 support. We recommend using TLS v1.2. 31 | 32 | You can check your TLS v1.2 compatibility using [this plugin](https://wordpress.org/plugins/tls-1-2-compatibility-test/). After installing the plugin, change the dropdown for 'Select API Endpoint' to _How's My SSL?_ and run the test. If compatibility with TLS v1.2 is not detected, contact your server host or make the necessary upgrades to support TLS v1.1 or v1.2. 33 | 34 | TLS 1.2 requires: 35 | 36 | - PHP 5.5.19 or higher 37 | - cURL 7.34.0 or higher 38 | - OpenSSL 1.0.1 or higher 39 | 40 | ### Does this cost me money? 41 | 42 | The ActiveCampaign Postmark service (and this plugin) are free to get started, for up to 100 emails a month. You can sign up at https://postmarkapp.com/. When you need to process more than 100 emails a month, ActiveCampaign Postmark offers monthly plans to fit your needs. 43 | 44 | ### My emails are still not sending, or they are going to spam! HELP!? 45 | 46 | First, enable logging in **Settings** and check your send attempts for any errors returned by the ActiveCampaign Postmark API. These errors can let you know why the send attempts are failing. If you aren't seeing log entries for your send attempts, then the plugin or contact form generating the emails is likely not using `wp_mail` and not compatible with this plugin. 47 | 48 | If you are still unsure how to proceed, just send an email to [support@postmarkapp.com](mailto:support@postmarkapp.com) or tweet [@postmarkapp](https://twitter.com/postmarkapp) for help. Be sure to include as much detail as possible. 49 | 50 | ### Why should I trust you with my email sending? 51 | 52 | Because we've been in this business for many years. We’ve been running an email marketing service, Newsberry, for five years. Through trial and error we already know what it takes to manage a large volume of email. We handle things like whitelisting, ISP throttling, reverse DNS, feedback loops, content scanning, and delivery monitoring to ensure your emails get to the inbox. 53 | 54 | Most importantly, a great product requires great support and even better education. Our team is spread out across six time zones to offer fast support on everything from using ActiveCampaign Postmark to best practices on content and user engagement. A solid infrastructure only goes so far, that’s why improving our customer’s sending practices helps achieve incredible results. 55 | 56 | ### How do I tag a message? 57 | 58 | There are two ways to tag a message. 59 | 60 | 1. Set an `X-PM-Tag` message header, i.e. `array_push( $headers, 'X-PM-Tag: PostmarkPluginTest' );` where you are calling `wp_mail()`. 61 | 62 | 2. Add a filter for `postmark_tag` that hooks into a function that returns the tag you desire. 63 | 64 | Using the `postmark_tag` filter option will override a tag set via message headers. 65 | 66 | ### Why aren't my HTML emails being sent? 67 | 68 | This plugin detects HTML by checking the headers sent by other WordPress plugins. If a "text/html" content type isn't set then this plugin won't send the HTML to ActiveCampaign Postmark to be sent out only the plain text version of the email. 69 | 70 | ### Why are password reset links not showing in emails sent with this plugin? 71 | 72 | There are a couple ways to resolve this issue. 73 | 74 | 1. Open the ActiveCampaign Postmark plugin settings and uncheck Force HTML and click Save Changes. If the default WordPress password reset email is sent in Plain Text format, the link will render as expected. 75 | 76 | 2. Access your WordPress site directory and open the `wp-login.php` file. 77 | 78 | Change this line: 79 | 80 | $message .= ‘<‘ . network_site_url(“wp-login.php?action=rp&key=$key&login=” . rawurlencode($user_login), ‘login’) . “>\r\n”; 81 | 82 | Remove the brackets, so it becomes: 83 | 84 | $message .= network_site_url(“wp-login.php?action=rp&key=$key&login=” . rawurlencode($user_login), ‘login’) . “\r\n”; 85 | 86 | And save the changes to the file. 87 | 88 | ### How do I set the from name? 89 | 90 | The plugin supports using the `wp_mail_from_name` filter for manually setting a name in the From header. 91 | 92 | ## Additional Resources 93 | 94 | [ActiveCampaign Postmark for WordPress FAQ](https://postmarkapp.com/support/article/1138-postmark-for-wordpress-faq) 95 | 96 | [Can I use the ActiveCampaign Postmark for WordPress plugin with Gravity Forms?](https://postmarkapp.com/support/article/1129-can-i-use-the-postmark-for-wordpress-plugin-with-gravity-forms) 97 | 98 | [How do I send with Ninja Forms and ActiveCampaign Postmark for WordPress?](https://postmarkapp.com/support/article/1047-how-do-i-send-with-ninja-forms-and-postmark-for-wordpress) 99 | 100 | [How do I send with Contact Form 7 and ActiveCampaign Postmark for WordPress?](https://postmarkapp.com/support/article/1072-how-do-i-send-with-contact-form-7-and-postmark-for-wordpress) 101 | 102 | [Can I use the ActiveCampaign Postmark for WordPress plugin with Divi contact forms?](https://postmarkapp.com/support/article/1128-can-i-use-the-postmark-for-wordpress-plugin-with-divi-contact-forms) 103 | 104 | ## Changelog 105 | ## [1.19.1] 106 | - Fix warning when logging sends with a null To address 107 | 108 | -------- 109 | 110 | [See the previous changelogs here](https://github.com/ActiveCampaign/postmark-wordpress/blob/master/CHANGELOG.md) 111 | 112 | ActiveCampaign, 2022 113 | -------------------------------------------------------------------------------- /assets/css/admin.css: -------------------------------------------------------------------------------- 1 | .logo-bar { 2 | background-color: #ffde00; 3 | border-radius: 5px; 4 | margin-bottom: 5px; 5 | padding: 10px; 6 | line-height: 1; 7 | } 8 | 9 | .logo-bar a { 10 | display: inline-block; 11 | } 12 | 13 | .footnote { 14 | font-size: 13px; 15 | margin-top: 5px; 16 | } 17 | 18 | .postmark-input { 19 | width: 300px; 20 | } 21 | 22 | .nav-tab { 23 | cursor: pointer; 24 | } 25 | 26 | .tab-content { 27 | display: none; 28 | } 29 | 30 | .tab-content.active { 31 | display: block; 32 | } 33 | 34 | .footnote { 35 | font-size: 13px; 36 | } 37 | 38 | #wpbody-content > div.wrap > div.tab-content.tab-log.active > table.pm-log { 39 | margin-bottom: 0; 40 | margin-top: 3%; 41 | overflow: hidden; 42 | overflow-y: scroll; 43 | display: block; 44 | border-collapse: collapse; 45 | } 46 | 47 | #wpbody-content > div.wrap > div.tab-content.tab-log.active > table.pm-log > tbody > tr:nth-child(odd) { 48 | background-color: white; 49 | } 50 | 51 | #wpbody-content > div.wrap > div.tab-content.tab-log.active > table.pm-log > tbody > tr > td { 52 | border-bottom: 1px solid #ddd; 53 | padding-bottom: 0.5em; 54 | padding-top: 0.5em; 55 | padding-left: 0.5em 56 | } 57 | 58 | #wpbody-content > div.wrap > div.tab-content.tab-log.active > table > thead > tr > th { 59 | font-size: 1.2em; 60 | padding-bottom: 0.7em; 61 | } 62 | 63 | .load-more { 64 | display: flex; 65 | justify-content: center; 66 | } 67 | -------------------------------------------------------------------------------- /assets/images/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ActiveCampaign/postmark-wordpress/40ecc957252bf79ce9b73e3a58348ab056c51dec/assets/images/logo.png -------------------------------------------------------------------------------- /assets/js/admin.js: -------------------------------------------------------------------------------- 1 | (function ($) { 2 | $(function() { 3 | var settings = postmark.settings; 4 | var logs_offset = 10; 5 | $('.pm-enabled').prop('checked', settings.enabled); 6 | $('.pm-api-key').val(settings.api_key); 7 | $('.pm-stream-name').val(settings.stream_name); 8 | $('.pm-sender-address').val(settings.sender_address); 9 | $('.pm-force-from').prop('checked', settings.force_from); 10 | $('.pm-force-html').prop('checked', settings.force_html); 11 | $('.pm-track-opens').prop('checked', settings.track_opens); 12 | $('.pm-track-links').prop('checked', settings.track_links); 13 | $('.pm-enable-logs').prop('checked', settings.enable_logs); 14 | 15 | // save 16 | $(document).on('click', '.save-settings', function() { 17 | var data = { 18 | 'enabled': $('.pm-enabled').is(':checked') ? 1 : 0, 19 | 'api_key': $('.pm-api-key').val(), 20 | 'stream_name': $('.pm-stream-name').val() ? $('.pm-stream-name').val() : 'outbound', 21 | 'sender_address': $('.pm-sender-address').val(), 22 | 'force_from': $('.pm-force-from').is(':checked') ? 1 : 0, 23 | 'force_html': $('.pm-force-html').is(':checked') ? 1 : 0, 24 | 'track_opens': $('.pm-track-opens').is(':checked') ? 1 : 0, 25 | 'track_links': $('.pm-track-links').is(':checked') ? 1 : 0, 26 | 'enable_logs': $('.pm-enable-logs').is(':checked') ? 1 : 0 27 | }; 28 | 29 | $.post(ajaxurl, { 30 | '_wpnonce' : $('#_wpnonce').val(), 31 | 'action': 'postmark_save', 32 | 'data': JSON.stringify(data) 33 | }, function(response) { 34 | $('.pm-notice').html('

' + response + '

'); 35 | $('.pm-notice').removeClass('hidden'); 36 | 37 | // Immediately hides logs tab if logs were disabled. 38 | if (data.enable_logs == 1) { 39 | $('#pm-log-nav-tab').show(); 40 | } else { 41 | $('#pm-log-nav-tab').hide(); 42 | } 43 | }); 44 | }); 45 | 46 | // send test email 47 | $(document).on('click', '.send-test', function() { 48 | $.post(ajaxurl, { 49 | '_wpnonce' : $('#_wpnonce').val(), 50 | 'action': 'postmark_test', 51 | 'email': $('.pm-test-email').val(), 52 | 'with_tracking_and_html': $('.pm-test-with-opens').is(':checked') ? 1 : 0, 53 | 'override_from_address' : $('.pm-test-email-sender').val() 54 | }, function(response) { 55 | $('.pm-notice').html('

' + response + '

'); 56 | $('.pm-notice').removeClass('hidden'); 57 | }); 58 | }); 59 | 60 | // tab handler 61 | $(document).on('click', '.nav-tab', function() { 62 | var which = $(this).attr('rel'); 63 | $('.nav-tab').removeClass('nav-tab-active'); 64 | $('.tab-content').removeClass('active'); 65 | $(this).addClass('nav-tab-active'); 66 | $('.tab-' + which).addClass('active'); 67 | }); 68 | 69 | // Loads more logs when 'Load More' button is clicked. 70 | $(document).on('click', '.load-more', function() { 71 | 72 | var data = { 73 | action: 'postmark_load_more_logs', 74 | offset: logs_offset, 75 | _wpnonce : $('#_wpnonce').val() 76 | }; 77 | 78 | // Prepares for next 'Load More' by increasing the offset amount for next query. 79 | logs_offset += 10; 80 | 81 | $.post( ajaxurl, data, function( response ) { 82 | 83 | // Parses response string into JSON. 84 | response = JSON.parse(response); 85 | 86 | // Adds new logs to logs table. 87 | $('#pm-log-table tr:last').after(response.html); 88 | 89 | // Tracks the number of total logs present in logs table. 90 | var has_more = response.has_more; 91 | 92 | // Hides the 'Load More' button if no additional logs to display. 93 | if (!has_more) { 94 | $('.load-more').hide(); 95 | } 96 | }); 97 | }); 98 | 99 | // force html if track opens is enabled 100 | $(document).on('click', '.pm-track-opens', function() { 101 | if ($(this).is(':checked')) { 102 | $('.pm-force-html').attr('checked', 'checked'); 103 | } 104 | }); 105 | 106 | // uncheck track opens if force html is disabled 107 | $(document).on('click', '.pm-force-html', function() { 108 | if (! $(this).is(':checked')) { 109 | $('.pm-track-opens').removeAttr('checked'); 110 | } 111 | }); 112 | 113 | // trigger active tab 114 | $('.nav-tab:first').click(); 115 | }); 116 | })(jQuery); -------------------------------------------------------------------------------- /includes/class-postmark-debug.php: -------------------------------------------------------------------------------- 1 | __( 'Active Campaign Postmark', 'postmark-wordpress' ), 28 | 'fields' => array( 29 | 'status' => array( 30 | 'label' => __( 'Enabled', 'postmark-wordpress' ), 31 | 'value' => $this->strbool( $settings->enabled ), 32 | 'private' => false, 33 | ), 34 | 'version' => array( 35 | 'label' => __( 'POSTMARK_VERSION', 'postmark-wordpress' ), 36 | 'value' => defined( 'POSTMARK_VERSION' ) ? POSTMARK_VERSION : $postmark::$POSTMARK_VERSION, 37 | 'private' => false, 38 | ), 39 | 'directory' => array( 40 | 'label' => __( 'POSTMARK_DIR', 'postmark-wordpress' ), 41 | 'value' => defined( 'POSTMARK_DIR' ) ? POSTMARK_DIR : $postmark::$POSTMARK_DIR, 42 | 'private' => false, 43 | ), 44 | 'url' => array( 45 | 'label' => __( 'POSTMARK_URL', 'postmark-wordpress' ), 46 | 'value' => defined( 'POSTMARK_URL' ) ? POSTMARK_URL : $postmark::$POSTMARK_URL, 47 | 'private' => false, 48 | ), 49 | 'apikey' => array( 50 | 'label' => __( 'API Key', 'postmark-wordpress' ), 51 | 'value' => $settings->api_key, 52 | 'private' => true, 53 | ), 54 | 'stream' => array( 55 | 'label' => __( 'Stream', 'postmark-wordpress' ), 56 | 'value' => $settings->stream_name, 57 | 'private' => false, 58 | ), 59 | 'sender_address' => array( 60 | 'label' => __( 'Sender Address', 'postmark-wordpress' ), 61 | 'value' => $settings->sender_address, 62 | 'private' => false, 63 | ), 64 | 'force_from' => array( 65 | 'label' => __( 'Force From', 'postmark-wordpress' ), 66 | 'value' => $this->strbool( $settings->force_from ), 67 | 'private' => false, 68 | ), 69 | 'force_html' => array( 70 | 'label' => __( 'Force HTML', 'postmark-wordpress' ), 71 | 'value' => $this->strbool( $settings->force_html ), 72 | 'private' => false, 73 | ), 74 | 'track_opens' => array( 75 | 'label' => __( 'Track Opens', 'postmark-wordpress' ), 76 | 'value' => $this->strbool( $settings->track_opens ), 77 | 'private' => false, 78 | ), 79 | 'track_links' => array( 80 | 'label' => __( 'Track Links', 'postmark-wordpress' ), 81 | 'value' => $this->strbool( $settings->track_links ), 82 | 'private' => false, 83 | ), 84 | 'enable_logs' => array( 85 | 'label' => __( 'Logs Enabled', 'postmark-wordpress' ), 86 | 'value' => $this->strbool( $settings->enable_logs ), 87 | 'private' => false, 88 | ), 89 | ), 90 | ); 91 | 92 | return $debug_info; 93 | } 94 | 95 | /** 96 | * String Boolean. 97 | * 98 | * @param bool $value Value. 99 | * @return string Yes or No based on boolean provided. 100 | */ 101 | private function strbool( $value ) { 102 | return $value ? 'Yes' : 'No'; 103 | } 104 | 105 | } 106 | 107 | new Postmark_Debug(); 108 | 109 | } 110 | -------------------------------------------------------------------------------- /page-settings.php: -------------------------------------------------------------------------------- 1 | 5 | 22 |
23 |
24 | 25 |
26 | 27 |

28 | General 29 | Send Test Email 30 | Overrides 31 | Status 32 | 33 | settings['enable_logs'] ) && true == $this->settings['enable_logs'] ) : ?> 34 | Logs 35 | 36 | 37 | 38 | 39 | 40 | Plugin Testing 41 | 42 |

43 | 44 | 45 | 46 | overridden_settings['api_key'] ) ) : ?> 47 |
POSTMARK_API_KEY is defined in your wp-config.php and overrides the API Key set here.
48 | 49 | 50 | overridden_settings['stream_name'] ) ) : ?> 51 |
POSTMARK_STREAM_NAME is defined in your wp-config.php and overrides the Message Stream set here.
52 | 53 | 54 | overridden_settings['sender_address'] ) ) : ?> 55 |
POSTMARK_SENDER_ADDRESS is defined in your wp-config.php and overrides the Sender Email set here.
56 | 57 | 58 | overridden_settings['force_from'] ) ) : ?> 59 |
POSTMARK_FORCE_FROM is defined in your wp-config.php and overrides the Force From set here.
60 | 61 | 62 |
63 | 64 | 65 | 66 | 70 | 71 | 72 | 73 | 77 | 78 | 79 | 80 | 84 | 85 | 86 | 87 | 91 | 92 | 93 | 94 | 98 | 99 | 100 | 101 | 105 | 106 | 107 | 108 | 112 | 113 | 114 | 115 | 119 | 120 | 121 | 122 | 126 | 127 |
67 | 68 | Send emails using ActiveCampaign Postmark 69 |
74 | 75 |
Your API key is in the API Tokens tab of your Postmark Server.
76 |
81 | 82 |
Optional - Default is 'outbound' if blank.
83 |
88 | 89 |
This email must be a verified Sender Signature. It will appear as the "from" address on all outbound emails.
90 |
95 | 96 | Force emails to be sent from the Sender Email specified above. Disallows overriding using the $headers array. 97 |
102 | 103 | Force emails to be sent as HTML. 104 |
109 | 110 | Track email opens (Force HTML is required). 111 |
116 | 117 | Track links in emails. 118 |
123 | 124 | Log send attempts for historical/troubleshooting purposes (Recommended). 125 |
128 | 129 |
130 | 131 |
132 |
133 | 134 |
135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 149 | 150 |
146 | 147 | Send test as HTML, with both Open Tracking and Link Tracking enabled, and a Tag. 148 |
151 | 152 |
153 | 154 |
155 |
156 | 157 |
158 |

Developer overrides

159 |

Instead of using Force HTML, we recommend setting wp_mail headers when possible.

160 |
161 | 		$headers = array();
162 | 
163 | 		// Override the default 'From' address if 'Force Sender Email' is not enabled
164 | 		$headers['From'] = 'john.smith@example.com';
165 | 
166 | 		// Send the message as HTML
167 | 		$headers['Content-Type'] = 'text/html';
168 | 
169 | 		// Enable open tracking (requires HTML email enabled)
170 | 		$headers['X-PM-Track-Opens'] = true;
171 | 
172 | 		// Enable or disable link tracking
173 | 		// Options are None, HtmlAndText, TextOnly, or HtmlOnly
174 | 		$headers['X-PM-Track-Links'] = 'HtmlAndText';
175 | 
176 | 		// Send the email
177 | 		$response = wp_mail( $to, $subject, $message, $headers );
178 | 		
179 | To learn more about wp_mail, see the WordPress Codex page. 180 |
181 | 182 | 183 |
184 | array( 192 | 'Accept' => 'application/json', 193 | 'Content-Type' => 'application/json', 194 | ), 195 | ) 196 | ) 197 | ) 198 | ); 199 | 200 | ?> 201 | 202 | 203 | 204 | 205 | 208 | 209 | 210 | 211 | 219 | 220 | 221 | 222 | 223 | 224 |
206 | page->state; ?> 207 |
212 | page->updated_at ) ); 214 | $date_format = get_option( 'date_format' ); 215 | $time_format = get_option( 'time_format' ); 216 | echo wp_date( "{$date_format} {$time_format}", $unix_date ); 217 | ?> 218 |
225 | 226 |
227 | 228 | 229 | 230 | settings['enable_logs'] ) && true == $this->settings['enable_logs'] ) : ?> 231 |
232 | 233 | prefix . 'postmark_log'; 237 | 238 | // Checks how many logs are in the logs table. 239 | $count = $wpdb->get_var( 'SELECT COUNT(*) FROM ' . $table ); 240 | 241 | // Only shows some logs if some logs are stored. 242 | if ( $count > 0 ) { 243 | 244 | // Pulls sending logs from db to display in UI. prepare() used to prevent SQL injections 245 | $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table ORDER BY log_entry_date DESC LIMIT %d", 10 ) ); 246 | 247 | // Logs table header HTML. 248 | echo ' 249 | 250 | 251 | 252 | 253 | 254 | 255 | '; 256 | 257 | // Builds HTML for each log to show as a row in the logs table. 258 | foreach ( $result as $row ) { 259 | echo ''; 260 | } 261 | 262 | echo '
DateFromToSubjectPostmark API Response
' . date( 'Y-m-d h:i A', strtotime( esc_html( $row->log_entry_date ) ) ) . ' ' . esc_html( $row->fromaddress ) . ' ' . esc_html( $row->toaddress ) . ' ' . esc_html( $row->subject ) . ' ' . $row->response . '
'; 263 | 264 | // Shows a 'Load More' button if more than 10 logs in logs table. 265 | if ( $count > 10 ) { 266 | echo '
267 |
'; 268 | } 269 | } else { 270 | echo '

No Logs

'; 271 | } 272 | ?> 273 | 274 |
275 | 276 | 277 |
278 | 279 | 280 | 281 | 284 | 285 | 286 | 287 | 290 | 291 | 292 | 293 | 296 | 297 | 298 | 299 | 302 | 303 |
282 | 283 |
288 | 289 |
294 | 295 |
300 | 301 |
304 | 305 |
306 | 307 |
308 |
309 | 310 |
311 | -------------------------------------------------------------------------------- /postmark-settings.php: -------------------------------------------------------------------------------- 1 | defined( 'POSTMARK_API_KEY' ) ? POSTMARK_API_KEY : null, 11 | 'stream_name' => defined( 'POSTMARK_STREAM_NAME' ) ? POSTMARK_STREAM_NAME : null, 12 | 'sender_address' => defined( 'POSTMARK_SENDER_ADDRESS' ) ? POSTMARK_SENDER_ADDRESS : null, 13 | 'force_from' => defined( 'POSTMARK_FORCE_FROM' ) ? POSTMARK_FORCE_FROM : null, 14 | ); 15 | $settings_from_constants = array_filter( $settings_from_constants ); 16 | $settings = array_merge( $settings, $settings_from_constants ); 17 | return array( 18 | 'settings' => $settings, 19 | 'settings_from_constants' => $settings_from_constants, 20 | ); 21 | -------------------------------------------------------------------------------- /postmark.php: -------------------------------------------------------------------------------- 1 | settings = $this->load_settings(); 82 | $this->overridden_settings = include POSTMARK_DIR . '/postmark-settings.php'; 83 | $this->overridden_settings = $this->overridden_settings['settings_from_constants']; 84 | } 85 | 86 | /** 87 | * Init. 88 | */ 89 | public function init() { 90 | add_action( 'admin_menu', array( $this, 'admin_menu' ) ); 91 | add_action( 'wp_ajax_postmark_save', array( $this, 'save_settings' ) ); 92 | add_action( 'wp_ajax_postmark_test', array( $this, 'send_test_email' ) ); 93 | add_action( 'wp_ajax_postmark_load_more_logs', array( $this, 'postmark_load_more_logs' ) ); 94 | add_action( 'wp_ajax_postmark_test_plugin', array( $this, 'postmark_test_plugin' ) ); 95 | } 96 | 97 | /** 98 | * Load Settings. 99 | * 100 | * @return array Return settings. 101 | */ 102 | public function load_settings() { 103 | $settings = get_option( 'postmark_settings' ); 104 | 105 | if ( false === $settings ) { 106 | $settings = array( 107 | 'enabled' => get_option( 'postmark_enabled', 0 ), 108 | 'api_key' => get_option( 'postmark_api_key', '' ), 109 | 'stream_name' => get_option( 'postmark_stream_name', 'outbound' ), 110 | 'sender_address' => get_option( 'postmark_sender_address', '' ), 111 | 'force_from' => get_option( 'postmark_force_from', 0 ), 112 | 'force_html' => get_option( 'postmark_force_html', 0 ), 113 | 'track_opens' => get_option( 'postmark_trackopens', 0 ), 114 | 'track_links' => get_option( 'postmark_tracklinks', 0 ), 115 | 'enable_logs' => get_option( 'postmark_enable_logs', 1 ), 116 | ); 117 | 118 | update_option( 'postmark_settings', wp_json_encode( $settings ) ); 119 | 120 | return $settings; 121 | } 122 | 123 | if ( is_array( $settings ) && ! isset( $settings['track_links'] ) ) { 124 | $settings['track_links'] = 0; 125 | update_option( 'postmark_settings', wp_json_encode( $settings ) ); 126 | return $settings; 127 | } 128 | 129 | if ( is_array( $settings ) && ! isset( $settings['stream_name'] ) ) { 130 | $settings['stream_name'] = 'outbound'; 131 | update_option( 'postmark_settings', wp_json_encode( $settings ) ); 132 | return $settings; 133 | } 134 | 135 | if ( is_array( $settings ) && ! isset( $settings['force_from'] ) ) { 136 | $settings['force_from'] = 0; 137 | update_option( 'postmark_settings', wp_json_encode( $settings ) ); 138 | return $settings; 139 | } 140 | 141 | return json_decode( $settings, true ); 142 | } 143 | 144 | /** 145 | * Admin Menu. 146 | */ 147 | public function admin_menu() { 148 | add_options_page( 'ActiveCampaign Postmark', 'ActiveCampaign Postmark', 'manage_options', 'ac_pm_admin', array( $this, 'settings_html' ) ); 149 | } 150 | 151 | /** 152 | * Retrieves additional logs. 153 | * 154 | * @return array Response. 155 | */ 156 | public function postmark_load_more_logs() { 157 | 158 | global $wpdb; 159 | 160 | $new_rows_html = ''; 161 | 162 | $table = $wpdb->prefix . 'postmark_log'; 163 | 164 | // Checks the wp_nonce. 165 | if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'postmark_nonce' ) ) { 166 | wp_die( __( 'We were unable to verify this request, please reload the page and try again.' ) ); 167 | } 168 | 169 | // Retrieves more logs from logs table using offset and prepare() to prevent SQL injections. 170 | $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table ORDER BY log_entry_date DESC LIMIT %d OFFSET %d", 10, $_POST['offset'] ) ); 171 | 172 | $has_more = true; 173 | 174 | if ( sizeof( $result ) < 10 ) { 175 | $has_more = false; 176 | } 177 | 178 | // Iterates through the retrieved logs and builds HTML rows for each one, to be added to the logs table in the UI. 179 | foreach ( $result as $row ) { 180 | $new_rows_html = $new_rows_html . '' . date( 'Y-m-d h:i A', strtotime( $row->log_entry_date ) ) . ' ' . $row->fromaddress . ' ' . $row->toaddress . ' ' . $row->subject . ' ' . $row->response . ''; 181 | } 182 | 183 | $response = array( 184 | 'html' => $new_rows_html, 185 | // Lets the front end know how if there are any more logs. 186 | 'has_more' => $has_more, 187 | ); 188 | 189 | echo wp_json_encode( $response ); 190 | 191 | wp_die(); 192 | } 193 | 194 | /** 195 | * Send Test Email. 196 | */ 197 | public function send_test_email() { 198 | // We check the wp_nonce. 199 | if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'postmark_nonce' ) ) { 200 | wp_die( __( 'We were unable to verify this request, please reload the page and try again.' ) ); 201 | } 202 | 203 | // We check that the current user is allowed to update settings. 204 | if ( ! current_user_can( 'manage_options' ) ) { 205 | wp_die( __( 'We were unable to verify this request, please reload the page and try again.' ) ); 206 | } 207 | 208 | // We validate that 'email' is a valid email address. 209 | if ( isset( $_POST['email'] ) && is_email( $_POST['email'] ) ) { 210 | $to = sanitize_email( $_POST['email'] ); 211 | } else { 212 | wp_die( __( 'You need to specify a valid recipient email address.', 'postmark-wordpress' ) ); 213 | } 214 | 215 | // We validate that 'with_tracking_and_html' is a numeric boolean. 216 | if ( isset( $_POST['with_tracking_and_html'] ) && 1 === $_POST['with_tracking_and_html'] ) { 217 | $with_tracking_and_html = true; 218 | } else { 219 | $with_tracking_and_html = false; 220 | } 221 | 222 | // We validate that 'override_from_address' is a valid email address. 223 | if ( isset( $_POST['override_from_address'] ) && is_email( $_POST['override_from_address'] ) ) { 224 | $override_from = sanitize_email( $_POST['override_from_address'] ); 225 | } else { 226 | $override_from = false; 227 | } 228 | 229 | $subject = 'Postmark Test: ' . html_entity_decode( get_bloginfo( 'name' ) ); 230 | $override_from = $_POST['override_from_address']; 231 | $headers = array(); 232 | 233 | if ( isset( $_POST['with_tracking_and_html'] ) && $_POST['with_tracking_and_html'] ) { 234 | $message = 'This is an HTML test email sent using the Postmark plugin. It has Open Tracking and Link Tracking enabled and is tagged.'; 235 | array_push( $headers, 'X-PM-Track-Opens: true' ); 236 | array_push( $headers, 'X-PM-TrackLinks: HtmlAndText' ); 237 | array_push( $headers, 'X-PM-Tag: PostmarkPluginTest' ); 238 | } else { 239 | $message = 'This is a test email sent using the Postmark plugin.'; 240 | } 241 | 242 | if ( false !== $override_from && '' !== $override_from ) { 243 | array_push( $headers, 'From: ' . $override_from ); 244 | } 245 | 246 | $response = wp_mail( $to, $subject, $message, $headers ); 247 | 248 | if ( false !== $response ) { 249 | echo 'Test sent'; 250 | } else { 251 | $dump = print_r( self::$LAST_ERROR, true ); 252 | echo 'Test failed, the following is the error generated when running the test send:
' . $dump . '
'; 253 | } 254 | 255 | wp_die(); 256 | } 257 | 258 | /** 259 | * Save Settings. 260 | */ 261 | public function save_settings() { 262 | // We check the wp_nonce. 263 | if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'postmark_nonce' ) ) { 264 | wp_die( __( 'We were unable to verify this request, please reload the page and try again.' ) ); 265 | } 266 | 267 | // We check that the current user is allowed to update settings. 268 | if ( ! current_user_can( 'manage_options' ) ) { 269 | wp_die( __( 'We were unable to verify this request, please reload the page and try again.' ) ); 270 | } 271 | 272 | // We check that we have received some data. 273 | if ( ! isset( $_POST['data'] ) ) { 274 | wp_die( __( 'We were unable to verify this request, please reload the page and try again.' ) ); 275 | } 276 | 277 | $data = json_decode( stripslashes( $_POST['data'] ), true ); 278 | 279 | $settings = array(); 280 | 281 | // We check that we were able to decode data. 282 | if ( ! is_array( $data ) ) { 283 | wp_die( __( 'Something went wrong!', 'postmark-wordpress' ) ); 284 | } 285 | 286 | // We validate that 'enabled' is a numeric boolean. 287 | if ( isset( $data['enabled'] ) && 1 === $data['enabled'] ) { 288 | $settings['enabled'] = 1; 289 | } else { 290 | $settings['enabled'] = 0; 291 | } 292 | 293 | // We validate that 'api_key' contains only allowed caracters [letters, numbers, dash]. 294 | if ( isset( $data['api_key'] ) && ( 1 === preg_match( '/^[A-Za-z0-9\-]*$/', $data['api_key'] || 'POSTMARK_API_TEST' === $data['api_key'] ) ) ) { 295 | $settings['api_key'] = $data['api_key']; 296 | } else { 297 | $settings['api_key'] = ''; 298 | } 299 | 300 | // We validate that 'stream_name' contains only allowed caracters [letters, numbers, dash]. 301 | if ( isset( $data['stream_name'] ) && ( 1 === preg_match( '/^[A-Za-z0-9\-]*$/', $data['stream_name'] ) ) ) { 302 | $settings['stream_name'] = $data['stream_name']; 303 | } else { 304 | $settings['stream_name'] = 'outbound'; 305 | } 306 | 307 | // We validate that 'sender_address' is a valid email address. 308 | if ( isset( $data['sender_address'] ) && is_email( $data['sender_address'] ) ) { 309 | $settings['sender_address'] = sanitize_email( $data['sender_address'] ); 310 | } else { 311 | $settings['sender_address'] = ''; 312 | } 313 | 314 | // We validate that 'force_from' is a numeric boolean. 315 | if ( isset( $data['force_from'] ) && 1 === $data['force_from'] ) { 316 | $settings['force_from'] = 1; 317 | } else { 318 | $settings['force_from'] = 0; 319 | } 320 | 321 | // We validate that 'force_html' is a numeric boolean. 322 | if ( isset( $data['force_html'] ) && 1 === $data['force_html'] ) { 323 | $settings['force_html'] = 1; 324 | } else { 325 | $settings['force_html'] = 0; 326 | } 327 | 328 | // We validate that 'track_opens' is a numeric boolean. 329 | if ( isset( $data['track_opens'] ) && 1 === $data['track_opens'] ) { 330 | $settings['track_opens'] = 1; 331 | } else { 332 | $settings['track_opens'] = 0; 333 | } 334 | 335 | // We validate that 'track_links' is a numeric boolean. 336 | if ( isset( $data['track_links'] ) && 1 === $data['track_links'] ) { 337 | $settings['track_links'] = 1; 338 | } else { 339 | $settings['track_links'] = 0; 340 | } 341 | 342 | // Validates that 'enable_logs' is a numeric boolean and creates table for storing logs. 343 | if ( isset( $data['enable_logs'] ) && 1 === $data['enable_logs'] ) { 344 | $settings['enable_logs'] = 1; 345 | // check if logs table exists, if not create it. 346 | pm_log_create_db(); 347 | } 348 | // Removes logs table if setting for logging is disabled. 349 | else { 350 | $settings['enable_logs'] = 0; 351 | postmark_log_remove_table(); 352 | pm_log_cron_deactivate(); 353 | } 354 | 355 | update_option( 'postmark_settings', wp_json_encode( $settings ) ); 356 | 357 | wp_die( 'Settings saved' ); 358 | } 359 | 360 | /** 361 | * Settings HTML. 362 | */ 363 | function settings_html() { 364 | include POSTMARK_DIR . '/page-settings.php'; 365 | } 366 | } 367 | 368 | /** 369 | * Creates a table for storing logs of send attempts. 370 | */ 371 | function pm_log_create_db() { 372 | 373 | global $wpdb; 374 | 375 | $table_name = $wpdb->prefix . 'postmark_log'; 376 | 377 | // Creates a logs table if it doesn't exist already. 378 | if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) != $table_name ) { 379 | 380 | $charset_collate = $wpdb->get_charset_collate(); 381 | 382 | $sql = "CREATE TABLE $table_name ( 383 | id INT(9) NOT NULL AUTO_INCREMENT, 384 | log_entry_date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, 385 | fromaddress text, 386 | toaddress text, 387 | subject text, 388 | response text, 389 | PRIMARY KEY id (id) 390 | ) $charset_collate;"; 391 | 392 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; 393 | 394 | dbDelta( $sql ); 395 | 396 | // Activates cron job for automatically purging old (7+ days) Postmark logs. 397 | pm_log_cron_activation(); 398 | 399 | } 400 | } 401 | 402 | /** 403 | * Schedules cron job for deleting old Postmark logs. 404 | */ 405 | function pm_log_cron_activation() { 406 | if ( ! wp_next_scheduled( 'pm_log_cron_job' ) ) { 407 | wp_schedule_event( time(), 'daily', 'pm_log_cron_job' ); 408 | } 409 | } 410 | 411 | // Attaches pm_clear_old_logs function to cron job hook. 412 | add_action( 'pm_log_cron_job', 'pm_clear_old_logs' ); 413 | 414 | /** 415 | * Deletes up to 500 Postmark logs at a time that are older than 7 days. 416 | */ 417 | function pm_clear_old_logs() { 418 | 419 | global $wpdb; 420 | 421 | $table_name = $wpdb->prefix . 'postmark_log'; 422 | 423 | // Checks if there are any logs older than seven days to delete. 424 | $rows_to_delete_count = $wpdb->get_var( 425 | $wpdb->prepare( 426 | "SELECT COUNT(*) 427 | FROM $table_name 428 | WHERE BINARY(%s) < BINARY(DATE_SUB(NOW(), INTERVAL %d DAY))", 429 | 'log_entry_date', 430 | 7 431 | ) 432 | ); 433 | 434 | // Deletes logs that are more than 7 days old, limited to 500 log deletions at a time to prevent locking up db. 435 | if ( $rows_to_delete_count > 500 ) { 436 | $rows_to_delete_count = 500; 437 | } 438 | 439 | if ( $rows_to_delete_count > 0 ) { 440 | 441 | $wpdb->query( 442 | $wpdb->prepare( 443 | "DELETE FROM $table_name 444 | WHERE BINARY(%s) < BINARY(DATE_SUB(NOW(), INTERVAL %d DAY)) 445 | LIMIT %d", 446 | 'log_entry_date', 447 | 7, 448 | $rows_to_delete_count 449 | ) 450 | ); 451 | 452 | // Check again for more logs to delete. 453 | pm_clear_old_logs(); 454 | } 455 | } 456 | 457 | /** 458 | * Unschedules Postmark logs cleanup cron job. 459 | */ 460 | function pm_log_cron_deactivate() { 461 | 462 | // Checks when the next cron job was scheduled for. 463 | $timestamp = wp_next_scheduled( 'pm_log_cron_job' ); 464 | 465 | // Unschedules upcoming cron job. 466 | wp_unschedule_event( $timestamp, 'pm_log_cron_job' ); 467 | } 468 | 469 | // Removes cron job for deleting old logs, if plugin is disabled. 470 | register_deactivation_hook( __FILE__, 'pm_log_cron_deactivate' ); 471 | 472 | // Creates logs table on activation, if it doesn't exist. 473 | register_activation_hook( __FILE__, 'pm_log_create_db' ); 474 | 475 | /** 476 | * Drops logs table (called when plugin is uninstalled). 477 | */ 478 | function postmark_log_remove_table() { 479 | global $wpdb; 480 | $table_name = $wpdb->prefix . 'postmark_log'; 481 | $sql = "DROP TABLE IF EXISTS $table_name"; 482 | $wpdb->query( $sql ); 483 | } 484 | 485 | // Removes logs table on uninstall. 486 | register_uninstall_hook( __FILE__, 'postmark_log_remove_table' ); 487 | 488 | if ( ! function_exists( 'wp_mail' ) ) { 489 | $postmark = new Postmark_Mail(); 490 | 491 | if ( is_array( $postmark->settings ) && ( 1 == $postmark->settings['enabled'] ) ) { 492 | include POSTMARK_DIR . '/wp-mail.php'; 493 | } 494 | } 495 | 496 | /** 497 | * Handle upgrades (build the logs db) for upgrades from versions prior to 1.10.1. 498 | * 499 | * @param [type] $upgrader_object Upgrader Object. 500 | * @param [type] $options Options. 501 | */ 502 | function pm_upgrade_completed( $upgrader_object, $options ) { 503 | 504 | $pm_plugin = plugin_basename( __FILE__ ); 505 | 506 | if ( 'update' === $options['action'] && 'plugin' === $options['type'] && isset( $options['plugins'] ) ) { 507 | 508 | foreach ( $options['plugins'] as $plugin ) { 509 | if ( $plugin == $pm_plugin ) { 510 | pm_log_create_db(); 511 | $postmark = new Postmark_Mail(); 512 | if ( isset( $postmark ) ) { 513 | $postmark->load_settings(); 514 | } 515 | } 516 | } 517 | } 518 | } 519 | 520 | add_action( 'upgrader_process_complete', 'pm_upgrade_completed', 10, 2 ); 521 | 522 | require_once 'includes/class-postmark-debug.php'; 523 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === ActiveCampaign Postmark for Wordpress === 2 | Contributors: andy7629, alexknowshtml, mgibbs189, jptoto, atheken, prileygraham, dorzki 3 | Tags: postmark, email, smtp, notifications, wp_mail, wildbit 4 | Requires PHP: 7.0 5 | Requires at least: 5.3 6 | Tested up to: 6.7 7 | Stable tag: 1.19.1 8 | 9 | The *officially-supported* ActiveCampaign Postmark plugin for Wordpress. 10 | 11 | == Description == 12 | 13 | If you're still sending email with default SMTP, you're blind to delivery problems! ActiveCampaign Postmark for Wordpress enables sites of any size to deliver and track WordPress notification emails reliably, with minimal setup time and zero maintenance. 14 | 15 | If you don't already have a Postmark account, you can get one in minutes, sign up at https://postmarkapp.com 16 | 17 | Check out our video on how to set up the Postmark for WordPress plugin [here](https://postmarkapp.com/webinars/postmark-wordpress). 18 | 19 | == Installation == 20 | 21 | 1. Upload postmark directory to your /wp-content/plugins directory 22 | 2. Activate plugin in WordPress admin 23 | 3. In WordPress admin, go to Settings then Postmark. You will then want to insert your Postmark details. If you don't already have a Postmark account, get one at https://postmarkapp.com 24 | 4. Verify sending by entering a recipient email address you have access to and pressing the "Send Test Email" button. 25 | 5. Once verified, then check "Enable" to override `wp_mail` and send using the Postmark API instead. 26 | 27 | == Frequently Asked Questions == 28 | 29 | = What is Postmark? = 30 | 31 | Postmark is a hosted service that expertly handles all delivery of transactional webapp and web site email. This includes welcome emails, password resets, comment notifications, and more. If you've ever installed WordPress and had issues with PHP's `mail()` function not working right, or your WordPress install sends comment notifications or password resets to spam, Postmark makes all of these problems vanish in seconds. Without Postmark, you may not even know you're having delivery problems. Find out in seconds by installing and configuring this plugin. 32 | 33 | = Will this plugin work with my WordPress site? = 34 | 35 | The Postmark for WordPress plugin overrides any usage of the `wp_mail()` function. Because of this, if any 3rd party code or plugins send mail directly using the PHP mail function, or any other method, we cannot override it. Please contact the makers of any offending plugins and let them know that they should use `wp_mail()` instead of unsupported mailing functions. 36 | 37 | = TLS Version Requirements/Compatibility = 38 | 39 | The Postmark API requires TLS v1.1 or v1.2 support. We recommend using TLS v1.2. 40 | 41 | You can check your TLS v1.2 compatibility using [this plugin](https://wordpress.org/plugins/tls-1-2-compatibility-test/). After installing the plugin, change the dropdown for 'Select API Endpoint' to _How's My SSL?_ and run the test. If compatibility with TLS v1.2 is not detected, contact your server host or make the necessary upgrades to support TLS v1.1 or v1.2. 42 | 43 | TLS 1.2 requires: 44 | 45 | - PHP 5.5.19 or higher 46 | - cURL 7.34.0 or higher 47 | - OpenSSL 1.0.1 or higher 48 | 49 | = Does this cost me money? = 50 | 51 | The Postmark service (and this plugin) are free to get started. You can sign up at https://postmarkapp.com/. When you need to process more email, Postmark offers monthly plans to fit your needs. 52 | 53 | = My emails are still not sending, or they are going to spam! HELP!? = 54 | 55 | No worries, our expert team can help. Just send an email to [support@postmarkapp.com](mailto:support@postmarkapp.com) or tweet [@postmarkapp](https://twitter.com/postmarkapp) for help. Be sure to include as much detail as possible. 56 | 57 | = Why should I trust you with my email sending? = 58 | 59 | Because we've been in this business for many years. Through trial and error we already know what it takes to manage a large volume of email. We handle things like whitelisting, ISP throttling, reverse DNS, feedback loops, content scanning, and delivery monitoring to ensure your emails get to the inbox. 60 | 61 | Most importantly, a great product requires great support and even better education. Our team is spread out across six time zones to offer fast support on everything from using Postmark to best practices on content and user engagement. A solid infrastructure only goes so far, that’s why improving our customer’s sending practices helps achieve incredible results 62 | 63 | = How do I tag a message? = 64 | 65 | There are two ways to tag a message. 66 | 67 | 1. Set an X-PM-Tag message header, i.e. array_push( $headers, 'X-PM-Tag: PostmarkPluginTest' ); where you are calling wp_mail(). 68 | 69 | 2. Add a filter for 'postmark_tag' that hooks into a function that returns the tag you desire. 70 | 71 | Using the postmark_tag filter will override a tag set via message headers. 72 | 73 | = How do I add metadata to a message? = 74 | 75 | Add a filter for 'postmark_metadata' that hooks into a function which returns the array of metadata you wish to attach to a message. 76 | 77 | = Why aren't my HTML emails being sent? = 78 | 79 | This plugin detects HTML by checking the headers sent by other WordPress plugins. If a "text/html" content type isn't set then this plugin won't send the HTML to Postmark to be sent out only the plain text version of the email. 80 | 81 | = How do I set environment specific Postmark plugin settings? 82 | 83 | You can optionally set the Postmark API Token, message stream, and default sending address by adding the following to your `wp-config.php` file: 84 | 85 | ``` 86 | define( 'POSTMARK_API_KEY', '' ); 87 | define( 'POSTMARK_STREAM_NAME', 'stream-name' ); 88 | define( 'POSTMARK_SENDER_ADDRESS', 'from@example.com' ); 89 | ``` 90 | 91 | Setting any of these here will override what is set via the plugin's UI. 92 | 93 | = Why are password reset links not showing in emails sent with this plugin? = 94 | 95 | There are a couple ways to resolve this issue. 96 | 97 | 1. Open the Postmark plugin settings and uncheck Force HTML and click Save Changes. If the default WordPress password reset email is sent in Plain Text format, the link will render as expected. 98 | 99 | 2. Access your WordPress site directory and open the `wp-login.php` file. 100 | 101 | Change this line: 102 | 103 | `$message .= ‘<‘ . network_site_url(“wp-login.php?action=rp&key=$key&login=” . rawurlencode($user_login), ‘login’) . “>\r\n”;` 104 | 105 | Remove the brackets, so it becomes: 106 | 107 | `$message .= network_site_url(“wp-login.php?action=rp&key=$key&login=” . rawurlencode($user_login), ‘login’) . “\r\n”;` 108 | 109 | And save the changes to the file. 110 | 111 | = How do I set the from name? = 112 | 113 | The plugin supports using the `wp_mail_from_name` filter for manually setting a name in the From header. 114 | 115 | = I also need a marketing automation tool to connect to my Wordpress site. What do you recommend? = 116 | 117 | At [ActiveCampaign](https://www.activecampaign.com/?utm_source=postmark&utm_medium=referral&utm_campaign=postmark_wordpress) you’ll find the email marketing, marketing automation, and CRM tools your marketing and sales teams will love. [ActiveCampaign integrates seamlessly with your Wordpress site](https://wordpress.org/plugins/activecampaign-subscription-forms/) so you can embed forms and collect email addresses on any page, track website visits (and then use that information to trigger automated follow-up emails), or enable live chat to engage with your visitors in real time. [Learn more about ActiveCampaign for Wordpress](https://wordpress.org/plugins/activecampaign-subscription-forms/). 118 | 119 | == Additional Resources == 120 | 121 | [Postmark for WordPress FAQ](https://postmarkapp.com/support/article/1138-postmark-for-wordpress-faq) 122 | 123 | [Can I use the Postmark for WordPress plugin with Gravity Forms?](https://postmarkapp.com/support/article/1129-can-i-use-the-postmark-for-wordpress-plugin-with-gravity-forms) 124 | 125 | [How do I send with Ninja Forms and Postmark for WordPress?](https://postmarkapp.com/support/article/1047-how-do-i-send-with-ninja-forms-and-postmark-for-wordpress) 126 | 127 | [How do I send with Contact Form 7 and Postmark for WordPress?](https://postmarkapp.com/support/article/1072-how-do-i-send-with-contact-form-7-and-postmark-for-wordpress) 128 | 129 | [Can I use the Postmark for WordPress plugin with Divi contact forms?](https://postmarkapp.com/support/article/1128-can-i-use-the-postmark-for-wordpress-plugin-with-divi-contact-forms) 130 | 131 | == Screenshots == 132 | 133 | 1. ActiveCampaign Postmark WP Plugin Settings screen. 134 | 135 | == Changelog == 136 | = v1.19.1 = 137 | * Fix warning when logging sends with a null To address 138 | 139 | -------- 140 | 141 | [See the previous changelogs here](https://github.com/ActiveCampaign/postmark-wordpress/blob/master/CHANGELOG.md) 142 | -------------------------------------------------------------------------------- /wp-mail.php: -------------------------------------------------------------------------------- 1 | ) if wp_mail_from_name filter used. 24 | * 25 | * @param string $from From email address. 26 | * @param string $from_name From header display name. 27 | */ 28 | function build_from_header_with_name( $from, $from_name ) { 29 | // Check if email address is already in Display Name format. 30 | if ( false !== strpos( $from, '<' ) && false !== strpos( $from, '>' ) ) { 31 | $email_address_start = strpos( $from, '<' ) + 1; 32 | $email_address_end = strpos( $from, '>' ); 33 | $email_address = substr( $from, $email_address_start, ( $email_address_end - $email_address_start ) ); 34 | } else { 35 | $email_address = $from; 36 | } 37 | 38 | return $from_name . ' <' . $email_address . '>'; 39 | } 40 | 41 | /** 42 | * WP Mail. 43 | * 44 | * @param [type] $to TO. 45 | * @param [type] $subject Subject. 46 | * @param [type] $message Message. 47 | * @param string $headers Headers. 48 | * @param array $attachments Attachments. 49 | */ 50 | function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) { 51 | 52 | // Compact the input, apply the filters, and extract them back out. 53 | $atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ); 54 | extract( $atts ); 55 | 56 | // Support using pre_wp_mail filter to short-circuit email sending. 57 | $pre_wp_mail = apply_filters( 'pre_wp_mail', null, $atts ); 58 | 59 | if ( null !== $pre_wp_mail ) { 60 | return $pre_wp_mail; 61 | } 62 | 63 | $settings = include POSTMARK_DIR . '/postmark-settings.php'; 64 | $settings = $settings['settings']; 65 | 66 | if ( ! is_array( $attachments ) ) { 67 | $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) ); 68 | } 69 | 70 | if ( ! empty( $headers ) && ! is_array( $headers ) ) { 71 | $headers = explode( "\n", str_replace( "\r\n", "\n", $headers ) ); 72 | } 73 | 74 | /* 75 | ================================================== 76 | Parse headers 77 | ================================================== 78 | */ 79 | 80 | $recognized_headers = array(); 81 | 82 | $headers_list = array( 83 | 'Content-Type' => array(), 84 | 'Cc' => array(), 85 | 'Bcc' => array(), 86 | 'Reply-To' => array(), 87 | 'From' => array(), 88 | 'X-PM-Track-Opens' => array(), 89 | 'X-PM-TrackLinks' => array(), 90 | 'X-PM-Tag' => array() 91 | ); 92 | 93 | $headers_list_lowercase = array_change_key_case( $headers_list, CASE_LOWER ); 94 | 95 | if ( ! empty( $headers ) ) { 96 | foreach ( $headers as $key => $header ) { 97 | $key = strtolower( $key ); 98 | if ( array_key_exists( $key, $headers_list_lowercase ) ) { 99 | $header_key = $key; 100 | $header_val = $header; 101 | $segments = explode( ':', $header ); 102 | if ( 2 === count( $segments ) ) { 103 | if ( array_key_exists( strtolower( $segments[0] ), $headers_list_lowercase ) ) { 104 | list( $header_key, $header_val ) = $segments; 105 | $header_key = strtolower( $header_key ); 106 | } 107 | } 108 | } else { 109 | $segments = explode( ':', $header ); 110 | if ( 2 === count( $segments ) ) { 111 | if ( array_key_exists( strtolower( $segments[0] ), $headers_list_lowercase ) ) { 112 | list( $header_key, $header_val ) = $segments; 113 | $header_key = strtolower( $header_key ); 114 | } 115 | } 116 | } 117 | 118 | // If the key was detected, assign it. 119 | if ( isset( $header_key ) && isset( $header_val ) ) { 120 | if ( false === stripos( $header_val, ',' ) ) { 121 | $headers_list_lowercase[ $header_key ][] = trim( $header_val ); 122 | } else { 123 | $vals = explode( ',', $header_val ); 124 | foreach ( $vals as $val ) { 125 | $headers_list_lowercase[ $header_key ][] = trim( $val ); 126 | } 127 | } 128 | 129 | unset( $header_key ); 130 | unset( $header_val ); 131 | } 132 | } 133 | 134 | foreach ( $headers_list as $key => $value ) { 135 | $value = $headers_list_lowercase[ strtolower( $key ) ]; 136 | if ( count( $value ) > 0 ) { 137 | $recognized_headers[ $key ] = implode( ', ', $value ); 138 | } 139 | } 140 | } 141 | 142 | /* 143 | ================================================== 144 | Content-Type hook 145 | ================================================== 146 | */ 147 | 148 | $content_type = 'text/plain'; 149 | if ( isset( $recognized_headers['Content-Type'] ) ) { 150 | if ( false !== strpos( $recognized_headers['Content-Type'], 'text/html' ) ) { 151 | $content_type = 'text/html'; 152 | } 153 | } 154 | $content_type = apply_filters( 'wp_mail_content_type', $content_type ); 155 | 156 | /* 157 | ================================================== 158 | Generate POST payload 159 | ================================================== 160 | */ 161 | 162 | // Allow overriding the From address when specified in the headers. 163 | $from = $settings['sender_address']; 164 | 165 | $force_from = isset( $settings['force_from'] ) && $settings['force_from']; 166 | if ( false === $force_from && isset( $recognized_headers['From'] ) ) { 167 | $from = $recognized_headers['From']; 168 | } 169 | 170 | // Support using wp_mail_from_name filter. 171 | $from_name = ''; 172 | $from_name = apply_filters( 'wp_mail_from_name', $from_name ); 173 | 174 | if ( ! empty ( $from_name ) ) { 175 | $from = build_from_header_with_name( $from, $from_name ); 176 | } 177 | 178 | $body = array( 179 | 'From' => $from, 180 | 'To' => is_array( $to ) ? implode( ',', $to ) : $to, 181 | 'Subject' => $subject, 182 | 'TextBody' => $message, 183 | ); 184 | 185 | if ( ! empty( $recognized_headers['Cc'] ) ) { 186 | $body['Cc'] = $recognized_headers['Cc']; 187 | } 188 | 189 | if ( ! empty( $recognized_headers['Bcc'] ) ) { 190 | $body['Bcc'] = $recognized_headers['Bcc']; 191 | } 192 | 193 | if ( ! empty( $recognized_headers['Reply-To'] ) ) { 194 | $body['ReplyTo'] = $recognized_headers['Reply-To']; 195 | } 196 | 197 | if ( isset( $settings['track_opens'] ) ) { 198 | $track_opens = (int) $settings['track_opens']; 199 | } 200 | 201 | if ( isset( $settings['track_links'] ) ) { 202 | $track_links = (int) $settings['track_links']; 203 | } else { 204 | $track_links = 0; 205 | } 206 | 207 | if ( isset( $recognized_headers['X-PM-Track-Opens'] ) ) { 208 | if ( $recognized_headers['X-PM-Track-Opens'] ) { 209 | $track_opens = 1; 210 | } else { 211 | $track_opens = 0; 212 | } 213 | } 214 | 215 | if ( isset( $recognized_headers['X-PM-TrackLinks'] ) ) { 216 | if ( 'none' !== $recognized_headers['X-PM-TrackLinks'] ) { 217 | $body['TrackLinks'] = $recognized_headers['X-PM-TrackLinks']; 218 | } else { 219 | $body['TrackLinks'] = 'none'; 220 | } 221 | } 222 | 223 | $body['Tag'] = null; 224 | 225 | if ( isset( $recognized_headers['X-PM-Tag'] ) ) { 226 | $body['Tag'] = $recognized_headers['X-PM-Tag']; 227 | } 228 | 229 | // Support using a filter to set a tag on a message 230 | if ( has_filter( 'postmark_tag' ) ) { 231 | $body['Tag'] = apply_filters( 'postmark_tag', $body['Tag'] ); 232 | } 233 | 234 | // Support using a filter to set metadata on a message 235 | if ( has_filter( 'postmark_metadata' ) ) { 236 | $body['Metadata'] = apply_filters( 'postmark_metadata', array() ); 237 | } 238 | 239 | if ( 1 === (int) $settings['force_html'] || 'text/html' === $content_type || 1 === $track_opens ) { 240 | $body['HtmlBody'] = $message; 241 | // The user really, truly wants this sent as HTML, don't send it as text, too. 242 | // For historical reasons, we can't "force html" and "track opens" set both html and text bodies, 243 | // which is incorrect, but in order not to break existing behavior, we only strip out the textbody when 244 | // the user has gone to the trouble of specifying content type of 'text/html' in their headers. 245 | if ( 'text/html' === $content_type ) { 246 | unset( $body['TextBody'] ); 247 | } 248 | } 249 | 250 | if ( 1 === $track_opens ) { 251 | $body['TrackOpens'] = 'true'; 252 | } 253 | 254 | if ( 1 === $track_links ) { 255 | $body['TrackLinks'] = 'HtmlAndText'; 256 | } 257 | 258 | if ( isset( $settings['stream_name'] ) && 'outbound' !== $settings['stream_name'] ) { 259 | $body['MessageStream'] = $settings['stream_name']; 260 | } else { 261 | $body['MessageStream'] = 'outbound'; 262 | } 263 | 264 | foreach ( $attachments as $attachment ) { 265 | if ( is_readable( $attachment ) ) { 266 | $body['Attachments'][] = array( 267 | 'Name' => basename( $attachment ), 268 | 'Content' => base64_encode( file_get_contents( $attachment ) ), 269 | 'ContentType' => postmark_determine_mime_content_type( $attachment ), 270 | ); 271 | } 272 | } 273 | 274 | /* 275 | ================================================== 276 | Send email 277 | ================================================== 278 | */ 279 | 280 | // Handle apostrophes in email address From names by escaping them for the Postmark API. 281 | $from_regex = "/(\"From\": \"[a-zA-Z\\d]+)*[\\\\]{2,}'/"; 282 | 283 | $args = array( 284 | 'headers' => array( 285 | 'Accept' => 'application/json', 286 | 'Content-Type' => 'application/json', 287 | 'X-Postmark-Server-Token' => $settings['api_key'], 288 | ), 289 | 'body' => preg_replace( $from_regex, "'", wp_json_encode( $body ), 1 ), 290 | ); 291 | $response = wp_remote_post( 'https://api.postmarkapp.com/email', $args ); 292 | 293 | // Logs send attempt, if logging enabled. 294 | if ( isset( $settings['enable_logs'] ) && 1 === $settings['enable_logs'] ) { 295 | global $wpdb; 296 | $table = $wpdb->prefix . 'postmark_log'; 297 | $to = $body['To']; 298 | 299 | // Only store the To address, not the To name. 300 | if ( ! empty( $body['To'] ) && ! empty( $to ) && false !== strpos( $body['To'], '<' ) && false !== strpos( $to, '>' ) ) { 301 | $to = substr( $body['To'], strpos( $body['To'], '<' ), -1 ); 302 | } 303 | 304 | // Only store the From address, not the From name. 305 | if ( false !== strpos( $from, '<' ) && false !== strpos( $from, '>' ) ) { 306 | $from = substr( $from, strpos( $from, '<' ), strpos( $from, '>' ) - 1 ); 307 | } 308 | 309 | $log_entry = array( 310 | 'log_entry_date' => current_time( 'mysql' ), 311 | 'fromaddress' => sanitize_email( $from ), 312 | 'toaddress' => sanitize_email( $to ), 313 | 'subject' => sanitize_text_field( $subject ), 314 | ); 315 | 316 | if ( is_array( $response ) ) { 317 | $log_entry['response'] = sanitize_text_field( $response['body'] ); 318 | 319 | } elseif ( is_wp_error( $response ) ) { 320 | $log_entry['response'] = $response->get_error_message(); 321 | } 322 | 323 | $wpdb->insert( $table, $log_entry ); 324 | } 325 | 326 | if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { 327 | do_action( 'postmark_error', $response, $headers ); 328 | Postmark_Mail::$LAST_ERROR = $response; 329 | return false; 330 | } 331 | 332 | do_action( 'postmark_response', $response, $headers ); 333 | 334 | // Support wp_mail_succeeded action 335 | do_action('wp_mail_succeeded', array( 336 | 'to' => $body['To'], 337 | 'subject' => $body['Subject'], 338 | 'message' => $body['HtmlBody'] ?? $body['TextBody'], 339 | 'headers' => $recognized_headers, 340 | 'attachments' => $body['Attachments'] ?? null, 341 | )); 342 | 343 | return true; 344 | } 345 | --------------------------------------------------------------------------------