0

I have Wordpress installed locally with Xampp. For testing purposes, I need to be able to send and receive transactional emails. I've spent 3 days trying the get my local server to send emails (and trying the plugins Wp Mail SMTP as well as Sendinblue), without any sucess. I've heard emails from localhost are often blocked by email providers and notoriously unreliable.

It would greatly simplify my life if I could just save an email locally, by sending it to a file, without needing to send it to a real webmail.

Any advice? (by the way, I'm working on a Mac, but in any case any advice at all would be greatly appreciated)

Many thanks!

EDIT:

I can't get mailtrap to work either. When I paste their code into functions.php:

function mailtrap($phpmailer) {
  $phpmailer->isSMTP();
  $phpmailer->Host = 'smtp.mailtrap.io';
  $phpmailer->SMTPAuth = true;
  $phpmailer->Port = 2525;
  $phpmailer->Username = 'xxxxxxxxxxxxx';
  $phpmailer->Password = 'xxxxxxxxxxxxx';
}

add_action('phpmailer_init', 'mailtrap');

then I make a PHP file and try to send an email like this:

$to = "bviatte@gmail.com";
   $subject = 'my subject';
   $message = 'I would like to work with you';
   $headers[] = 'From: Ben<bviatte@gmail.com>';


   $sent_message = wp_mail( $to, $subject, $message, $headers);
   var_dump($sent_message); // i get boolean false here.
   if ( $sent_message ) {
       echo 'The test message was sent. Check your email inbox.';
   } else {
       echo 'The message was not sent!'; //this gets printed in d end.
   }
?>

I get a boolean false error message...

Ben Viatte
  • 485
  • 1
  • 5
  • 16
  • 1
    Mailtrap is a great solution to bypass SMTP and just write to disk: https://mailtrap.io/blog/wordpress-smtp-settings/ – Chris Haas Nov 09 '21 at 16:48
  • I forgot to mention, mailtrap is also one of the routes I went down already, again with no sucess on localhost. Upon reading your comment, I'll try again though. Thank you! – Ben Viatte Nov 09 '21 at 16:58

1 Answers1

0

This is definitely a common use case, but good documentation is sparse. It’s actually fairly easy to set up, and you don’t even need to touch any PHP.

The key is to get wp_mail() to use the sendmail binary on your system, which means:

  1. Remove any SMTP plugins you may have installed;
  2. Get a Postfix server (which is fronted by sendmail) running on your machine with configurations including –
myhostname = mail.example.com
inet_interfaces = $myhostname, localhost
mydestination = $myhostname, localhost.$mydomain, localhost
mynetworks_style = host
relay_domains =   # blank
  1. Edit /etc/hosts and add mail.example.com after 127.0.0.1;
  2. Make sure the user in user@mail.example.com you address the email to is a real non-system user on your machine, with a proper home directory.

Now, change WP’s admin address to user@mail.example.com and you’ll receive a confirmation message in user’s home directory’s /Maildir. Or assign user@mail.example.com to receive incoming messages from a contact form and they’ll appear in that user’s inbox.

/var/log/maillog will give you clues in case of misconfiguration.