bitandblack / contact-form
Requires
- php: >=7.4
- ext-curl: *
- ext-json: *
- ext-openssl: *
- bitandblack/helpers: ^1.0 || ^2.0
- phpmailer/phpmailer: ^6.0
- psr/log: ^1.0 || ^2.0 || ^3.0
- symfony/console: ^5.0 || ^6.0 || ^7.0
Requires (Dev)
- ext-dom: *
- monolog/monolog: ^2.0 || ^3.0
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^9.0
- rector/rector: ^0
- symplify/easy-coding-standard: ^12.0
This package is auto-updated.
Last update: 2024-09-12 15:36:04 UTC
README
联系表单
使您的联系表单更加安全。
它是用来做什么的?
联系表单脚本使您的联系表单更安全。它
- 检查表单是否在5秒内提交,并阻止提交
- 一个会话只能提交一次
- 为垃圾邮件机器人添加蜜罐
- 检查IP或电子邮件地址是否已知为不良
安装
此库是为与 Composer 一起使用而制作的。通过运行 $ composer require bitandblack/contact-form
将其添加到您的项目中。
使用
设置您的SMTP连接
<?php
use ContactForm\Configuration\SMTP;
$smtp = new SMTP('709fc8b0135c1d', '9dbd8257be31c9', 'smtp.mailtrap.io', 2525);
设置表单并告诉名称空间以及名称和电子邮件地址的字段名称
<?php
use ContactForm\Configuration\Form;
$form = new Form('contact', 'name', 'email');
然后初始化联系表单
<?php
use ContactForm\ContactForm;
$contactForm = new ContactForm($form, $smtp);
添加验证提交、初始化邮件和发送表单的逻辑
<?php
use ContactForm\Configuration\Mail;
$mailSentInCurrentSubmit = false;
$mailSentInPreviousSubmit = false;
if ($contactForm->isSubmitValid($_POST)) {
$mail = new Mail(
'New message from your website',
'contact@yourdomain.com',
'There is a message for you: '.$form->getData('message')
);
$mail
->setReplyToName($form->getName())
->setReplyToMail($form->getEmail())
;
$contactForm->addMail($mail);
$mailSentInCurrentSubmit = $contactForm->sendMail();
}
$mailSentInPreviousSubmit = $contactForm->hasSentMailPreviously();
可以在此处添加多个邮件。
通过调用 $contactForm->getAdditionalFields()
将附加字段添加到您的表单中。例如
<form action="" method="post">
<input type="text" name="contact[name]" title="Your Name" placeholder="John Doe">
<input type="email" name="contact[email]" title="Your Email Address" placeholder="john.doe@online.de">
<textarea name="contact[message]" title="Your Message" placeholder="Dear Sir or Madame, ..."></textarea>
<button type="submit">Send</button>
<?php echo $contactForm->getAdditionalFields(); ?>
</form>
添加一条语句以在表单顶部显示一条消息
<?php
if ($mailSentInCurrentSubmit || $mailSentInPreviousSubmit) {
echo '
<p>
Your mail has been sent.
</p>
';
}
如果您想查看整个脚本,请查看 example
文件夹。
邮件发送器
联系表单默认使用 PHPMailer。您可以通过使用 $contactForm->setMailer(new MyCustomMailer())
来设置自己的邮件发送器。邮件发送器需要实现 MailerInterface
。当发送邮件时,联系表单将克隆此对象以确保始终具有相同的配置。
外部垃圾邮件验证
联系表单通过 StopFormSpam API 提供IP和电子邮件地址的验证。要启用此功能,您需要调用 $contactForm->addSpamValidation(new \ContactForm\Validate\StopForumSpam())
。您也可以以相同的方式设置自己的验证。每个验证对象都需要实现 ValidationInterface
。
帮助
如果您对联系表单的使用有疑问,请随时通过 hello@bitandblack.com
联系我们。
有关Bit&Black的更多信息,请访问 www.bitandblack.com。