No results found.

Sending Test Emails in Magento Without Spamming the Customer Database

Testing deliverability (SPF/DKIM/DMARC, spam score) usually means creating a customer with a mail-tester.com address and triggering “forgot password”. Works, but leaves junk rows in customer_entity.

Send the template directly instead, via TransportBuilder in an n98-magerun2 dev:console session. This skips account creation entirely, so the customer table stays clean, and it’s quick enough to re-run for each store view or SMTP change you’re testing.

function email($email, $storeId = 1)
{
    $di = \Magento\Framework\App\ObjectManager::getInstance();
    $state = $di->get(\Magento\Framework\App\State::class);

    try {
        $state->setAreaCode("frontend");
    } catch (\Exception $e) { }

    try {
        $transport = $di->get(\Magento\Framework\Mail\Template\TransportBuilder::class)
            ->setTemplateIdentifier("contact_email_email_template")
            ->setTemplateOptions([
                "area" => "frontend",
                "store" => $storeId
            ])
            ->setTemplateVars([
                "data" => new \Magento\Framework\DataObject([
                    "name" => "Test User",
                    "email" => $email,
                    "comment" => "Test email from n98-magerun2"
                ])
            ])
            ->setFromByScope("general")
            ->addTo($email)
            ->getTransport();

        $transport->sendMessage();
    } catch (\Exception $e) {
        echo "Unable to send message: " . $e->getMessage() . PHP_EOL;
    }

    echo "Sent to {$email}" . PHP_EOL;
}

email('[email protected]');
email('[email protected]', 2);  // Store ID 2

$storeId is optional and defaults to 1, pass a second argument to test other store views since SMTP config and sender addresses can differ by scope.

Paste into dev:console

Open a console session, then paste the function and a call in one go:

n98-magerun2 dev:console
function email($email, $storeId = 1) { $di = \Magento\Framework\App\ObjectManager::getInstance(); $state = $di->get(\Magento\Framework\App\State::class); try { $state->setAreaCode("frontend"); } catch (\Exception $e) { echo "Unable to set Area Code: " . $e->getMessage() . PHP_EOL; } try { $transport = $di->get(\Magento\Framework\Mail\Template\TransportBuilder::class)->setTemplateIdentifier("contact_email_email_template")->setTemplateOptions(["area" => "frontend", "store" => $storeId])->setTemplateVars(["data" => new \Magento\Framework\DataObject(["name" => "Test User", "email" => $email, "comment" => "Test email from n98-magerun2"])])->setFromByScope("general")->addTo($email)->getTransport(); $transport->sendMessage(); } catch (\Exception $e) { echo "Unable to send message: " . $e->getMessage() . PHP_EOL; } echo "Sent to {$email}" . PHP_EOL; }
email("[email protected]");

Run without opening the console

dev:console reads from stdin, so the one-liner above can be piped straight in, no interactive session needed:

echo '$email="[email protected]"; $storeId = 2; $di = \Magento\Framework\App\ObjectManager::getInstance(); $state = $di->get(\Magento\Framework\App\State::class); try { $state->setAreaCode("frontend"); } catch (\Exception $e) { echo "Unable to set Area Code: " . $e->getMessage() . PHP_EOL; } try { $transport = $di->get(\Magento\Framework\Mail\Template\TransportBuilder::class)->setTemplateIdentifier("contact_email_email_template")->setTemplateOptions(["area" => "frontend", "store" => $storeId])->setTemplateVars(["data" => new \Magento\Framework\DataObject(["name" => "Test User", "email" => $email, "comment" => "Test email from n98-magerun2"])])->setFromByScope("general")->addTo($email)->getTransport(); $transport->sendMessage(); } catch (\Exception $e) { echo "Unable to send message: " . $e->getMessage() . PHP_EOL; } echo "Sent to {$email}" . PHP_EOL;' | n98-magerun2 dev:console

Handy for scripting a quick check into a deploy or SMTP config change, without dropping into an interactive shell at all.

Checking the result with mail-tester.com

mail-tester.com gives you a throwaway address, scores whatever lands in it out of 10, and breaks the score down into SPF, DKIM, DMARC, blacklist, and spam-content checks.

  1. Open mail-tester.com and copy the generated test address, it looks like [email protected].
  2. Run the snippet above with that address as $email.
  3. Back on mail-tester.com, click “Then check your score”. The report shows exactly which check failed, so you’re not guessing why mail is landing in spam.

Since this sends through Magento’s real transport and template system, the result reflects what a real customer email would look like, without a real customer record behind it.