我正在构建一个联系表单,该表单在提交后会使用 SilverStripe 文档中提供的方法组合( Introduction to frontend forms 和 Simple contact form )发送电子邮件。
代码(包含在下面)似乎工作正常,但是我想知道是否可以从本地服务器(在我的例子中为 MAMP)发送测试电子邮件以正确测试功能。
提前非常感谢
public function ContactForm() {
$form = Form::create(
$this,
__FUNCTION__,
FieldList::create(
TextField::create('Name','')
->setAttribute('placeholder','Name*')
->addExtraClass('form-field'),
EmailField::create('Email','')
->setAttribute('placeholder','Email*')
->addExtraClass('form-field'),
TextareaField::create('Message','')
->setAttribute('placeholder','Message*')
->addExtraClass('form-field')
),
FieldList::create(
FormAction::create('submit', 'Send Message')
->setUseButtonTag(true)
->addExtraClass('text-button')
),
RequiredFields::create('Name','Email','Message')
);
$form->addExtraClass('contact-form');
return $form;
}
public function submit($data, $form){
$email = new Email();
$email->setTo('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fc99919d9590bc99919d9590d29f9391" rel="noreferrer noopener nofollow">[email protected]</a>');
$email->setFrom($data['Email']);
$email->setSubject("Contact Message from {$data["Name"]}");
$messageBody = "
<p><strong>Name:</strong> {$data['Name']}</p>
<p><strong>Message:</strong> {$data['Message']}</p>
";
$email->setBody($messageBody);
$email->send();
$form->sessionMessage("Thanks for your message, I'll get back to you as soon as I can",'good');
return $this->redirectBack();
}
编辑:2017 年 8 月 4 日
由于 per 已将 MAMP 中的 php.ini 文件(均在 MAMP/bin/php/php5.6.10/conf 和 MAMP/conf/php5.6.10 中)中的 sendmail 字段更新为:
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path =/usr/sbin/sendmail -t -i -f <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="05777c646b64666d71606b456268646c692b666a68" rel="noreferrer noopener nofollow">[email protected]</a>
per 已更新 mysite/_config/config.yml 以包含:
Email:
send_all_emails_to: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0b2b9a1aea1a3a8b4a5ae80a7ada1a9aceea3afad" rel="noreferrer noopener nofollow">[email protected]</a>'
一切都无济于事...
最佳答案
是 - 使用 SilverStripe Email 类发送的任何电子邮件都可以重定向以用于测试目的:
# File: conf/ConfigureFromEnv.php
if(defined('SS_SEND_ALL_EMAILS_TO')) {
Config::inst()->update("Email","send_all_emails_to", SS_SEND_ALL_EMAILS_TO);
}
然后,这用于覆盖此处的电子邮件目的地:
# File email/Email.php
public function send($messageID = null) {
// ...
if($sendAllTo = $this->config()->send_all_emails_to) {
$subject .= " [addressed to $to";
$to = $sendAllTo;
// ...
因此,要利用此功能,请在您的环境配置中定义 SS_SEND_ALL_EMAILS_TO
:
# File: _ss_environment.php
define('SS_SEND_ALL_EMAILS_TO', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1e67716b6c5e7b737f7772307d7173" rel="noreferrer noopener nofollow">[email protected]</a>');
关于php - 从本地服务器测试 SilverStripe 电子邮件联系表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43241833/