E
E
Eugene Ordinary2016-09-30 11:37:06
PHP
Eugene Ordinary, 2016-09-30 11:37:06

How to make a stub for mail on localhost?

Created mail.php file

<?php
$str = date( 'Y-m-d H:i:s' )."\r\n";
$str .= file_get_contents( 'php://stdin' )."\r\n\r\n";
file_put_contents( __DIR__.'/mail_log.txt', $str, FILE_APPEND );
?>

In php.ini, in the sendmail_path parameter, I specified the path to mail.php, restarted Apache, but nothing works. An application script that successfully sends mail on a third-party server now hangs for a long time on a local server.
If mail.php is called directly from the browser, then everything works out, writes an empty entry with a timestamp to the log file.
UPD.
Found a solution.
sendmail_path = "path_to_php.exe path_to_mail.php"
But why do you need to do this? Is this an indication to process mail.php via php.exe?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
romy4, 2016-09-30
@romy4

I use Mailslurper for such purposes. everything is beautiful and cool

D
Dmitry, 2016-09-30
@slo_nik

Good morning.
I did not find a link to the blog where I found this solution, but here is the script and some description for you.
I use ubuntu, but I think that if you have windows, you can still run this stub. Perhaps tweaking the script a bit.

#!/usr/bin/php
<?php
/* заглушка mail() */
$input = file_get_contents('php://stdin');
preg_match('|^To: (.*)|', $input, $matches);
//$t = tempnam("/home/username/localhost/email", $matches[1]);
//
// более понятно название файла + возможность открывать в почтовой программе
$t = "/home/username/localhost/email/" . $matches[1] . '_' . time() . '.eml';

chmod($t, 0644);
file_put_contents($t, $input);
?>

I placed this file in the home directory of the system, in a hidden directory. Set permissions to execute the file as a program.
In /etc/php5/cli/php.ini and /etc/php5/apache2/php.ini set the path to the file.
In the localhost directory, I created the email directory with access rights 0777
That's it, now all letters are added to this directory and opened by the mail client.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question