Решение проблем с отправкой почты из скриптов в Sendmail. Тормозит отправка писем в php (sendmail) Пунктуальный inurl sendmail php

Настройка почтового сервера sendmail (решение проблемы с отправкой почты из скриптов)

В некоторых случаях бывает, что почтовый сервер Sendmail не отправляет почту, возвращая ошибку:

Feb 28 22:04:24 myhost sendmail: mATM4O8w58065806: Losing ./qfmATM4O8w58065806: savemail panic Feb 28 22:04:24 myhost sendmail: mATM4O8w58065806: SYSERR(www): savemail: cannot save rejected email anywhere

Также в логах может быть ошибка вида:

Stat=Data format error

Проблема, как правило, вызвана несуществующим hostname сервера, либо несуществующим почтовым адресом при отправке.

В таких случаях необходимо указать существующий почтовый адрес. В ISPmanager его можно прописать в свойствах WWW домена в поле "E-Mail администратора".

При указании адреса в конфигурации веб-сервера Apache в описании виртуального хоста:

Php_admin_value sendmail_path "/usr/sbin/sendmail -t -i -f [email protected]"

Если используется PHP в режиме как CGI или как FastCGI, запись прописывается в файле ~/php-bin/php.ini

Sendmail_path = "/usr/sbin/sendmail -t -i -f [email protected]"

Отключение проверки корректности адреса отправителя при отправке почты

Sendmail по умолчанию запускается с Message Submission Program (MSP). Это отдельный процесс для обработки почтовой очереди.

Рассмотрим его отключение в системе FreeBSD

Пропишите в файле /etc/make.conf строку, отключающую автоматическое создание файла submit.cf

SENDMAIL_SET_USER_ID=yes

После чего необходимо удалить файл submit.cf, поменять права на исполнимый файл sendmail (поставить на него SUID bit вместо SGID, то есть установить права 4755 вместо 2755) и перезапустить Sendmail

Каждый раз настраивая локальный вебсервер, сталкиваюсь с вопросом о заглушке для 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

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

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

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

Проблема

Начал смотреть, в чём же дело. В логах php-fpm ничего интереного нет, в логах nginx видно, что php-fpm upstream отваливает по таймауту.

2013/04/18 15:28:01 6928#0: *1409 upstream timed out (110: Connection timed out) while reading response header from upstream, client: xx.xx.xx.xx, server: , request: "POST /page.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9010", host: "site.ru", referrer: "http://site.ru"

И тут мне в голову пришла идея заглянуть в syslog. Там то я и увидел интересные строчки:

Sep 9 18:53:20 mega sm-msp-queue: My unqualified host name (mega) unknown; sleeping for retry Sep 9 18:54:20 mega sm-msp-queue: unable to qualify my own domain name (mega) -- using short name

Ага, начал грешить в сторону sendmail. Пробую перезапустить его командой:

Sudo service sendmail restart

и вижу подтверждение своей догадки.

Andrey@mega:/home/andrey# sudoservice sendmail restart sudo: unable to resolve host xyz.xyz * Restarting Mail Transport Agent (MTA) sendmail andrey@mega:/home/andrey#

Решение

Дело оказалось в имени сервера. После того, как поставил вот такое имя сервера (с точкой) — всё заработало как надо.

127.0.0.1 localhost localhost.localdomain mega mega.foobar

Спасибо!

Если вам помогла статья, или вы хотите поддержать мои исследования и блог - вот лучший способ сделать это:

Знакомство с 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() используется в тех случаях, когда речь не заходит о масштабных рассылках.