📜 ⬆️ ⬇️

Sendmail stub for php and php

image

Every time setting up a local web server, I come up with a question about the stub for Sendmail.

There is an excellent solution for Windows: fake sendmail for windows (sendmail.exe) http://glob.com.au/sendmail/
On Habré there is an article about this solution: PHP mail under Windows http://habrahabr.ru/post/26518/
Another Windows solution: Test Mail Server Tool http://www.toolheap.com/test-mail-server-tool/
There is also an article for Linux users: Sendmail-stub for Linux http://habrahabr.ru/post/49665/
')
Personally, I liked the solution in pure PHP more, about which further description will go.


The peculiarity of this solution is that its configuration is minimal, you can even do without the file itself, unless of course you need to know the contents of the message. In my opinion, such a method is worth what other PHP developers would know about it.

The question is, what is your priority as a developer, your needs from stubs, as well as your level of knowledge and skills.

Of the benefits:


Of the minuses:


The list of arguments and possibilities is a stub:


Parameters for PHP.INI file:

[mail function] ;SMTP = localhost ;smtp_port = 25 ;sendmail_from = me@example.com sendmail_path = "php.exe C:\sendmail.php --dir C:\mail --open" 


If the path to php.exe is registered in PATH, then you can omit it in PHP.INI. Otherwise, it is desirable to change php.exe to <Path to folder with PHP> \ php.exe

 sendmail_path = "C:\server\bin\php\php.exe C:\sendmail.php --dir C:\mail --open" 


On Linux, you can immediately specify the path to the file, without forgetting to make it run first: chmod 755 sendmail.php
 sendmail_path = "/home/someuser/sendmail.php --dir /tmp/mail" 


The sendmail.php script itself:

 #!/usr/bin/env php <?php /* PHP.INI * [mail function] * ;SMTP = localhost * ;smtp_port = 25 * ;sendmail_from = me@example.com * sendmail_path = php.exe sendmail.php --dir C:\mail --open */ $is_windows = stristr(PHP_OS, 'WIN'); $options = getopt("", ['open', 'prepend', 'file:', 'dir:']); $is_open = isset($options['open']); $is_prepend = isset($options['prepend']); $is_onefile = isset($options['file']); $mail_dir = isset($options['dir']) ? $options['dir'] : sys_get_temp_dir().'/mail'; $file_name = isset($options['file']) ? $options['file'] : mkname(); $file_path = $mail_dir.'/'.$file_name; if( !is_dir( $mail_dir ) ) { mkdir( $mail_dir, 0777, TRUE ); if( !is_dir( $mail_dir ) ) { die('Mail folder ['.$mail_dir.'] not created'); } } $stream = $is_onefile ? PHP_EOL . str_repeat("-=", 10) . date('Ymd H:i:s') . str_repeat("-=", 10) . PHP_EOL : ''; while (false !== ($line = fgets(STDIN))) { $stream .= ($is_windows ? str_replace("\n", PHP_EOL, $line) : $line); } if($is_prepend && file_exists($file_path)) { $file_contents = file_get_contents($file_path); $stream .= $file_contents; } file_put_contents($file_path, $stream, $is_prepend ? 0 : FILE_APPEND); if ($is_open && $is_windows){ pclose(popen("start /B notepad ". $file_path, "r")); } function mkname($i=0) { global $mail_dir; $fn = 'mail_'.date('Ym-d_H-i-s_').$i.'.txt'; return file_exists($mail_dir.'/'.$fn) ? mkname(++$i) : $fn; } 


As I promised, there is also the possibility to do without a file at all.

 sendmail_path = "C:\server\bin\php\php.exe -r 'echo 1;'" 


Or use a simplified solution

 sendmail_path = "C:\server\bin\php\php.exe C:\sendmail.php" 


sendmail.php:

 #!/usr/bin/env php <?php $mail = ''; while (false !== ($line = fgets(STDIN))) { $mail .= $line; } file_put_contents("c:/mail/mail.txt", $mail . PHP_EOL . str_repeat("-=", 20) . PHP_EOL, FILE_APPEND); 


I would be glad if it would be useful for you.

Source: https://habr.com/ru/post/252607/


All Articles