Skouter mortgage estimates. Web application with view written in PHP and Vue, but controller and models in Go.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

README.md 16 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. # Grav Email Plugin
  2. The **email plugin** for [Grav](http://github.com/getgrav/grav) adds the ability to send email utilizing the `symfony/mailer` package. This is particularly useful for the **admin** and **login** plugins.
  3. > IMPORTANT: **Version 4.0** replaced the old deprecated **SwiftMailer** library with **Symfony/Mailer** package. This is a modern and well supported library that also has the capability to support 3rd party transport engines such as `SendGrid`, `MailJet`, `MailGun`, `MailChimp`, etc. This library should be backwards compatible with existing configurations, but please create an issue if you run into any problems.
  4. # Installation
  5. The email plugin is easy to install with GPM.
  6. ```
  7. $ bin/gpm install email
  8. ```
  9. # Configuration
  10. The plugin uses `sendmail` binary as the default mail engine.
  11. ```
  12. enabled: true
  13. from:
  14. to:
  15. mailer:
  16. engine: sendmail
  17. smtp:
  18. server: localhost
  19. port: 25
  20. encryption: none
  21. user:
  22. password:
  23. sendmail:
  24. bin: '/usr/sbin/sendmail -bs'
  25. content_type: text/html
  26. debug: false
  27. ```
  28. You can configure the Email plugin by using the Admin plugin, navigating to the Plugins list and choosing `Email`.
  29. That's the easiest route. Or you can also alter the Plugin configuration by copying the `user/plugins/email/email.yaml` file into `user/config/plugins/email.yaml` and make your modifications there.
  30. The first setting you'd likely change is your `Email from` / `Email to` names and emails.
  31. Also, you'd likely want to setup a SMTP server instead of using PHP Mail, as the latter is not 100% reliable and you might experience problems with emails.
  32. ## Built-in Engines
  33. By default Email 4.0 supports 4 native engines:
  34. * SMTP - Standard "Simple Mail Transport Protocol" - The default for most providers
  35. * SMTPS - "Simple Mail Transport Protocol Secure" - Not very commonly used
  36. * Sendmail - Uses the built-in `sendmail` binary file available on many Linux and Mac systems
  37. * Native - Uses `sendmail_path` of `php.ini` for Mac + Linux, and `smtp` and `smtp_port` on Windows
  38. Due to the modular nature of Symfony/Mailer, 3rd party engines are supported via Grav plugins.
  39. ## 3rd-Party Engines Plugin Support
  40. Along with the **Email** `v4.0` release, there has also been several custom provider plugins released to provide support for `SMTP`, `API`, and sometimes even `HTTPS` support for 3rd party providers such as **Sendgrid**, **MailJet**, **MailGun**, **Amazon SES**, **Mailchimp/Mandrill**, and others! `API` or `HTTPS` will provide a faster email sending experience compared to `SMTP` which is an older protocol and requires more back-and-forth negotiation and communication compared to the single-request of `API` or `HTTPS` solutions.
  41. Examples of the currently available plugins include:
  42. * https://github.com/getgrav/grav-plugin-email-sendgrid - Sengrid Mailer
  43. * https://github.com/getgrav/grav-plugin-email-amazon - Amazon SES
  44. * https://github.com/getgrav/grav-plugin-email-mandrill - Mailchimp Mandrill Mailer
  45. * https://github.com/getgrav/grav-plugin-email-mailersend - Mailersend Mailer
  46. More plugins will be released soon to support `Gmail`, `Mailgun`, `Mailjet`, `OhMySMTP`, `Postmark`, and `SendInBlue`.
  47. ## SMTP Configurations for popular solutions:
  48. ### Google Email
  49. A popular option for sending email is to simply use your Google Accounts SMTP server. To set this up you will need to do 2 things first:
  50. As Gmail no longer supports the "allow less secure apps" option, you now need to have 2FA enabled on the account and setup an "App Password" to create a specific password rather than your general account password. Follow these instructions: [https://support.google.com/accounts/answer/185833](https://support.google.com/accounts/answer/185833)
  51. Then configure the Email plugin:
  52. ```
  53. mailer:
  54. engine: smtp
  55. smtp:
  56. server: smtp.gmail.com
  57. port: 587
  58. user: 'YOUR_GOOGLE_EMAIL_ADDRESS'
  59. password: 'YOUR_GOOGLE_PASSWORD'
  60. ```
  61. > NOTE: Check your email sending limits: https://support.google.com/a/answer/166852?hl=en
  62. ### Mailtrap.io
  63. A good way to test emails is to use a SMTP server service that's built for testing emails, for example [https://mailtrap.io](https://mailtrap.io)
  64. Setup the Email plugin to use that SMTP server with the fake inbox data. For example enter this configuration in `user/config/plugins/email.yaml` or through the Admin panel:
  65. ```
  66. mailer:
  67. engine: smtp
  68. smtp:
  69. server: smtp.mailtrap.io
  70. port: 2525
  71. encryption: none
  72. user: YOUR_MAILTRAP_INBOX_USER
  73. password: YOUR_MAILTRAP_INBOX_PASSWORD
  74. ```
  75. That service will intercept emails and show them on their web-based interface instead of sending them for real.
  76. You can try and fine tune the emails there while testing.
  77. ### Sparkpost
  78. Generous email sending limits even in the free tier, and simple setup, make [Sparkpost](https://www.sparkpost.com) a great option for email sending. You just need to create an account, then setup a verified sending domain. Sparkpost does a nice job of making this process very easy and undertandable. Then just click on the SMTP Relay option to get your details for the configuration:
  79. ```
  80. mailer:
  81. engine: smtp
  82. smtp:
  83. server: smtp.sparkpostmail.com
  84. port: 587
  85. user: 'SMTP_Injection'
  86. password: 'SEND_EMAIL_API_KEY'
  87. ```
  88. Then try sending a test email...
  89. ### Sendgrid
  90. [Sendgrid](https://sendgrid.com) offers a very easy-to-setup serivce with 100 emails/day for free. The next level allows you to send 40k/email a day for just $10/month. Configuration is pretty simple, just create an account, then click SMTP integration and click the button to create an API key. The configuration is as follows:
  91. ```
  92. mailer:
  93. engine: smtp
  94. smtp:
  95. server: smtp.sendgrid.net
  96. port: 587
  97. user: 'apikey'
  98. password: 'YOUR_SENDGRID_API_KEY'
  99. ```
  100. ### Mailgun
  101. [Mailgun is a great service](https://www.mailgun.com/) that offers 10k/emails per month for free. Setup does require SPIF domain verification so that means you need to add at least a TXT entry in your DNS. This is pretty standard for SMTP sending services and does provide verification for remote email servers and makes your email sending more reliable. The Mailgun site, walks you through this process however, and the verification process is simple and fast.
  102. ```
  103. mailer:
  104. engine: smtp
  105. smtp:
  106. server: smtp.mailgun.org
  107. port: 587
  108. user: 'MAILGUN_EMAIL_ADDRESS'
  109. password: 'MAILGUN_EMAIL_PASSWORD'
  110. ```
  111. Adjust these configurations for your account.
  112. ### MailJet
  113. Mailjet is another great service that is easy to quickly setup and get started sending email. The free account gives you 200 emails/day or 600 emails/month. Just signup and setup your SPF and DKIM entries for your domain. Then click on the SMTP settings and use those to configure the email plugin:
  114. ```
  115. mailer:
  116. engine: smtp
  117. smtp:
  118. server: in-v3.mailjet.com
  119. port: 587
  120. user: 'MAILJUST_USERNAME_API_KEY'
  121. password: 'MAILJUST_PASSWORD_SECRET_KEY'
  122. ```
  123. ### ZOHO
  124. ZOHO is a popular solution for hosted email due to it's great 'FREE' tier. It's paid options are also very reasonable and combined with the latest UI updates and outstanding security features, it's a solid email option.
  125. In order to get ZOHO working with Grav, you need to send email via a user account. You can either use your users' password or generate an **App Password** via your ZOHO account (clicking on your avatar once logged in), then navigating to `My Account -> Security -> App Passwords -> Generate`. Just enter a unique app name (i.e. `Grav Website`).
  126. NOTE: The SMTP host required can be found in `Settings -> Mail - > Mail Accounts -> POP/IMAP -> SMTP`. This will provide the SMTP server for this account (it may not be `imap.zoho.com` depending on what region you are in)
  127. ```
  128. mailer:
  129. engine: smtp
  130. smtp:
  131. server: smtp.zoho.com
  132. port: 587
  133. user: 'ZOHO_EMAIL_ADDRESS'
  134. password: 'ZOHO_EMAIL_PASSWORD'
  135. ```
  136. ### Sendmail
  137. Although not as reliable as SMTP not providing as much debug information, sendmail is a simple option as long as your hosting provider is not blocking the default SMTP port `25`:
  138. ```
  139. mailer:
  140. engine: sendmail
  141. sendmail:
  142. bin: '/usr/sbin/sendmail -bs'
  143. ```
  144. Simply adjust your binary command line to suite your environment
  145. ## SMTP Email Services
  146. Solid SMTP options that even provide a FREE tier for low email volumes include:
  147. * SendGrid (100/day free) - https://sendgrid.com
  148. * Mailgun - (10k/month free) - https://www.mailgun.com
  149. * Mailjet - (6k/month free) - https://www.mailjet.com/
  150. * Sparkpost - (15k/month free) - https://www.sparkpost.com
  151. * Amazon SES (62k/month free) - https://aws.amazon.com/ses/
  152. If you are still unsure why should be using one in the first place, check out this article: https://zapier.com/learn/email-marketing/best-transactional-email-sending-services/
  153. ## Testing with CLI Command
  154. You can test your email configuration with the following CLI Command:
  155. ```
  156. $ bin/plugin email test-email -t test@email.com
  157. ```
  158. You can also pass in a configuration environment:
  159. ```
  160. $ bin/plugin email test-email -t test@email.com --env=mysite.com
  161. ```
  162. This will use the email configuration you have located in `user/mysite.com/config/plugins/email.yaml`. Read the docs to find out more about environment-based configuration: https://learn.getgrav.org/advanced/environment-config
  163. # Programmatically send emails
  164. Add this code in your plugins:
  165. ```php
  166. $to = 'email@test.com';
  167. $from = 'email@test.com';
  168. $subject = 'Test';
  169. $content = 'Test';
  170. $message = $this->grav['Email']->message($subject, $content, 'text/html')
  171. ->setFrom($from)
  172. ->setTo($to);
  173. $sent = $this->grav['Email']->send($message);
  174. ```
  175. # Emails sent with Forms
  176. When executing email actions during form processing, action parameters are inherited from the global configuration but may also be overridden on a per-action basis.
  177. ```
  178. title: Custom form
  179. form:
  180. name: custom_form
  181. fields:
  182. # Any fields you'd like to add to the form:
  183. # Their values may be referenced in email actions via '{{ form.value.FIELDNAME|e }}'
  184. process:
  185. email:
  186. subject: "[Custom form] {{ form.value.name|e }}"
  187. body: "{% include 'forms/data.txt.twig' %}"
  188. from: Custom Sender <sender@example.com>
  189. to: Custom Recipient <recipient@example.com>
  190. process_markdown: true
  191. ```
  192. ## Multiple Emails
  193. You can send multiple emails by creating an array of emails under the `process: email:` option in the form:
  194. ```
  195. title: Custom form
  196. form:
  197. name: custom_form
  198. fields:
  199. # Any fields you'd like to add to the form:
  200. # Their values may be referenced in email actions via '{{ form.value.FIELDNAME|e }}'
  201. process:
  202. email:
  203. -
  204. subject: "[Custom Email 1] {{ form.value.name|e }}"
  205. body: "{% include 'forms/data.txt.twig' %}"
  206. from: Site Owner <owner@mysite.com>
  207. to: Recipient 1 <recepient_1@example.com>
  208. template: "email/base.html.twig"
  209. -
  210. subject: "[Custom Email 2] {{ form.value.name|e }}"
  211. body: "{% include 'forms/data.txt.twig' %}"
  212. from: Site Owner <owner@mysite.com>
  213. to: Recipient 2 <recepient_2@example.com>
  214. template: "email/base.html.twig"
  215. ```
  216. ## Templating Emails
  217. You can specify a Twig template for HTML rendering, else Grav will use the default one `email/base.html.twig` which is included in this plugin. You can also specify a custom template that extends the base, where you can customize the `{% block content %}` and `{% block footer %}`. For example:
  218. ```twig
  219. {% extends 'email/base.html.twig' %}
  220. {% block content %}
  221. <p>
  222. Greetings {{ form.value.name|e }},
  223. </p>
  224. <p>
  225. We have received your request for help. Our team will get in touch with you within 3 Business Days.
  226. </p>
  227. <p>
  228. Regards,
  229. </p>
  230. <p>
  231. <b>My Company</b>
  232. <br /><br />
  233. E - <a href="mailto:help@mycompany.com">help@mycompany.com</a><br />
  234. M - +1 555-123-4567<br />
  235. W - <a href="https://mycompany.com">mycompany.com</a>
  236. </p>
  237. {% endblock %}
  238. {% block footer %}
  239. <p style="text-align: center;">My Company - All Rights Reserved</p>
  240. {% endblock %}
  241. ```
  242. ## Sending Attachments
  243. You can add file inputs to your form, and send those files via Email.
  244. Just add an `attachments` field and list the file input fields names. You can have multiple file fields, and this will send all the files as attachments. Example:
  245. ```
  246. form:
  247. name: custom_form
  248. fields:
  249. my-file:
  250. label: 'Add a file'
  251. type: file
  252. multiple: false
  253. destination: user/data/files
  254. accept:
  255. - application/pdf
  256. - application/x-pdf
  257. - image/png
  258. - text/plain
  259. process:
  260. email:
  261. body: '{% include "forms/data.html.twig" %}'
  262. attachments:
  263. - 'my-file'
  264. ```
  265. ## Additional action parameters
  266. To have more control over your generated email, you may also use the following additional parameters:
  267. * `reply_to`: Set one or more addresses that should be used to reply to the message.
  268. * `cc` _(Carbon copy)_: Add one or more addresses to the delivery list. Many email clients will mark email in one's inbox differently depending on whether they are in the `To:` or `Cc:` list.
  269. * `bcc` _(Blind carbon copy)_: Add one or more addresses to the delivery list that should (usually) not be listed in the message data, remaining invisible to other recipients.
  270. ### Specifying email addresses
  271. Email-related parameters (`from`, `to`, `reply_to`, `cc`and `bcc`) allow different notations for single / multiple values:
  272. #### Single email address string
  273. ```
  274. to: mail@example.com
  275. ```
  276. #### `name-addr` RFC822 Formatted string
  277. ```
  278. to: Joe Bloggs <maiil@example.com>
  279. ```
  280. #### Multiple email address strings
  281. ```
  282. to:
  283. - mail@example.com
  284. - mail+1@example.com
  285. - mail+2@example.com
  286. ```
  287. or in `name-addr` format:
  288. ```
  289. to:
  290. - Joe Bloggs <mail@example.com>
  291. - Jane Doe <mail+1@example.com>
  292. - Jasper Jesperson <mail+2@example.com>
  293. ```
  294. #### Simple array format with names
  295. ```
  296. to: [mail@exmaple.com, Joe Bloggs]
  297. ```
  298. #### Formatted email address with names
  299. ```
  300. to:
  301. email: mail@example.com
  302. name: Joe Bloggs
  303. ```
  304. or inline:
  305. ```
  306. to: {email: 'mail@example.com', name: 'Joe Bloggs'}
  307. ```
  308. #### Multiple email addresses (with and without names)
  309. ```
  310. to:
  311. - [mail@example.com, Joe Bloggs]
  312. - [mail+2@example.com, Jane Doe]
  313. ```
  314. ```
  315. to:
  316. -
  317. email: mail@example.com
  318. name: Joe Bloggs
  319. -
  320. email: mail+2@example.com
  321. name: Jane Doe
  322. ```
  323. or inline:
  324. ```
  325. to:
  326. - {email: 'mail@example.com', name: 'Joe Bloggs'}
  327. - {email: 'mail+2@example.com', name: 'Jane Doe'}
  328. ```
  329. ## Multi-part MIME messages
  330. Apart from a simple string, an email body may contain different MIME parts (e.g. HTML body with plain text fallback):
  331. ```
  332. body:
  333. -
  334. content_type: 'text/html'
  335. body: "{% include 'forms/default/data.html.twig' %}"
  336. -
  337. content_type: 'text/plain'
  338. body: "{% include 'forms/default/data.txt.twig' %}"
  339. ```
  340. # Troubleshooting
  341. ## Emails are not sent
  342. #### Debugging
  343. The first step in determining why emails are not sent is to enable debugging. This can be done via the `user/config/email.yaml` file or via the plugin settings in the admin. Just enable this and then try sending an email again. Then inspect the `logs/email.log` file for potential problems.
  344. #### ISP Port 25 blocking
  345. By default, when sending via PHP or Sendmail the machine running the webserver will attempt to send mail using the SMTP protocol. This uses port `25` which is often blocked by ISPs to protected against spamming. You can determine if this port is blocked by running this command in your temrinal (mac/linux only):
  346. ```
  347. (echo >/dev/tcp/localhost/25) &>/dev/null && echo "TCP port 25 opened" || echo "TCP port 25 closed"
  348. ```
  349. If it's blocked there are ways to configure relays to different ports, but the simplest solution is to use SMTP for mail sending.
  350. #### Exceptions
  351. If you get an exception when sending email but you cannot see what the error is, you need to enable more verbose exception messages. In the `user/config/system.yaml` file ensure your have the following configuration:
  352. ```
  353. errors:
  354. display: 1
  355. log: true
  356. ```
  357. ## Configuration Issues
  358. As explained above in the Configuration section, if you're using the default settings, set the Plugin configuration to use a SMTP server. It can be [Gmail](https://www.digitalocean.com/community/tutorials/how-to-use-google-s-smtp-server) or another SMTP server you have at your disposal.
  359. This is the first thing to check. The reason is that PHP Mail, the default system used by the Plugin, is not 100% reliable and emails might not arrive.