Avoid emails going to spam, using smtp and own sender account

I have smtp configured in a server, in config, like:

    'email' => [
        'transport' => [
            'type' => 'smtp',
            'host' => 'xxxxx.yyyyyyy.zz',
            'port' => 465,
            'security' => true,
            'auth' => true,
            'username' => 'bot@site.com',
            'password' => '1234567890',                
        ]

Then, in a controller, I am using this code to send emails:

if ($kirby->request()->is('POST')) {
    $name = get('your-name');
    $email = get('your-email');
    $message = get('your-message');

    // Send email
    if (site()->contactemail()->isNotEmpty()) {
        try {
            kirby()->email([
                'from' => strval($email),
                'to' => strval(site()->contactemail()),
                'subject' => 'Contact from ' . $name,
                'template' => 'contact',
                'data' => [
                    'name' => $name,
                    'email' => $email,
                    'message' => $message
                ]
            ]);
            $submitted = true;
        } catch (Exception $e) {
           // etc
        }
    }       
}

bot@site.com and site()->contactemail() do NOT belong to the same server/company/hosting plan… the second is a standalone gmail account.

As you see I use $email as sender in ‘from’ field, so the sender email is always changing, which is contributing to emails being labeled as spam, because the client cannot whitelist the sender.

If, instead, I use bot@… as sender in ‘from’ field the client CAN whitelist that account, but then all emails are grouped in a single thread, and client does not like that (me neither).

Is there anything PHP I can add to the email code that allows me to ensure emails will not be sent to spam in gmail ? Any combination of fields that would allow the email to be identified as coming from bot@… while keeping the $email visible and remaining in separate threads, etc ?

As I understand I could do something like:

'from' => ['bot@site.com', $email]

…which would solve half of the problem (shown name) but not the other half (thread grouping), right?

Google suggests to Send each message with a unique reference header value that doesn’t match a previous message. How to achieve this with $kirby->email() ?

I’ve cheked in gmail, and it does not seem to be able to whitelist by server, btw, at least not for regular accounts.

Thank you!

After some testing, we found out that using:

'from' => ['bot@site.com', $name],

…does seem to prevent Gmail from grouping the emails in threads, but you may possibly need to provide an additional different email subject every time, too.

I would still be interested in understanding what a ‘reference header value’ is and how to use it, set it in Kirby, in relation to Google’s suggestion to Send each message with a unique reference header value that doesn’t match a previous message.

Thanks

You might try this:

if ($kirby->request()->is('POST')) {
    $name = get('your-name');
    $email = get('your-email');
    $message = get('your-message');

    // Send email
    if (site()->contactemail()->isNotEmpty()) {
        try {
            kirby()->email([
                'from' => strval($email),
                'to' => strval(site()->contactemail()),
                'subject' => 'Contact from ' . $name,
                'template' => 'contact',
                'data' => [
                    'name' => $name,
                    'email' => $email,
                    'message' => $message
                ]
                'beforeSend' => function ($mailer) {
                    $message_id = bin2hex(random_bytes(24));
                    $mailer->addCustomHeader('References', "<$message_id@site.com>");
                    return $mailer;
                }
            ]);
            $submitted = true;
        } catch (Exception $e) {
           // etc
        }
    }       
}

In theory this should prevent threading, at least on gmail. Other email clients might use other heuristics.

1 Like

Thank you!