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.
Related
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.
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.
Is there a way that I can configure the WAMP server for PHP to enable the mail() function?
Configuring a working email client from localhost is quite a chore, I have spent hours of frustration attempting it. I'm sure someone more experienced may be able to help, or they may perhaps agree with me.
If you just want to test, here is a great tool for testing mail locally, that requires almost no configuration:
http://www.toolheap.com/test-mail-server-tool/
Install Fake Sendmail (download sendmail.zip).
Then configure C:\wamp\sendmail\sendmail.ini:
smtp_server=smtp.gmail.com
smtp_port=465
auth_username=user#gmail.com
auth_password=your_password
The above will work against a Gmail account.
And then configure php.ini:
sendmail_path = "C:\wamp\sendmail\sendmail.exe -t"
Now, restart Apache, and that is basically all you need to do.
Using an open source program call Send Mail, you can send via wamp rather easily actually. I'm still setting it up, but here's a great tutorial by jo jordan. Takes less than 2 mins to setup.
Just tried it and it worked like a charm! Once I uncommented the error log and found out that it was stalling on the pop3 authentication, I just removed that and it sent nicely. Best of luck!
You need a SMTP server to send your mail. If you have one available which does not require SMTP authentification (maybe your ISP's?) just edit the 'SMTP' ([mail function]) setting in your php.ini file.
If this is no option because your SMTP server requires authentification you won't be able to use the internal mail() function and have to use some 3rd party class which supports smtp auth. e.g. http://pear.php.net/package/Mail/
I tried Test Mail Server Tool and while it worked great, you still need to open the email on some client.
I found Papercut:
https://github.com/ChangemakerStudios/Papercut-SMTP
(updated URL for 2021)
For configuration it's easy as Test Mail Server Tool (pratically zero-conf), and it also serves as an email client, with views for the Message (great for HTML emails), Headers, Body (to inspect the HTML) and Raw (full unparsed email).
It also has a Sections view, to split up the different media types found in the email.
It has a super clean and friendly UI, a good log viewer and gives you notifications when you receive an email.
I find it perfect, so I just wanted to give my 2c and maybe help someone.
Sendmail wasn't working for me so I used msmtp 1.6.2 w32 and most just followed the instructions at DeveloperSide. Here is a quick rundown of the setup for posterity:
Enabled IMAP access under your Gmail account (the one msmtp is sending emails from)
Enable access for less secure apps. Log into your google account and go here
Edit php.ini, find and change each setting below to reflect the following:
; These are commented out by prefixing a semicolon
;SMTP = localhost
;smtp_port = 25
; Set these paths to where you put your msmtp files.
; I used backslashes in php.ini and it works fine.
; The example in the devside guide uses forwardslashes.
sendmail_path = "C:\wamp64\msmtp\msmtp.exe -d -C C:\wamp64\msmtp\msmtprc.ini -t --read-envelope-from"
mail.log = "C:\wamp64\msmtp\maillog.txt"
Create and edit the file msmtprc.ini in the same directory as your msmtp.exe file as follows, replacing it with your own email and password:
# Default values for all accounts
defaults
tls_certcheck off
# I used forward slashes here and it works.
logfile C:/wamp64/msmtp/msmtplog.txt
account Gmail
host smtp.gmail.com
port 587
auth on
tls on
from ReplaceWithYourEmail#gmail.com
user ReplaceWithYourEmail#gmail.com
password ReplaceWithYourPassword
account default : gmail
I used Mercury/32 and Pegasus Mail to get the mail() functional. It works great too as a mail server if you want an email address ending with your domain name.
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.
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');