Basic mail function (PHP) additional "-f" parameter question - php

Is the -f additional parameter correctly set in this mail function.
#mail("example#exmaple.com.uy",$title,$body,$headers,"-f");
I am Getting the X Warning from some servers.
Sorry for the basic question but some parts of the documentation got me confused (specially some user comments).
Thanks in advance!

From the manual:
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.
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.
source: http://www.astahost.com/info.php/Sending-Mail-Php39s-Mail-Function_t2728.html
The additional_parameters parameter
can be used to pass an additional
parameter to the program configured to
use when sending mail using 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.
You may need to add the user that your
web server runs as to your sendmail
configuration to prevent a 'X-Warning'
header from being added to the message
when you set the envelope sender using
this method. Example 3. Sending mail
with extra headers and setting an
additional command line parameter.
i.e:
<?php
mail("nobody#example.com", "the subject", $message,
"From: webmaster#{$_SERVER['SERVER_NAME']}", "-fwebmaster#{$_SERVER['SERVER_NAME']}");
?>
After -f you need to set the outgoing email address to prevent the warning (in this case its webmaster#-the domain-

If your machine runs on a linux server. Your apache install more than likely runs under the user 'www-data'.
you can figure this out easily by going to /etc/apache2 and typing
cat envvars | grep APACHE_RUN_USER
whatever is after '=' is what user apache is running as.
You need to add this user to the trusted-users file. This file is located at /etc/mail/trusted-users
just
nano /etc/mail/trusted-users
and write 'www-data'.
save and you should be good to go.

-f should be followed by the address you want as envelope address on your mail.
#mail("example#exmaple.com.uy",$title,$body,$headers,"-fexample#exmaple.com.uy");

Have you tried sending it without the -f flag?
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 need to specify an email address after the -f flag. Like this: "-fexample#example.com". You may also need to add the user that your web server run as to your sendmail configuration.

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.

Ubuntu sendmail only sends to localhost

I have set up a LAMP server with sendmail on Ubuntu 14.04.
When sending mail, either from terminal or PHP mail(), it won't work properly.
When I use mail("user#localhost","test","test"); it sends it correctly, and I can read the message with mail from terminal, but when sending to Gmail address, message won't go trough.
Same outcome when using "test" | mail -s "test" xyz#gmail.com" from server terminal.
Any idea how to fix this?
I have been Googling for several hours now, I have tried everything without finding working solution, and I'm starting to lose hope.
If you're running Ubuntu here is a much simpler solution:
issue from command line >
apt-get install ssmtp
Then edit the configuration file in /etc/ssmtp/ssmtp.conf
A sample configuration to use your gmail for sending e-mails:
# root is the person who gets all mail for userids < 1000
root=your#email.com
# Here is the gmail configuration (or change it to your private smtp server)
mailhub=smtp.gmail.com:587 (leave this the way it is)
AuthUser=your#gmail.com (just change this)
AuthPass=yourGmailPass (and change the password for your gmail account)
UseTLS=YES (leave this the way it is)
UseSTARTTLS=YES (leave this the way it is)
Note: Make sure the "mail" command is present in your system. mailutils package should provide this one in Debian based systems.
Then try send emails again, send it to another domain #gmail, #aol, #yahoo, #privatedomain besides localhost email addresses.
There're tons of possible problems, so you need to get some logs of error that happening.
Most probably, Google just rejects your email as your server is not correctly configured.

PHP - 553 Relaying disallowed with Zoho mail and MyBB

I'm trying to use Zoho's SMTP servers to send registration emails from a MyBB installation. However, the emails are never sent and when I check the logs I notice that the error is.
Month Day Year:Hour:Minute host=smtp.zoho.com tls=on auth=on user=user[at]mydomain.com from=user[at]mydomain.com recipients=some.user[at]gmail.com smtpstatus=553 smtpmsg='553 Relaying disallowed' errormsg='the server did not accept the mail' exitcode=EX_UNAVAILABLE
(Some parts were masked to hide private information!)
This is the sendmail path set in php.ini
sendmail_path = "/usr/bin/msmtp -C /etc/msmtp/myserver --logfile /var/log/msmtp/myserver.log -a default -t"
(Some parts were masked to hide private information!)
This is the myserver configuration for MSMTP
# Define here some setting that can be useful for every account
defaults
logfile /var/log/msmtp/general.log
# Settings for default account
account default
protocol smtp
host smtp.zoho.com
tls on
tls_starttls off
tls_certcheck off
port 465
auth plain
user user[at]mydomain.com
password **********
from user[at]mydomain.com
logfile /var/log/msmtp/myserver.log
# If you don't use any "-a" parameter in your command line,
# the default account "default" will be used.
# account default: default # (disabled because this gives a "redefined" error)
(Some parts were masked to hide private information!)
The problem is that I know it's working and that no ports are blocked because when I use the same command in the command line. The email is successfully sent and received.
sudo echo -e "Subject: Test Mail\r\n\r\nThis is a test mail" | msmtp --debug -a default --from=user[at]mydomain.com -t some.user[at]gmail.com --file=/etc/msmtp/myserver
(Some parts were masked to hide private information!)
Which means that it's either something wrong with the way MyBB sends emails or it's something wrong with my PHP configuration or command line that I have set in php.ini file.
I've searched this all day but every result that I get is either not related to PHP or it never worked from the beginning. And mine is working from the command line but not from PHP.
I use UFW on the server and every port is completely opened so I'me guessing it's not related to blocked ports.
It worked with Gmail but every email was sent to spam folder and it didn't worked every time with every email.
EDIT:
I've tried to send an email with PHP5-CLI and it worked. So I'm guessing that it's either something wrong with PHP5-FPM or MyBB.
I can't test this without a MyBB install, but if it uses the native PHP mail function, you may need to add the -f option to the mail() function call since you are using sendmail.. Note it is the 5th parameter (I wasted a day once thinking it the was 4th)
mail('nobody#example.com', 'the subject', 'the message', null,
'-flocaluserr#thisdomain.com');

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.

What does the -f flag mean in the fifth parameter in the PHP mail function?

What does the -f flag mean in the fifth parameter in the PHP mail function?
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.
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.
The fifth parameter is for sending command line flags to the server that actually handles sending the email. So not knowing what that really means, I did a google search and found a list of command line options for command line options for sendmail.
If I had to guess, not based on that page but on almost all other sites that mention that option in passing, it's for setting the "from" header at the server level rather than at the Header level. So you'd do
mail($stuff, $junk, $blah, $headers, '-fsender#server.org');

Categories