pipe mail to PHP script : cant find the pip.php file - php

hi i'm trying to pipe incoming emails to php script
i did step by step from my manual but when i send an email i get this error from my delivery system :
A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es)
failed:
pipe to |/home2/mimjobco/public_html/pipe.php
i chosse pipe.php address from cpanel so it cant be wrong
here is the picture !
here is my pipe.php code :
#!/usr/bin/php –q
<?php
require_once('class/support.php');
require_once('class/db.php');
$title = 'email_request';
$text = 'email_req_text';
$sup_id = 1 ;
$sup = new support;
$sup->title = $title;
$sup->part_id = $sup_id ;
$sup->text = $text;
$sup->email = 'email';
$sup->type = 1;
$sup->set_ticket();
mail('xxxx#gmail.com','new message recived','new message recived ');
i also set it's permission to 755
am i missing something ?
is there another thing that i should have done? someone had mention something about crone job ?!

The error message says "/home2/mimjobco/pipe.php", but the screenshot shows the file is in your "public_html" directory (presumably, "/home2/mimjobco/public_html/pipe.php").
Note that "pipe.php" should only be in "public_html" if you want it to be accessible on the website. Otherwise, it should go elsewhere in your home directory hierarchy (e.g. ~/bin/).

It looks like you are using bluehost, here is someone else who was able to fix the problem: http://www.bluehostforum.com/showthread.php?5786-Reading-email-with-php&p=26554#post26554
Setting up filter in cPanel, problem that delivery error coming back. Many solutions not available on a shared server.
Finally, this seems to work to avoid the error in delivery emails coming back - note the -q:
|/usr/bin/php -q /home/myacct/public_html/mydir/myfile.php
Solution came from http://forums.exocrew.com/index.php?showtopic=1838. (DEAD LINK)
The code for parsing emails being piped to the php file is at:
http://evolt.org/node/27914/
This gives you the message parsed as variables for subject, from, body etc.
Hope this helps!

Related

Access variables or functions from other scripts in cron job via cPanel

Background
Hi,
I am new to cron jobs and I am trying to set up a test one just to see how they work. I created it in cPanel like this:
wget -O - -q https://website.com/staging/wp-content/themes/AT103/test-cron.php
My file is pretty simple so far:
<?php
$email_to = "info#domain.com";
$title = "Test title";
$body = "Test body";
mail($email_to, $title, $body);
?>
All works fine here, I recieve the email every interval that my cron job runs.
What I want
On my site people can put up ads/listings to sell their stuff. In the cron job I want to go through all listings and email the buyers and sellers for the ones that has expired. As a first step in this I need to access the variables on my website, e.g. a listing object which is put in a variable like $post. Or through a function which returns the stuff I want if easier.
What I tried
I tried to access a random file and its functions by using lots of different code examples found online. The file is in the same folder as my test-cron.php. Here are a few things I tried to put at the top of test-cron.php (one at a time, not all at once):
require '/functions.php';
require 'https://website.com/staging/wp-content/themes/AT103/functions.php';
require '/home3/username/public_html/staging/wp-content/themes/AT103/functions.php';
This all resulted in the same thing, I did not get an email anymore. So I assume there is some sort of error with the lines? I also tried with require_once() with the same result.
Questions
How do I access other scripts ("live" variables or functions) in my folder hierarchy through a cron job?
If it is not possible for some reason, can I instead access my database information somehow?
If the file to be required/included, is in the same folder as the running script you would use one of the following:
require 'functions.php'; no leading slash tells it to look in the include path, then in the current directory.
require './functions.php'; (better) the ./ explicitly says look in the current directory.
https://www.php.net/manual/en/ini.core.php#ini.include-path
EDIT:
I just realized I did not address the fact that you are using cron and that's because ...
You are sill running PHP, cronjob or not makes no difference it still works the same!
However, it can be more difficult to debug on a production server. If you want to see exactly what's happening when the script fails then you can wrap it in a try block, catch and send the error to your email, or output the error and view it in the browser.
I know on Bluehost shared-hosting, if any of my cronjobs produce any output it will be automatically sent to me via email. I use the format below, and always get an email telling me when & why it happened. While developing you can simply navigate to your test-cron.php in the browser.
<?php
try {
require './functions.php';
/*
all of your logic
*/
} catch (Error $e) {
echo "Caught Error: \n" . $e;
}

Procmail setup, to execute PHP script

I've got a procmail setup working pretty well, seems to be executing my PHP script no problem when it receives an email.
Here is an example of the .procmailrc file:
#BEGIN PROCMAIL SCRIPT FOR MAIL PARSING
DEFAULT=$HOME/Maildir/
MAILDIR=$HOME/Maildir
PMDIR=$HOME/.procmail
LOGFILE=$PMDIR/log.`date +%y-%m-%d`
SHELL=/bin/sh
:0
|`/usr/local/php53/bin/php /home/usrmail/email/script.php`
The log output below:
From x13542053#homiemail-mx22.g.dreamhost.com Mon Feb 18 20:49:35 2013
Subject: TEST
Folder: HELLO 2559
/bin/sh: HELLO: No such file or directory
Is just a simple echo "HELLO"; in the php script, seems to work fine!
However when I try to actually parse the email using the following code I get the error below in the log:
$rawEmail = '';
if (($fp = fopen('php://stdin', 'r')) !== false) {
while (!feof($fp)) {
$rawEmail .= fread($fp, 1024);
}
fclose($fp);
}
$email = new Zend_Mail_Message(array(
'raw' => $rawEmail
));
From actualaddress#gmail.com Mon Feb 18 20:44:36 2013
Subject: Re: Test
Folder: Fatal error: Class 'Zend_Mail_Message' not found in /home/sy 2747
/bin/sh: Fatal: No such file or directory
This isn't working correctly obviously, I'm not sure why. For one, the From header went through on this one but not the other, that seems intermittent.
Also the directory (/home/sy) is either truncated or something is cutting it off to cause an error.
I'm not familiar with Zend really at all, or procmail. I was happy enough to get this far, just want to parse the email a bit so I can fetch the body of the email and put it somewhere. Someone convinced me this would be a better way than just using IMAP or something and I listened. If anyone's got any wonderful solutions or alternatives I'm all ears. Thanks guys!
You are mixing two related but different pieces of syntax.
If you want to pipe to your script, it should be just
:0
* conditions, maybe
| /path/to/script
or if you want to use the output of the script, something like
:0
* conditions, maybe
`echo HELLO`
would file into a folder named HELLO, i.e. use the output from the script as a literal.
As for the error message from PHP, I imagine you need to add something to PHP's library path (quick googling suggests you should fix the include_path in your php.ini).
What are you trying to accomplish, though? If you just want to send the message where the headers say it should be going, something like
:0
* conditions, maybe
! -t
should get you that. I cannot imagine a situation where you would want to do this (other than if you are trying to solve the wrong problem altogether). If you want to send a truncated copy of the message, something like
:0c
* conditions, maybe
{
:0fw
| head -n 10
:0
! -t
}
would truncate the message to the first ten lines. If you want to truncate to 1024 bytes exactly, that's not much harder.
On the other hand, if you just want to store the message's body (full RFC822 body, i.e. any MIME attachments etc will just be included verbatim, undecoded) you can do that with
:0b
saved/
or maybe if you want PHP there
:0b
| /path/to/script.php
For what it's worth, the error message is being truncated, but that is mainly because you are trying to use it as the name of the script to deliver the email to. If you take out the backquotes, the error message should end up in Procmail's standard error without truncation.

Does PHP Email script leave a record anywhere (like in cPanel?)

The title basically says it all.
We've got a subdomain setup where every single subdomain is the same exact site, but branded for different companies.
There's a PHP script that sends an email based on certain actions when a form is submitted. Is there anyway (possibly in cPanel) to see a record of those sent emails? Or will I have to code something in to that script?
If mail() encounters an error, this usually logs the error in this location: /var/log/messages (unix path)
Otherwise, logging is as easy as adding few extra lines of code when you are calling the mail() function.
$file = "mail.log"; // log file name
$fh = fopen($file, 'a') or die("can't open file"); // create a handle
fwrite($fh, "to: ".$to.",subject: ".$subject.", body: ".$body."\n"); // log email sent
fclose($fh); // close file
It's easiest to do this work in your application itself. Then you have control over the format, and can use that data in your application at a later point, if necessary.

Stopping a rogue PHP script sending E-Mail

Please don't laugh at me but I believe that I just did something extremely stupid. I was working on setting up a newsletter for a site that I am working on, but I tried it out at first when there was a typo. While scanning throughout the database and sending emails, I screwed up on the part that makes it stop. I fixed the code, but the emails are still being send (to my mom :O) and they don't seem to be stoping.
This is the script when I executed it:
$message = $_POST['emailmessage'];
$subject = $_POST['subject'];
$query = mysql_query("SELECT `email` FROM `members` WHERE `active`='1'");
//This line underneath should not be there
$rows = mysql_fetch_assoc($query);
$headers = array(
"From: contact#thestopitcampaign.com",
"Content-Type: text/html"
);
//should be '$rows = mysql_fetch_assoc($query)' instead of '$rows'
while($rows)
{
mail($rows['email'],$subject,$message,implode("\r\n",$headers));
echo "<p>Sent to: " . $rows['email'] . "</p>";
}
I contacted FatCow to see if they could stop the script, but they said that they could not do that and they would have to delete my entire account and put me on a different server. I cannot do that. Is there anyway to generate an error or something that would make the rogue script stop? FYI I do not have SSH access.
--I looked in my php config file and the timeout for a script is 300 seconds. That seems like a lot of emails to send. Is there anyway to stop those emails?
What has been sent can't be stopped any more. But it won't run forever and probably has already stopped.
If the server is not grossly misconfigured by the provider, your script didn't run any longer than a certain time limit, e.g. 60 seconds. Even though messages continue to come through, it's probably no longer running, but the mail server is taking its time to handle all the messages that it created.
I would wait and see - the flood is likely to end soon.
What the provider says about moving the account to a different server doesn't seem to make any sense at all - if there is a rogue process that is sending E-Mail, they should be able to kill it easily. But anyway... I would wait.
My guess is that the emails are just queued up and should eventually stop as php script wont keep executing itself after the page has stopped loading.
I'd suggest you to use a newsletter provider like mailchimp as they are more relyable, are safe and the service is easy to integrate in your website.
Hope that helps!

Php, $result = mysql_query($query) or die ... how can i quickly email a fault?

I would like to add a simple, email me if theres a fault in the query feature.
After
$result = mysql_query($query) or die
What would I add ?
If there is a faulty SQL on frequent access page
or in the event of mysql server down,
is going to flood your inbox.
How about using error_log to log the faulty sql into local disk?
log rotate should be cater too (like log file can be Ymdh.log
Not really ASAP notification
setup a cronjob run every minute
do a line-count on log file
email new lines since last minute
if there is no new line, don't email
store the line-count into a separate file for next execution
So, you will receive an email to indicate whether any new faulty SQL happen last minute, and no alert if nothing went wrong.
Since not able to convince you...
Then you can compare the faulty sql with the error log
If the faulty sql not found in the error log, append into error log, email the faulty sql
You would need to seek for a fashionable way to do string comparison fast in the error log (if the error log is growing too fast)
log rotate can be applied too
you can use a function say
$result = mysql_query($query) or reportErrorData()
function reportErrorData()
{
$fp=popen("/usr/sbin/sendmail -t -oi","w");
fwrite($fp,"To: DEV_EMAIL\n");
fwrite($fp,"From: support#support.com\n");
fwrite($fp,"Subject: System Error\n\n");
fwrite($fp, "Message: error message\n\n")
pclose($fp);
}
you can also pass in parameters to the reportErrorData function such as the sql, _FILE_, _FUNCTION_ etc.. also it will be handy to email any $_SESSION, $_GET or $_POST data, to see who the logged in user was if thats the case and to see where the error occoured i.e which file, function line etc.
hope this helps.
if(!$result = mysql_query($query){
mail(...);
die;
}

Categories