note 126334 deleted from function.mail by crell
| From: | crell@php.net | Date: | Tue, 12 Apr 2022 01:09:30 +0000 |
| Subject: | note 126334 deleted from function.mail by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218225@lists.php.net to get a copy of this message | ||
Note Submitter: atesin () gmial ! com
----
HOW PHP MAIL() WORKS INTERNALLY, more or less, according some tests i did
imagine php is running with uid 33 when reaches a mail() function like this
---- /my/htdocs/mailer-script.php line 69 ----
$was_sent = mail(
'Alice <alice@wonder.land>, white-rabbit@endless.cave',
'Invitation',
"Hi, you are cordially invited to drink a cup of tea at my house\ntomorrow at sunset",
"Some-Header: foo\r\nOther-Header: bar", // can also be an associative array of
'Header' => 'Value'
'param1 param2');
----
then look for ini options like this (all optional, on any operating system)
---- ini options ----
; path to mail sender command + arguments, if omitted will defaults to NULL
sendmail_path = '/bin/sendmail-esque arg1 arg2'
; the From header, can be also set in sendmail but make sure it SENDS ONE
sendmail_from = 'Mad Hatter <hatter@house.forest>'
; recommended to not being blind, look at file permissions
mail.log = '/var/log/php/mail.log'
; adds an originating script header to send php mail internals (security sensitive)
mail.add_x_header = on
; outgoing mail server address, if omitted will defaults to "localhost"
;SMTP = 'localhost'
; server tcp port, if omitted will defaults to port 25
;smtp_port = 25
----
then craft a message like this (please avoid duplicated headers)
---- mail message ----
From: Mad Hatter <hatter@house.forest>
To: Alice <alice@wonder.land>, white-rabbit@endless.cave
Subject: Invitation
Some-Header: foo
Other-Header: bar
X-PHP-Originating-Script: 33:mailer-script.php
Hi, you are cordially invited to drink a cup of tea at my house
tomorrow at sunset
----
then APPEND this log line
---- /var/log/php/mail.log ----
[<date> <time> <zone>] mail() on [/my/htdocs/mailer-script.php:69]: <all
headers joined...>
----
THEN IF (sendmail_path is not empty, AND file is found, AND has +rx permissions):
- craft and run this shell command
---- shell ----
/bin/sendmail-esque arg1 arg2 param1 param2
----
- send the crafted message to its standard input
- wait the program to exit
- pass last exit code to mail() return
OR ELSE (i.e. sendmail bin not run) (not sure about these):
- try to internally open an smtp connection (with fsockopen()?)
- send the message directly and wait for connection to close
- pass last socket status to mail() return