Email contact form with success message but no email received

I’m getting a success message but no email received. Tried both locally and on my server.

Do I need to setup a “transport configuration”? https://getkirby.com/docs/guide/emails

try {
  $kirby->email([
    'template' => 'email',
    'from'     => 'MYEMAIL',
    'replyTo'  => $data['email'],
    'to'       => 'MYEMAIL',
    'subject'  => esc($data['name']) . ' sent you a message from your contact form.',
    'data'     => [
      'text'   => esc($data['text']),
      'sender' => esc($data['name'])
    ]
  ]);
}

The problem is often that if you send email from your local server, it never arrives because rejected by the receiving mail server.

I suggest you set up something like Mailhog for development.

But even on my server I don’t receive anything. Do I need to setup anything on that side as well?

I don’t quite understand?

I’ve tried the form on the web, not just locally on my computer. Same result.

Very often, such mail arrives ages later in a bulk. Have you also checked the spam folder?

Are you on a Mac? Then you can try to log what is happening like I suggested here: Kirby Mail - Sending to multiple recipients

However, I suggest you install MailHog or a similar software locally for testing. It has the additional advantage that your IP doesn’t get blocked for sending Spam.

Instead of sending through your own server, you could also try with a mail service.

1 Like

Ah just arrived in the spam folder now. So you suggest using a service like SendGrid to improve deliverability?

Yes, while this is no guarantee, I think it’s a much better option if you want to make sure your mail arrives.

1 Like

I’m having a similar issue, but in reverse.

When I test my email form on my local environment I receive the email instantly but after uploading it to a live environment I don’t receive any emails but get the success message. (I’ve checked my spam and its been two days now). Do I need to set the transport config ?

For reference I’ve followed the structure on the Kirby example

The transport config only needs to be set if you are using SMTP and would then need your SMTP-settings. Maybe you need to configure that for your hosting provider, that might be. The standard way of sending mail would be through PHP mail but that must then be enabled on the server. Check out your hosting providers documentation or contact their support.

Thanks, I’ll check with the hosting provider

I’ve spoken to my hosting provider and PHP mail is enabled so I’m not sure where the problem is. The code in my controller is:

<?php
return function($kirby, $pages, $page) {

    $alert = null;

    if($kirby->request()->is('POST') && get('submit')) {

        if(empty(get('website')) === false) {
            go($page->url());
            exit;
        }

        $data = [
            'name'  => get('name'),
            'company' => get('company'),
            'email' => get('email'),
            'tel' => get('tel'),
            'text'  => get('text')
        ];

        $rules = [
            'name'  => ['required', 'minLength' => 3],
            'company' => ['required', 'minLength' => 3],
            'email' => ['required', 'email'],
            'tel' => ['required', 'minLength' => 3],
            'text'  => ['required', 'minLength' => 3, 'maxLength' => 3000],
        ];

        $messages = [
            'name'  => 'Please enter a valid name',
            'company' => 'Please enter a valid company name',
            'email' => 'Please enter a valid email address',
            'tel' => 'Please enter a valid phone number',
            'text'  => 'Please enter a text between 3 and 3000 characters',
        ];

        if($invalid = invalid($data, $rules, $messages)) {
            $alert = $invalid;

        } else {
            try {
                $kirby->email([
                    'template' => 'email',
                    'from'     => 'noreply@company.test',
                    'replyTo'  => ($data['email']),
                    'to'       => 'name@email.com',
                    'subject'  => esc($data['name']) . ' sent you a message from your contact form',
                    'data'     => [
                        'name'  => esc($data['name']),
                        'company' => esc($data['company']),
                        'email' => esc($data['email']),
                        'tel' => esc($data['tel']),
                        'text'   => esc($data['text']),
                    ]
                ]);

            } catch (Exception $error) {
                if(option('debug')):
                    $alert['error'] = 'The form could not be sent: <strong>' . $error->getMessage() . '</strong>';
                else:
                    $alert['error'] = 'The form could not be sent!';
                endif;
            }

            if (empty($alert) === true) {
                $success = 'Your message has been sent, thank you. We will get back to you soon!';
                $data = [];
            }
        }
    }

    return [
        'alert'   => $alert,
        'data'    => $data ?? false,
        'success' => $success ?? false
    ];
};

Have you also checked your trash folder?

It’s definitely a problem with PHP mail that it often doesn’t arrive and hence using an SMTP service with a SPF DNS record and whatever other email settings there are to verify your email and make sure it’s not regarded as spam.