Кромешная inurl sendmail php. Тормозит отправка писем в php (sendmail)

Знакомство с php-функцией mail() и простейший пример её использования для отправки писем по электронной почте. Что лучше или Sendmail vs mail.

PHP-функция mail() позволяет вам отправлять письма на указанный адрес электронной почты.

Синтаксис :

Bool mail (string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]]);

Параметры :

  • to — адрес электронной почты получателя письма в соответствии с форматом RFC 2822. Например :
  • subject — тема отправляемого письма в соответствии с форматом RFC 2047
  • message — содержание отправляемого письма. Каждая строка должна быть не длиннее 70 символов и заканчиваться символом новой строки (\n).

    Только для Windows : Когда PHP обращается к SMTP серверу напрямую, точка в начале строки удаляется. Для борьбы с этим нюансом, следует использовать замену на двойную точку в начале строки.

    $message = str_replace ("\n.", "\n..", $message);

  • additional_headers (не обязательно) – строка, которая будет добавлена после заголовков письма. Обычно используется для добавления дополнительных заголовков (From, Cc и Bcc). Строки должны быть разделены CRLF (\r\n).
  • additional_parameters (не обязательно) – дополнительные параметры, используемые программой назначенной для отправки писем.

Возвращает : TRUE если письмо отправлено успешно или FALSE если нет. Следует отметить, если письмо успешно поставлено в очередь на отправление, это не значит, что оно достигнет адресата.

Теперь, когда мы разобрались в теории, можно приступить к практике. Но для начала я хочу привести парочку полезных функций, которые позволят в значительной степени облегчить нашу задачу.

Пример : php-функция MIME base64 кодирования, с учетом кодировки

Function mime_base64($string, $charset="windows-1251") { return "=?" . $charset . "?B?" . base64_encode(trim($string)) . "?="; }

Одна из проблем, возникающих при отправке почты, заключается в формировании заголовка письма. В частности, заголовок, имена отправителя и получателя, написанные кириллицей могут быть распознаны почтовым клиентом получателя в искаженном виде. Для того чтобы избежать этой проблемы, можно воспользоваться MIME base64 кодированием, преобразующее последовательность байт в последовательность печатных ASCII символов.

Function is_email($string) { return preg_match("/^()+([\.a-zA-Z0-9_-])*@()+(\.+)*\.({2,6})$/", $string); }

С эти тоже разобрались. А теперь то, ради чего и писалась данная заметка.

Пример : отправка писем с помощью php-функции mail()

$charset = "windows-1251"; $from = mime_base64("Имя отправитель", $charset) . " "; $to = mime_base64("Имя получитель", $charset) . " "; $subject = mime_base64("Тема отправляемого письма", $charset); $message = "Содержание отправляемого письма."; $message = wordwrap($message, 70); $headers = "From: " . $from . "\r\n" . "Reply-To: " . $from . "\r\n" . "X-Mailer: PHP/" . phpversion(). "\r\n" . "MIME-Version: 1.0" . "\r\n" . "Content-type: text/plain; charset="" . $charset . """; if (mail($to, $subject, $message, $headers)) { echo "

Письмо отправлено.

"; } else { echo "

Произошла ошибка.

"; }

Думаю, что комментарии излишни, так как я и без того уделил достаточно много внимание различным деталям. Варианты набора RFC-заголовков могут отличаться, но на локальном сервере я получил следующее:

X-Sendmail-Cmdline: sendmail.pl -t -i To: =?windows-1251?B?yOz/IO/u6/P36PLl6/w=?= Subject: =?windows-1251?B?0uXs4CDu8u/w4OLr/+Xs7uPuIO/o8fzs4A==?= From: =?windows-1251?B?yOz/IO7y7/Dg4ujy5ev8?= Reply-To: =?windows-1251?B?yOz/IO7y7/Dg4ujy5ev8?= X-Mailer: PHP/5.3.3 MIME-Version: 1.0 Content-type: text/plain; charset="windows-1251"

Sendmail vs php mail()

Напоследок небольшой ликбез. Sendmail является одним из почтовых серверов (MTA), в то время как php-функция mail() просто реализует процесс отправки писем через SMTP соединение. Понятно, что использование php-функции является удобным, но далеко не оптимальным решением. Обычно mail() используется в тех случаях, когда речь не заходит о масштабных рассылках.

Каждый раз настраивая локальный вебсервер, сталкиваюсь с вопросом о заглушке для Sendmail.

Есть прекрасное решение для Windows: fake sendmail for windows (sendmail.exe) http://glob.com.au/sendmail/
На хабре есть статья об этом решении: PHP mail под Windows http://habrahabr.ru/post/26518/
Ещё одно решение под Windows: Test Mail Server Tool http://www.toolheap.com/test-mail-server-tool/
Так же есть статья для линуксоидов: Sendmail-заглушка для Linux http://habrahabr.ru/post/49665/

Лично мне больше понравилось решение на чистом PHP, о котором и пойдёт дальнейшее описание.

Из плюсов:

  • Отсутствие сторонних сервисов
  • Кроссплатформенное решение для OS на которой установлен PHP
  • Минимальный размер файла заглушки
  • Вся логика заглушки доступна для редактирования на PHP
  • Сохранение сообщения в файле

Из минусов:

  • Отсутствие пересылки на реальный почтовый ящик

Список аргументов и возможностей заглушки:

  • --dir <Путь к папке> - Папка для файлов
  • --file <имя файла> - Сохранять каждое сообщение в конкретном общем файле
  • --prepend - Добавлять новое сообщение в начало общего файла
  • --open - Автоматически открывать файл сообщения в Notepad

Параметры для PHP.INI файла:

;SMTP = localhost ;smtp_port = 25 ;sendmail_from = [email protected] sendmail_path = "php.exe C:sendmail.php --dir C:mail --open"

Если путь к php.exe прописан в PATH, то можно не указывать его в PHP.INI иначе желательно изменить php.exe на <Путь к папке с PHP>php.exe

Sendmail_path = "C:serverbinphpphp.exe C:sendmail.php --dir C:mail --open"

На линукс можно сразу указать путь к файлу, не забыв предварительно сделать его запускаемым: chmod 755 sendmail.php

Sendmail_path = "/home/someuser/sendmail.php --dir /tmp/mail"

Сам sendmail.php скрипт:

#!/usr/local/bin/php

Буду рад, если Вам это будет полезным.

Каждый раз настраивая локальный вебсервер, сталкиваюсь с вопросом о заглушке для Sendmail.

Есть прекрасное решение для Windows: fake sendmail for windows (sendmail.exe) http://glob.com.au/sendmail/
На хабре есть статья об этом решении: PHP mail под Windows http://habrahabr.ru/post/26518/
Ещё одно решение под Windows: Test Mail Server Tool http://www.toolheap.com/test-mail-server-tool/
Так же есть статья для линуксоидов: Sendmail-заглушка для Linux http://habrahabr.ru/post/49665/

Лично мне больше понравилось решение на чистом PHP, о котором и пойдёт дальнейшее описание.

Особенность этого решения в том, что для его настройка минимальна, можно даже обойтись без самого файла, если конечно вам нет нужды знать содержимое сообщения. На мой взгляд, такой метод стоит того, что бы о нём знали другие разработчики PHP.

Вопрос в том, что является вашим приоритетом как разработчика, ваши потребности от заглушки, а также ваш уровень знаний и умений.

Из плюсов:

  • Отсутствие сторонних сервисов
  • Кроссплатформенное решение для OS на которой установлен PHP
  • Минимальный размер файла заглушки
  • Минимальная настройка
  • Вся логика заглушки доступна для редактирования на PHP
  • Сохранение сообщения в файле

Из минусов:

  • Отсутствие пересылки на реальный почтовый ящик
  • Отсутствие удобного доступа к отправленным сообщениям

Список аргументов и возможностей заглушки:

  • --dir <Путь к папке> - Папка для файлов
  • --file <имя файла> - Сохранять каждое сообщение в конкретном общем файле
  • --prepend - Добавлять новое сообщение в начало общего файла
  • --open - Автоматически открывать файл сообщения в Notepad

Параметры для PHP.INI файла:

;SMTP = localhost ;smtp_port = 25 ;sendmail_from = [email protected] sendmail_path = "php.exe C:\sendmail.php --dir C:\mail --open"

Если путь к php.exe прописан в PATH, то можно не указывать его в PHP.INI иначе желательно изменить php.exe на <Путь к папке с PHP>\php.exe

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

На линукс можно сразу указать путь к файлу, не забыв предварительно сделать его запускаемым: chmod 755 sendmail.php
sendmail_path = "/home/someuser/sendmail.php --dir /tmp/mail"

Сам sendmail.php скрипт:

#!/usr/bin/env php

Как я и обещал, есть также возможность обойтись совсем без файла.

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

Либо использовать упрощённый вариант решения

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

sendmail.php:

#!/usr/bin/env php

Буду рад, если Вам это будет полезным.

The behaviour of these functions is affected by settings in php.ini .

Mail configuration options
Name Default Changeable Changelog
mail.add_x_header "0" PHP_INI_PERDIR Available since PHP 5.3.0.
mail.log NULL PHP_INI_PERDIR Available since PHP 5.3.0. (PHP_INI_SYSTEM|PHP_INI_PERDIR)
mail.force_extra_parameters NULL PHP_INI_PERDIR Available since PHP 5.0.0. (PHP_INI_SYSTEM|PHP_INI_PERDIR)
SMTP "localhost" PHP_INI_ALL
smtp_port "25" PHP_INI_ALL
sendmail_from NULL PHP_INI_ALL
sendmail_path "/usr/sbin/sendmail -t -i" PHP_INI_SYSTEM
For further details and definitions of the PHP_INI_* modes, see the .

Here"s a short explanation of the configuration directives.

Add X-PHP-Originating-Script that will include UID of the script followed by the filename.

Mail.log string

The path to a log file that will log all mail() calls. Log entries include the full path of the script, line number, To address and headers.

Mail.force_extra_parameters string

Force the addition of the specified parameters to be passed as extra parameters to the sendmail binary. These parameters will always replace the value of the 5th parameter to mail() , even in safe mode.

SMTP string

Used under Windows only: host name or IP address of the SMTP server PHP should use for mail sent with the mail() function.

Smtp_port int

Used under Windows only: Number of the port to connect to the server specified with the SMTP setting when sending mail with mail() ; defaults to 25.

Sendmail_from string

Which "From:" mail address should be used in mail sent from PHP under Windows. This directive also sets the "Return-Path:" header.

Sendmail_path string

Where the sendmail program can be found, usually /usr/sbin/sendmail or /usr/lib/sendmail . configure does an honest attempt of locating this one for you and set a default, but if it fails, you can set it here.

Systems not using sendmail should set this directive to the sendmail wrapper/replacement their mail system offers, if any. For example, » Qmail users can normally set it to /var/qmail/bin/sendmail or /var/qmail/bin/qmail-inject .

qmail-inject does not require any option to process mail correctly.

This directive works also under Windows. If set, smtp , smtp_port and sendmail_from are ignored and the specified command is executed.

5 years ago

On Ubuntu 13.04, not sure of the other Distros.

If you simply uncomment the default:

sendmail_path = "sendmail -t -i"

Your mail() functions will all fail. This is because, you should place the FULL PATH (i.e. /usr/sbin/sendmail -t -i)

The documentation states PHP tries it"s best to find the correct sendmail path, but it clearly failed for me.

So, always enter in the FULLPATH to sendmail or you may get unexpected failing results.

As a secondary note: Those that just want to ENFORCE the -f parameter, you can do so in php.ini using:

mail.force_extra_parameters = [email protected]

You can leave the sendmail path commented out, it will still use the defaults (under UNIX -t -i options which if you look them up are very important to have set)....

But, now there is no way to change this, even with the 5th argument of the mail() function. -f is important, because if NOT set, will be set to which ever user the PHP script is running under, and you may not want that.

Also, -f sets the Return-Path: header which is used as the Bounce address, if errors occur, so you can process them. You you can not set Return-Path: in mail() headers for some reason... you could before. Now you have to use the -f option.

2 years ago

If anyone gets this cryptic error message in the PHP error logs:
"sh: -t: command not found"
after upgrading from PHP 5.4, this may be the solution for you.

I upgraded PHP from 5.4 to 5.6 and all our mail() functionality suddenly broke, with no useful error logging.

Ini file; it is now ignored if you use ini_set() to specify a path to the sendmail binary on the fly.

So, just specify "sendmail_path" in php.ini instead. That"s all there is to it -- that fixed all the mail() functionality for us.

Hope this little note saves someone else as much time as I spent troubleshooting and researching. Cheers!

3 years ago

Setting return path:

Return-path header does not work because it is not part of smtp.

On *nix -f may be set on the mail command
alternatively
some MTAs accept mbox-style "From " header on the first header line
note: no colon after "From" , must be the first header line
or you may be able to re-configure you MTA to interpret return-path headers

F is the most reliable method but it may mean that you can"t set any other command-line option due to shell escaping problems.

also note that email formatting requirements differ between windows and everything else. sendmail expects only \n line-endings