duanhan8757 2017-04-05 21:20
浏览 71
已采纳

从本地服务器测试SilverStripe电子邮件联系表单

I am building a contact form which sends an email upon submission using a combination of approaches provided in SilverStripe documentation (Introduction to frontend forms, and Simple contact form).

The code (included below) seems to be working fine, however I am wondering if it is possible to send test emails from a local server (in my case, MAMP) to properly test the functionality.

Many thanks in advance

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('email@email.com');
        $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();
    }

Edit: 8/04/17

As per have updated the sendmail fields in the php.ini files in MAMP (both in MAMP/bin/php/php5.6.10/conf and MAMP/conf/php5.6.10) to:

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path =/usr/sbin/sendmail -t -i -f  ryanachten@gmail.com

As per have updated the mysite/_config/config.yml to include:

Email:
  send_all_emails_to: 'ryanachten@gmail.com'

All to no avail...

  • 写回答

2条回答 默认 最新

  • doucong7963 2017-04-10 03:03
    关注

    In addition to making the _ss_environment changes suggested above, the true barrier for testing email delivery in my case was allowing less secure apps to communicate with the email account used for testing.

    I was initially using Gmail for this, which doesn’t allow less secure apps to be enabled for accounts with 2-step authentication. I could have changed this security feature, but instead opted to use a secondary, Yahoo email address for these trials.

    Note: this answer is only intended to be applicable to the testing nature of this question (making sure the email functionality is executing appropriately); further steps will be made later in development to qualify as a secure email app.

    Hopes this helps others in a similar situation.

    Resources:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示