John Davidson

php - How to locally send an email to a file for localhost testing (wordpress)

0 comments
Message:


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 = "[email protected]";
$subject = 'my subject';
$message = 'I would like to work with you';
$headers[] = 'From: Ben<[email protected]>';


$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...



My Answer:
To locally send an email to a file for localhost testing in WordPress, you can use the following steps:

1. Create a custom function in your theme's functions.php file to save the email content to a file instead of sending it. Here's an example of how you can do this:

php
function save_email_to_file($to, $subject, $message, $headers) {
$email_data = "To: " . $to . "\n";
$email_data .= "Subject: " . $subject . "\n";
$email_data .= "Message: " . $message . "\n";
$email_data .= "Headers: " . print_r($headers, true) . "\n\n";

$file_path = get_template_directory() . '/emails/' . time() . '.txt';
file_put_contents($file_path, $email_data);
}


2. Modify your email sending code to call the `save_email_to_file` function instead of using `wp_mail`. Here's an example:

php
$to = "example@example.com";
$subject = 'Test email';
$message = 'This is a test email message';
$headers = array('From: Your Name ');

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


3. After sending the email, you can check the `emails` folder inside your theme directory to view the saved email files.

Please make sure to create the `emails` folder inside your theme directory before using this code. Also, remember to remove or comment out this custom function once you are done testing and ready to send real emails.

Rate this post

5 of 5 based on 5080 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us