PHP text messages suddenly sending from a different address - php

I have a website that sends out text messages from the server. I'm using PHP for it:
$headers = "From: " . "Business Name <info#businessdomain.com>" . "\r\n";
$result = #mail( $to,$subject,$message,$headers);
Up until yesterday, the FRM: was always "Business Name" in the text message.
Suddenly, I'm now getting reports that FRM: is coming in as username#173.222.21.166 where username is my actual FTP account username (real IP not provided of course)!!
I definitely don't want to expose my FTP username. Is there something that would determine the FRM: name other than what I have set? I haven't changed the script at all... and suddenly it has changed.
UPDATE: using PHP mail not SMS Gateway

You can add a command line parameters to php's mail function, one of which is from.
mail($to, $subject, $message, $headers, '-finfo#businessdomain.com');
Note the -f directly infront of your from address

This may be related to the mail server on the local machine or the mail configuration on the local machine. There are certain settings, which can prevent faking the FROM header and substituting it by the actual user running the program that sends the mail.
I recommend checking the mail config for php and for the system itself.

Related

php mail 'from' header not behaving

I have an odd problem.
I have a 'standard' Email include I use that wraps the php mail() function. This has been in use for a few years and in two places in my site is quite happily sending mail every day. However, on re-using the same include in a different part of the same site on the same host, I am finding I cannot send mail including a 'From' header. The only difference is this new sending location on the site is a basic auth-protected directory.
So before people leap in and tell me this or that, remember, this is the exact same code as it working at the same time on the same host and so the exact-same include works in two locations and not the third.
So I set, among other headers (as this carries multipart HTML Email):
$headers = "From: $fromName <$fromEmail>\n";
$headers .= "Reply-To: \"$fromName\" <$fromEmail>\n";
$headers .= "Sender: \"$fromName\" <$fromEmail>\n";
This works fine in two locations on the site, but the inclusion of the 'From' line causes a mail send failure in a third location on the same host. The code also works fine from my dev machine, it's only the Live hosted box that won't send mail from this third location. Now...
Yes I know I'm using "\n" and not "\r\n", but my experience has been
"\r\n" is necessary on Windows hosts but causes problems on *ix
hosts - and yes I know the RFC says CRLF - and so this is 'fixed'
later in the wrapper if it's on a Windows host but left as "\n" on
*ix
No, setting "\r\n" does not make any difference!
I have experiemented with the mail() fifth paramter and using -f, but that
only gives me mail sent on behalf of
The 'Sender' field actually has the effect that on opening the message the recipient sees; From Pretty_name [email_address], however, in Outlook at least, in the mail list the sender is shown as email_address, not 'Pretty_name'
I've experiented with ini-set, but that only sets an Email address and the message is already going with the desired From Email address, it's the 'Pretty_name' that won't work
So remember - the exact same code is runing elsewhere on the same site without any problems, but the issue here seems to be (for a reason I've yet to fathom) that the same code can only be used without the 'From' header from this location.
Anyone any ideas on what seems such inconsistent behaviour?
Thx
First, if mail() returns false, then make sure you output the last error message:
print_r(error_get_last());
If the error message doesn't really indicate much about the email itself, then it's likely something wrong in the php.ini file (e.g. configuration mistake during an upgrade or something). You can test that by isolating the problem code to a separate file, then temporarily swap out the php.ini file for the default one, restart your web server to let the new php.ini take effect, and run your test again (I'd recommend dumping phpinfo() along with your script so you can confirm that the new php.ini is in effect).
Second, use \r\n. I know it has no functional difference in your test here, but it IS the right line ending and using just \n WILL cause problems in some mail servers that actually follow the rules. When you use \n, you are relying on the mail server to have additional code that handles automatic correction of errors.
Using \r\n might cause problems in some poorly-coded servers that DON'T follow the rules, but that's not really your problem to solve. Always follow the RFC.

Is -f key mandatory for PHP mail() send emails with correct from address?

Having a real tough time fixing an issue on a RS cloud server. The emails sent by PHP scripts, although a From header is included, the from address is getting changed to current user that is running the php.
e.g
mail('someone#gmail.com', 'Test mail', 'Reply back when you see this', 'From: web#mysite.com' . "\r\n");
Although the additional header is included, the email is being sent as apache#mysite.com or manjuser#mysite.com (when I login to ssh as manjuser and run the php from cli). Clearly overriding the from address. This is resulting in emails not getting delivered to destination. RS email app rejects it as both apache and manjuser email boxes are not present.
Question is:
Is there a way to tell sendmail/postfix to use the From header or a fixed email address instead of requiring -f to be sent every single time mail function is used?

php redirect email to file

I am using php5.
Are there some settings or a simple php.ini directive that would redirect all the emails to a folder?
I want on the development machine to have all the emails generated by the system not sent to the actual receiver but put in a folder.
Thanks.
I used to have some code like this (kinda pseudocode):
define ('DEBUG', true);
function send_email($to, $subject, $body) {
if (DEBUG) {
file_put_contents('some_folder/' . $to . date('dmY-His') . '.html', $body);
}else{
// Actual code to send email
}
}
But i agree with others, it's easier/better to setup an development email account to receive those emails.
I don't think you will be able to do something like this. Mails are sent by a mail server so it must be your mail server that writes them to a file instead of sending them.
Why not simply send it to a special development email?
Sample:
define('DEBUG', true);
if(DEBUG)
{
// Override recipient
$recipient = 'development#domain.tld';
}
// Send mail...
No settings that I'm aware of in PHP itself. However, if you're using Postfix on your development server, here's a recipe I cooked up to redirect all outbound email to a single (local) address:
/etc/postfix/main.cf: (add this to the existing file, don't replace everything)
virtual_alias_maps = regexp:/etc/postfix/virtual
/etc/postfix/virtual:
/.*/ duskwuff#localhost
You can configure your mail server to accept SMTP messages as normal, but make it unable to forward them onto another mail server. If your mail server supports it, make it redirect all messages to a postmaster account, or any other address of your choice.
This means that PHP will behave as normal, everything will appear to work straight away with the message, but it just won't go to the 'intended' recipient.
It also means that you can inspect headers (pretty much as they would normally appear), to support debugging.
There are many ways to do this. Basically, you need to define the sendmail command in your php.ini to point to a program or script which will save the mail locally.
One solution is this:
Catch emails with php script
Another is this:
Mail catcher

Email creation script for Wamp

I would like to make it available for clients to create emails through a web based interface without me having to go to mercury mail program and add a new user every time. i just need a source or script that will allow my clients to create emails on my wampserver email system.
I'm using MercuryMail as my mail host and i'm currently public with my server but need an email creation script that i can put into a web page. Preferably PHP.
Please help,
Thanks.
1) Open the “php.ini“. You should know where it is located because it depends upon the particular server you’re running.
2) Search for the attribute called “SMTP” in the php.ini file.Generally you can find the line “SMTP=localhost“. change the localhost to the smtp server name of your ISP. And, there is another attribute called “smtp_port” which should be set to 25.I’ve set the following values in my php.ini file.
SMTP = smtp.wlink.com.np
smtp_port = 25
3) Restart the apache server so that PHP modules and attributes will be reloaded.
4) Now try to send the mail using the mail() function ,
mail(“you#yourdomain.com”,”test subject”,”test body”);
you might get the warning like this,
Warning: mail() [function.mail]: “sendmail_from” not set in php.ini or custom “From:” header missing in C:\Program Files\xampp\htdocs\testmail.php on line 1
5) Now specify the following headers and try to send the mail again,
$headers = ‘MIME-Version: 1.0′ . “\r\n”;
$headers .= ‘Content-type: text/html; charset=iso-8859-1′ . “\r\n”;
$headers .= ‘From: sender#sender.com’ . “\r\n”;
mail(“you#yourdomain.com”,”test subject”,”test body”,$headers);
Well that’s all, the mail is sent to “you#yourdomain.com” from the localhost.
Note : Some smtp server verifies the email address of the sender so the email address which is in the place of “sender#sender.com” should be a valid and existing email address otherwise mail might not be sent to the “you#yourdomain.com”.

How to write a PHP script that read bounce email?

I am doing a bounce-email handling with PHP. I have include the return path in the mail function, e.g:
mail($to_address, $subject, $message, $headers, "-f".$return_path );
$return_path = "bounce_handle#domain.com";
Now, what should my php script looks like (and where should i put it) in order to read all the bounce emails? (can show me with some sample code?)
You'll need to configure whichever mail transport agent handles (MTA) "bounce_handle#domain.com" to send the mail to the PHP script that does whatever magic you need it to do. The MTA is what actually handles mail coming into the server. There are many different MTA's, but most of them have some configuration where you can basically tell it to pipe email coming into a certain address into a custom script.
Alternatively, you could setup a mailbox for your bounce handler and have PHP read it via POP3. For this, you'd have to configure an actual email account for your bounce handler. Then you have your PHP script connect to that mailbox using standard protocols. See the php.net documentation on IMAP/POP for how this is accomplished.

Categories