0

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 = 'mytestemail@gmail.com';

$subject = 'Test email';

$message = 'Test';

$headers = "From: The Sender name <myemailaddress@gmail.com>";

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

?>
Greysus
  • 571
  • 1
  • 7
  • 10
  • [How to Configure Your WordPress Email Settings (The RIGHT Way)](https://wpforms.com/how-to-configure-your-wordpress-email-settings-the-right-way/) – Phil Mar 29 '22 at 03:56
  • 1
    You don't even check the return value of `mail()` and most likely haven't read [the documentation](https://www.php.net/manual/en/function.mail.php) either. Did you even [search existing Q&As](https://stackoverflow.com/questions/tagged/email+php) that could have led you to [How can I get the error message for the mail() function?](https://stackoverflow.com/q/3186725/4299358) – AmigoJack Mar 29 '22 at 04:55

1 Answers1

2

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

cam
  • 3,179
  • 1
  • 12
  • 15
  • 2
    Depends: [XAMPP](https://www.apachefriends.org/) always came with [mailtodisk](https://stackoverflow.com/search?q=%5Bxampp%5D+mailtodisk) for this purpose. – AmigoJack Mar 29 '22 at 05:04