cstudios / craft-smtpmailer
一款用于发送各种方式电子邮件的联系人表单插件
Requires
- craftcms/cms: ^4.0.0-alpha
- google/recaptcha: ^1.2
- phpmailer/phpmailer: ^6.4
README
Craft CMS SMTP 邮件插件
我们为内部使用构建了这个插件,但决定将其公开
与 Pixel&Tonic 的联系人表单相比,SMTP 邮件插件提供了更多的发送邮件选项。但是,您必须以编程方式设置一切。此插件没有控制面板界面。
在您使用此插件之前,请务必仔细阅读说明。
说明
将这些行添加到您的 .env
文件中
MAILER_HOST=smtp.example.com MAILER_USERNAME=login@example.com MAILER_PASSWORD=******** MAILER_PORT=465 MAILER_ENCRYPTION=ssl MAILER_DELIVERY_PROTOCOL=smtp
显然,这些只是占位符,您需要自己的 SMTP 配置才能使其工作。
在您的 templates
目录下创建一个 _mails
目录 > templates/_mails
注意下划线 > _
templates/_mails
在您的配置文件夹中创建一个 email_template_config.php
文件 > config/email_template_config.php
并将此 文件 的内容复制到其中
这些模板是如何工作的?
templates/_mails
包含您的电子邮件模板。这些将通过电子邮件地址发送。此目录中的文件将获取您在联系人页面上设置的变量。
示例
您有一个工作申请表单(例如,在这里:templates/pages/contact.twig
)。您可能想获取申请人的年龄
如果您使用此代码
<label for="job">How old are you</label> <input id="job" type="text" name="applicant_age">
那么您可以在您的电子邮件模板(您可能在这里设置,templates/_mails/job_contact.twig
)中使用此变量,如下所示
<p>Applicant's age: {{ applicant_age }}</p>
这取决于您在输入字段的名称属性中输入的内容。(name="variable_name"
)
我们将发送电子邮件到哪儿?
首先,重要的是要知道电子邮件模板不仅是一个文件名,还是一个标识符。因此,如果您有一个 templates/_mails/job_contact.twig
文件,则此模板的 ID 为 job_contact
在您的联系人页面上放置此行
{{ craft.email.templateInput('job_contact') | raw }}
这将告诉此插件两件事
- 我们有一个
templates/_mails/job_contact.twig
文件 - 我们在
config/email_template_config.php
文件中有一个job_contact
键
现在这个文件很重要。在这里,您实际上引用了 PHPmailer 中指定的函数。您使用键来引用方法,并使用值来填写数据。我在此存储库中留下了一个示例:[https://raw.githubusercontent.com/cstudiossro/smtpmailer/main/email_template_config.php](https://raw.githubusercontent.com/cstudiossro/smtpmailer/main/email_template_config.php)
如何更改主题
将这些行放入您的 email_template_config.php
文件中
'test_template' => [ //... 'Subject' => 'Test Subject', //... ],
如何使用 Google reCAPTCHA?
将此行添加到 .env 文件中(您将需要 reCAPTCHA 秘密)
RECAPTCHA_SECRET=""
将这些行放入您的 email_template_config.php
文件中
'test_template' => [ //... 'recaptcha' => [ 'setExpectedHostname' => ['example.com'], 'setExpectedAction' => ['homepage'], 'setScoreThreshold' => ['0.5'] ], //... ],
之后,将此行放入您的表单中
<div class="g-recaptcha" data-sitekey="{{ gRecaptchaSitekey }}"></div>
如果 reCAPTCHA 无法验证用户怎么办?
页面将返回您到电子邮件表单,但您可以按以下方式检索 reCAPTCHA 错误代码
{% set recaptchaErrorCodeString = craft.app.session.getFlash('recaptchaErrorCodes',null, true) $}
错误代码由竖线分隔,例如:missing-input-secret|missing-input-response
在 twig 2 和 3 中,您可以使用此方式分割它:https://twig.symfony.com.cn/doc/3.x/filters/split.html
{% set recaptchaErrorCodes = recaptchaErrorCodeString|split('|') %}
所有可能的reCaptcha错误代码列表:https://developers.google.com/recaptcha/docs/verify#error_code_reference
如何生成联系表单?
<form action="/email/send" method="post"> {# necessary fields #} {{ csrfInput() }} {{ redirectInput('/') }} {{ craft.email.templateInput('test_template') | raw }} {# custom fields #} <label for="">What's your name?</label> <input name="name" type="text"> <label for="">Which job are you applying for?</label> <input name="job" type="text"> <label for="">What's your email address</label> <input name="email" type="text"> <button>Send</button> </form>
如何生成电子邮件模板?
<p>You've received a new job application:</p> <p>Name: {{ name }}</p> <p>Job in question: {{ job }}</p> <p>Applicant's email address: {{ email }}</p>
如何使用sendmail(而不是SMTP)?
进入.env
文件,将delivery_protocol行复制到这里
MAILER_DELIVERY_PROTOCOL=sendmail
如何发送静态文件
在你的email_template_config.php
文件中
'addAttachment' => [ Craft::getAlias('@webroot/photo.jpg') ],
@webroot
别名指向你的/web文件夹
@webroot
提供绝对文件系统路径,而@web
只提供相对网络路径
如何从表单发送文件?
<label for="">File 1</label> <input type="file" name="attachments[]"> <label for="">File 2</label> <input type="file" name="attachments[]">
我该如何发送自动回复邮件?
自动回复将发送给填写了你的联系表单的人
将这些行粘贴到你的email_template_config.php
文件中,在你想添加自动回复的模板内
'test_template' => [ //... 'replyConfig' => [ 'setFrom' => [ //$mail->setFrom('from@example.com', 'From'); ['from@example.com', 'From'] ], //The name of the input field that stores the mail address to which the mail will go 'setReplyInputName' => ['applicantEmailAddress'], //The name of the template, put it here: templates/_mails/ 'setReplyTemplate' => ['test_reply_template'], ], //... ]
在联系表单上
<label for="">My email address</label> <input name="applicantEmailAddress" type="text">