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 protected]');
$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 [email protected]
As per have updated the mysite/_config/config.yml to include:
Email:
send_all_emails_to: '[email protected]'
All to no avail...