'How to send an email in Wordpress using PHP [duplicate]

For starters, I downloaded a Wordpress plugin that allows you to insert PHP snippets into a Wordpress site. I added this code hoping that it will send an email but I have not receeved anything yet. I have used two different snippet plugins, and I am not sure what could be causing the issue, or if there would be an easier way to go about doing this.

<?php

$to = '[email protected]';

$subject = 'Test email';

$message = 'Test';

$headers = "From: The Sender name <[email protected]>";

mail($to, $subject, $message, $headers); 

?>


Solution 1:[1]

You can use wp_mail. The good thing about wp_mail is that it uses whatever the default mail delivery service is configured in WordPress.

wp_mail( $to, $subject, $message, $headers );

It's worth noting that the default PHP mailer is unlikely to work when running on your local machine.

I recommend setting up a mail plugin for WordPress that uses a third-party mail service like Mailgun to send emails. I use WP Mail SMTP

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1