How Does PHP Send Mail Using mail() Without sendmail? - php

Okay, so I have a simple script that sends an e-mail via PHP's mail function like so:
<?php mail('foo#example.org', 'Subject', 'This is a message', array('From' => 'bar#example.org', 'To' => 'foo#example.org', 'Subject' => 'This is a message')); ?>
It works perfectly when run on a Synology DS212j (a Linux-based NAS using BusyBox for most common shell commands), however as far as I can tell there is no instance of sendmail anywhere that I can find, and the BusyBox sendmail isn't enabled. In fact, if I use ini_get("sendmail_path") then a value of " -t -i" is returned.
There's an environment variable of MAIL=/var/mail/root but this doesn't to do anything either (there is no /var/mail).
So I'm at a complete loss as to how PHP is actually sending its e-mail, so I'm hoping someone might know. I'm having trouble going through sendmail for PHP on another platform (on the same network sigh) so if I can use the same mechanism as my NAS then it could make things easier.

If you have qmail or postfix on your system and they are configured with sendmail wrappers, then PHP can be configured to work with these. Otherwise sendmail binaries must be on the system for php to send email using mail() function.
reference : http://php.net/manual/en/mail.requirements.php

Related

PHP mail from user instead of webmaster [duplicate]

I am using PHP with Apache on Linux, with Sendmail. I use the PHP mail function. The email is sent, but the envelope has the Apache_user#localhostname in MAIL FROM (example nobody#conniptin.internal) and some remote mail servers reject this because the domain doesn't exist (obviously). Using mail, can I force it to change the envelope MAIL FROM?
EDIT: If I add a header in the fourth field of the mail() function, that changes the From field in the headers of the body of the message, and DOES NOT change the envelope MAIL FROM.
I can force it by spawning sendmail with sendmail -t -odb -oi -frealname#realhost and piping the email contents to it. Is this a better approach?
Is there a better, simpler, more PHP appropriate way of doing this?
EDIT: The bottom line is I should have RTM. Thanks for the answers folks, the fifth parameter works and all is well.
mail() has a 4th and 5th parameter (optional). The 5th argument is what should be passed as options directly to sendmail. I use the following:
mail('to#blah.com','subject!','body!','From: from#blah.com','-f from#blah.com');
PHP Official documentation for mail()
bool mail ( string $to , string $subject , string $message [, string
$additional_headers [, string $additional_parameters ]] )
...
additional_parameters (optional)
The additional_parameters parameter can be used to pass additional
flags as command line options to the program configured to be used
when sending mail, as defined by the sendmail_path configuration
setting. For example, this can be used to set the envelope sender
address when using sendmail with the -f sendmail option.
This parameter is escaped by escapeshellcmd() internally to prevent
command execution. escapeshellcmd() prevents command execution, but
allows to add additional parameters. For security reasons, it is
recommended for the user to sanitize this parameter to avoid adding
unwanted parameters to the shell command.
Since escapeshellcmd() is applied automatically, some characters that
are allowed as email addresses by internet RFCs cannot be used. mail()
can not allow such characters, so in programs where the use of such
characters is required, alternative means of sending emails (such as
using a framework or a library) is recommended.
The user that the webserver runs as should be added as a trusted user
to the sendmail configuration to prevent a 'X-Warning' header from
being added to the message when the envelope sender (-f) is set using
this method. For sendmail users, this file is /etc/mail/trusted-users.
You can try this (im not sure tho):
ini_set("sendmail_from", yourmail#example.com);
mail(...);
ini_restore("sendmail_from");
I would also recommend checking into PHPMailer. It's great for creating and sending email, making the process a lot easier, along with support for SMTP.
following to php manual additinal -f parameter need to be passed to mail function
Not as many write here "-f from#email.com" but without white space "-ffrom#email.com"
https://www.php.net/manual/en/function.mail.php
What you actually need to do is change the hostname of the machine Apache is running on, plus the user Apache is running as.
In your current case it is:
Apache user:nobody
Server hostname: conniptin.internal
Changing those two values is pretty simple and will solve the root of your problem.
Although if you need to do it from PHP then perhaps use the system/exec functions. I do not think it will work in practice though, as you need to restart Apache and probably also the entire host for the new names to be used.

Can't override sendmail_path with mail() PHP

In the php.ini, the sendmail_path is : -femail#site.com -t -i
But, in a subdomain, I need to send email with the sender : email#new.site.com
I tried to use
ini_set('sendmail_path',-femail#new.site.com),
but sendmail_path is system, so nothing append.
I tried to define sender in mail(), doesn't work (on the log of the server, the sender is still email#site.com, but in the email client, the sender is fine, but it doesn't matter).
I tried to define the 5th parameter, but the function just stop working (do nothing, no error).
Any suggestions ?
Thanks,
Greg
Think about how mail is configured in PHP - it's just a wrapper around an exec call (with some predefined arguments). Hence it's trivial to invoke sendmail via exec substituting your own aruments. This is described in the first comment on the page describing PHP mail config. You just need to composite your own headers (sendmail extracts the recipient addresses from the headers to fill in the envelope, any Bcc lines are stripped before the email is forwarded).
Another approach would be to use a SMTP capable abstraction layer such as swiftmailer or phpmailer - but you probably won't be able to use 'localhost' if it's configured as a slave relay.

How to get PHP to send mail to local users (cakePHP, Linux SLES)

Currently I have a mail server configured (a real one from my ISP) and mail internal and external works on the command line. In PHP only external users work.
For testing I would like to send to internal users only.
( Ideally I would like to set up lots of aliases that point to one user so mail to:
tom#localhost.com, dick#localhost.com, harry#localhost.com end up in /var/mail/johnsmith )
I'd be greatful if someone could help here. I'm hesitant to edit the postfix config files...
On the command line johnsmith#localhost works but not in PHP. It's using cakePHP and I checked the value of $email-addr just before the send ($this->Email->send();) and the value is johnsmith#localhost. I'm not that familiar with cakePHP yet. The var/log/mail shows nothing, only external email addresses.
(server is Suse linux)
You can use the basic mail php function
http://php.net/manual/en/function.mail.php
Under linux, mail php function relies on sendmail, just check that sendmail is properly installed.
In /etc/postfix/main.cf add localhost.com:
mydestination = $myhostname, localhost.$mydomain,localhost,localhost.com
This allows sending on "localuser#localhost.com" via command line. I loaded up a test php mail form script in a browser and it works, sending mail through to /var/mail/localuser.
At the moment it means I have to check each local users /var/mail file. I'm working on the alias. My first attempt at that failed.

PHP not sending outbound mail

I'm using the sendmail package and php, When I try to use the mail function in PHP it returns true but nothing is sent.
php config
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path =/usr/sbin/sendmail -t -i
php file
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);
trace(mail('jamie#domain.tld','Testing','test.'));
the mailer log is displaying this
mail() on [/var/www/misc/mail.php:5]: To: jamie#domain.tld -- Headers:
Running sendmail through the CLI as this:
echo -e "To: jamie#domain.tld\nSubject: Test\nTest\n" | sendmail -bm -t -v
Returns "Sender ok", "Recipient ok"
Anyone know of anything which could be causing php to not send the email?
PHP has nothing to do with mail delivery. Its job begins and ends with handing the email off to the specified SMTP agent/server. If mail() does not return a boolean FALSE, then PHP's job has succeeded and it's out of the picture.
Check your SMTP server's logs to see what happens to the email after the handoff is completed. It's quite likely the mail is being rejected/greylisted/spamfiltered into oblivion, because PHP's mail() truly blows.
Consider switching to either Swiftmailer or PHPMailer, both of which offer far better diagnostics of the PHP<->SMTP interactions than mail() ever will.
Testing manually from the command line means little: your shell environment is very different from the in-webserver environment that your script is likely executing in - apples and oranges.
This can be misleading. PHP will return true because, as far as its concerned, it has done its part. But it has no guarantee that the SMTP server will honour the request.
When this happened to me once, it turned out my host had implemented a new stipulation that all script mail must feature a 'from' address in the headers, which had to be a valid address associated with the hosting account.
Might be worth investigating that possibility.

Sendmail path error?

I just switched to using Msmpt, but I am unable to send using the mail() function. I can send via SSH command line perfectly but var_dump shows that mail returns a false value.
sendmail_path = /usr/bin/msmtp -t -i
Is the path, but manually browsing, I am unable to locate "msmtp". Any ideas how to find where it is located so I am able to correct the sendmail path?
Typing
which msmtp
from the command line should return the path to msmtp.
I dont have any experience with msmtp but tried many smtp server types..
At first, does your smtp require authentication (for your host. maybe its' localhost, but it's up to configuration.) if yes. you may need to use a php smtp library with authentication. there is one in pear http://email.about.com/od/emailprogrammingtips/qt/et073006.htm Check the link use the sample code. Trying to send a mail via sockets (! notice that not mail() function) will tell you much more diagnostic information. if you dont have pear installed check phpmailer library in sourceforge.net. i use phpmailer which is much easier to use than pear.

Categories