Craft CMS工具缺失邮件选项:新手校园项目邮件发送求助
无插件实现Craft CMS订阅邮件发送方案
一、前端订阅表单实现
在你的Twig模板里添加带验证的订阅表单,用POST方式提交:
<form method="post" accept-charset="UTF-8"> {{ csrfInput() }} <input type="hidden" name="action" value="template-actions/send-subscribe-email"> <div> <label for="subscriberEmail">邮箱地址</label> <input type="email" id="subscriberEmail" name="email" required placeholder="输入你的邮箱"> </div> <button type="submit">订阅新闻简报</button> </form>
二、模板内触发邮件发送逻辑
在同一个模板的表单上方添加提交后的处理代码,这就是邮件发送的触发位置:
{% if craft.request.isPost and craft.request.getParam('action') == 'template-actions/send-subscribe-email' %} {% set subscriberEmail = craft.request.getParam('email') %} {% if subscriberEmail %} {# 调用Craft核心自带的邮件发送方法 #} {% set sendResult = craft.mailer.sendMail({ from: '你的校园项目邮箱@xxx.com', to: subscriberEmail, subject: '订阅成功!欢迎加入校园新闻简报', htmlBody: '<p>感谢你的订阅,我们会定期为你推送最新校园资讯。</p>' }) %} {# 给用户反馈操作结果 #} {% if sendResult %} <div class="alert-success">订阅成功!确认邮件已发送到你的邮箱~</div> {% else %} <div class="alert-error">订阅失败,请稍后再试。</div> {% endif %} {% endif %} {% endif %}
craft.mailer.sendMail()是Craft原生自带的方法,完全不需要插件,只要SMTP配置正确就能直接运行。
三、关于「系统消息找不到」的说明
官方文档里提到的系统消息是Craft Pro版专属功能,免费版中确实没有该选项,不用纠结这个,上面的自定义方案完全能满足你的订阅需求。
四、确认Gmail SMTP配置
确保你在Craft后台「设置→邮件」里的配置正确:
- 传输方式:SMTP
- 主机:smtp.gmail.com
- 端口:587
- 加密方式:TLS
- 用户名:你的Gmail邮箱地址
- 密码:如果开启了两步验证,必须使用Gmail应用专用密码,不能用普通登录密码
内容的提问来源于stack exchange,提问作者Stef Verniers
相关产品推荐
相关产品推荐

