2
votes

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...

2

2 Answers

2
votes

Yes - any emails sent using the SilverStripe Email class can be redirected for testing purposes:

# File: conf/ConfigureFromEnv.php
if(defined('SS_SEND_ALL_EMAILS_TO')) {
    Config::inst()->update("Email","send_all_emails_to", SS_SEND_ALL_EMAILS_TO);
}

Reference

Then this is used to override email destinations here:

# File email/Email.php
public function send($messageID = null) {
    // ...
    if($sendAllTo = $this->config()->send_all_emails_to) {
        $subject .= " [addressed to $to";
        $to = $sendAllTo;
        // ...

Reference

So to utilise this, define SS_SEND_ALL_EMAILS_TO in your environment configuration:

# File: _ss_environment.php
define('SS_SEND_ALL_EMAILS_TO', '[email protected]');
1
votes

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: