PHP can send email from command line but not browser - php

I have a form set up at my homepage. When someone submits the form, it redirects them to a php script that sends me the form. The only issue is, the form won't send me anything when I use the browser. I have a script running on the same server that when I execute from the command line, sends an email immediately. When I go to the page in a browser, it gives a confirmation message, but won't send anything. What's happening? PHP is using MSMTP to send email if that helps. I believe apache is running as root (I know that's bad). How do I check?
Here's the code if anyone wants it:
<?php
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );
$from = "mailbot#prototypexenon.tech";
$to = "<MYEMAIL>";
$subject = "PHP Mail Test script";
$message = "This is a test to check the PHP Mail functionality";
$headers = "From:" . $from;
mail($to,$subject,$message, $headers);
echo "Test email sent";
?>
EDIT: Apparently new-server-owner was smart enough to not let apache run as root. All fixed now. Thanks #EdHeal

PHP running via the command line and PHP running via a web server can use totally different php.ini files. Run phpinfo() and see which php.ini file is being used in each case. Probably one of them doesn't have SMTP properly configured.

Apparently new-server-owner was smart enough to not let apache run as root. All fixed now. Thanks #EdHeal

Related

PHP text messages suddenly sending from a different address

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.

php mail() giving me a "no disk" error

Hey I am trying to send an email from a php script. When I try to do so, I get a pop up box that is titled "mailtodisk.exe - No Disk". In the body of the error message, it says, "There is no disk in the drive. Please insert a disk into drive \Device\Harddisk1\DR1".
I have tried to figure this out but to no avail. I am doing this from localhost.
Here is my script that is supposed to send the email:
<?php
$to = $_POST['email1'];
$subject = "Test mail";
$message = "I just sent you an email!";
$from = "ULSRL#louisiana.edu";
$headers = "From:" . $from;
if( mail($to, $subject, $message, $headers) )
{
echo ("Mail Sent.");
}
else
{
echo ("Mail could not be sent!");
}
?>
Any help is greatly appreciated! Thanks :)
I am guessing you are using XAMPP?
If so you will need to modify the php.ini located in your XAMPP installion, look for the following lines:
; XAMPP: Comment out this if you want to work with fakemail for forwarding to your mailbox (sendmail.exe in the sendmail folder)
;sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
; XAMPP: Comment out this if you want to work with mailToDisk, It writes all mails in the C:\xampp\mailoutput folder
sendmail_path = "C:\mailtodisk\mailtodisk.exe"
mail cannot be delivered in PHP even SMTP server is running and PHP mail() returns true
This should also help:
http://blog.joergboesche.de/xampp-sendmail-php-mailversand-fuer-windows-konfigurieren#xampp_180_sendmail
You are trying this solution in Localhost. So you can get this error. Try in live work. You will definitely succeed.
For XAMPP, I have the same message it shows about 4 times providing options
{cancel}, {try again}, {continue}
No matter what I choose it replied 4 times and instead of showing the next page it showed the blank page.
My XAMPP version saves emails to the C:\xampp\mailoutput and this part was working.
What I did was: I commented out the following line is PHP.ini
;sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"
Now it does not save the files in C:\xampp\mailoutput
BUT it does not give me this annoying error and the next page loads fine.
Root cause of the problem is that mailtodisk.exe tries to reach non existing drive on your Windows machine, which cause this annoying message. This can be considered as a bug in mailtodisk actually, so you can simply don't use it as other replies here suggest.
This problem is caused usually because of USB sockets attached to your computer, for example for reading SD cards, which are currently empty.
If you prefer walking on the wild side, you can tweak your registry and suppress this message. See video demonstration here https://www.youtube.com/watch?v=Aj7-pLaAq2c

Can't send email with PHP Mail

I'm trying to send an email using php. I've the following only it doesn't seem to output anything to my browser nor send my email, can anybody see what i'm doing wrong?
$to = "liam#mysite.co.uk";
$subject = "Alien Sighting";
$message = foreach ($_POST as $key=>$value) {
$$key = $value;
}
$from = "Sighting#site.co.uk";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
Can you give us $_POST content please ?
Does your PHP configuration display all errors messages? Make sure to use error_reporting (E_ALL) in your dev environnement to find any typo / error. That should help to get an output to your browser.
You have to be sure to setup your SMTP server to send your email if you are running the script on your computer otherwise you can get a test server so you don't have to setup the SMTP it will send automatically
Are u using Rmail.php? Do u uploaded your code to a webserver or is it on your local PC?
I am always using Rmail.php to send mails.
$mail = new Rmail();
$mail->setFrom(YOUR ADDRESS);
$mail->setSubject(YOUR SUBJECT);
$mail->setHTML($textSchueler);
$mail->send(array($address));
You'll also want to check and make sure your server is configured to send email at all. While most shared hosting accounts will be configured for you if you are setting up your own server or developing locally PHP may not have the any way to send the messages. Try checking to see if the mail() function returns true indicating the message was successfully sent and then look in the mail logs on the server to verify transmission.
In this kind of situation where PHP does not output anything to your browser (even it should), most likely the script fails before your echo statement.
Make sure you have not disabled error reporting. See PHP runtime configuration error_reporting and display_errors. You can change error reporting behaviour also runtime with error_reporting() function in the code. Enabling error reporting helps you to figure out what the problem might be.
Even you had disabled the error reporting on the browser, check out your webserver logs. They should have details what's the problem (syntax error in the script, failed mail delivery by mail function call... there are many possibilities).

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

cannot initiate mail function in joomla

I am getting this error in Joomla while sending the mail.
I am not getting this error every time. But some times its shows me "cannot initiate mail function".
Any solution for this?
That could be any number of things, but a general list of things to check would be first, your Joomla config:
Admin panel > Global Configuration > Server > Mail Settings > Mailer
Make sure that's set to use the PHP mail function. If it is, try making a script called test.php and putting it in the root of your site (where the index.php file is for Joomla). Make that file something like this:
<?php
$to = "you#youremailaddress.com";
if( mail( $to , 'This is a test message.' , 'Is this working?' ) ) {
echo 'Email sent.';
} else {
echo 'Email failed to send.';
}
?>
Make sure you change the $to = line to your email address.
Now go to that script: http://www.yourjoomlasite.com/test.php
You should see the text 'Email sent.' in your browser and then receive an email to that address you entered. If not, then you should contact your hosting provider and ask them to upgrade to the latest PHP version and/or resolve the mail() function issue for you. That is the most raw implementation of sending mail via PHP and if that fails to work then it's got to be an issue with your host.
If you are working in the local server, the mail will not initiate. So host your website in server and try it.
If you have any questions, let me know
If you are using WAMP or similar on your own machine and are getting this message then you need to install a mail server. This is the best solution on Windows.
http://www.toolheap.com/test-mail-server-tool/

Categories