I am trying to solve some errors on a Script piped by postfix, and when I send an error to Bugsnag I would like to send as well the raw email.
I grab the email content with
$handle = fopen("php://stdin", "r");
$email = stream_get_contents($handle);
The doc says that you can add custom metaData with the setMetaData function, but in my case this will be true only for this script.
Figured this out. Actually, the setMetaData method can be called at any time during the script.
Related
I have a PHP app that I built using the Slim routing framework. The app needs to send dynamic emails from orders so that users can just respond to those emails and the response goes right back into the app (stored in MySQL). I can easily create the dynamic address per order and that goes out just fine. My problem is getting it back.
I setup a subdomain (mailer.example.com) and in cPanel I setup a forwarder to catch all mail to that subdomain and forward it to a specific PHP file. That php file reads stdin and grabs the mime message and currently writes it to a file:
#!/usr/bin/php -q
<?php
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd))
{
$email .= fread($fd, 1024);
}
fclose($fd);
$filename = "mail_".date("mdYHis").rand(1,99999999);
file_put_contents("mailfiles/".$filename, $email);
header("Location: http://www.example.com/public/mailer/process/".$filename);
As you can see, at the end I would like to forward this to my actual app which has all the database calls and other routines to process the email. But I don't know how to get the request into the app. It seems to ignore the header call above.
Am I doing this all wrong or should I just do all the processing I need in this script? I realize that would maybe be the easiest path forward but I'm also trying to use a mail parse library that fits nicely in my app.
Not sure if that all makes sense. Let me know what other info you need.
I think what you're looking to do isn't to return an HTTP response with a Location header, but rather initiate an HTTP request to your web server.
In that case, you should replace your header() call with:
$ch = curl_init('http://localhost/public/mailer/process/' . $filename);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
Note that your script (the one called to save the mail contents) will wait for your app to finish processing the request. If this is an issue for you, you'll need to use a technique to run PHP processes in the background.
You can read a mailbox with php and parse the emails that way.
// To connect to a POP3 server on port 110 on the local server, use:
$mbox = imap_open ("{localhost:110/pop3}INBOX", "user_id", "password");
I would not, create a custom reply-email/mailbox, per order, instead add the order to the subject as a reference. That way you only need to parse one mailbox.
Find the imap functionality here
I have been trying to set up a Shopify webhook (documentation here) through the Shopify admin section. So far I have not been able to grab any of the data being sent in a test webhook using multiple methods, but I know it is sending because I created a Requestbin and have been seeing data come through.
I found this chunk of code here on Stackoverflow.
header('Access-Control-Allow-Origin: *');
$postdata = file_get_contents("php://input");
$file = "log.txt";
$current = file_get_contents($file);
$current .= $postdata;
file_put_contents($file, $current);
echo $current;
So far it is the only code I have been able to use to actually see any JSON. But Im not wanting to write the JSON to the "log.txt" file every time the webhook is fired. But once I try to remove or adjust the code in anyway I no longer see any JSON. It seems that I have to write $postdata to a file, and then retrieve the contents to get an array.
Is is possible to access the JSON without having to first write it to another file?
So to begin, my fundamental thought process was flawed on how webhooks worked.
header('Access-Control-Allow-Origin: *');
$postdata = file_get_contents("php://input");
$file = "log.txt";
$current = file_get_contents($file);
$current .= $postdata;
file_put_contents($file, $current);
echo $current;
The reason this code needed to write the contents of file_get_contents("php://input") to the text file was because the POST request sent by the Shopify webhook, only hits the page briefly. As soon as you refresh the page, you lose all the POST data...seems obvious I know. That said I needed a way to determine if the JSON Shopify is sending out is working all fine on my server. So I created this...
$json = json_decode(file_get_contents("php://input"));
file_put_contents('./realRequests.txt', $json->id . "\n", FILE_APPEND);
What this does is capture the ID of an order everytime Shopify fires the webhook, and wrties it to a new line in a requests file. That way you know you are able to recive working code from shopify.
I'm developing a telegram bot using php and a web hook. All it's fine but sometimes I would like to "wait for a reply" from the user. For example:
If the client write /info without any parameters I would like to show a "usage" message and ask&wait for a ID parameter.
I know there is a property "ForceReply" to force for a reply, but when I set it up nothing happens, and I don't know how to know if the client message its a reply for my question.
Do I have to put my php server on hold? (I think it would be a bad practice) Do I have to whait for a type of message?
Thanks
When you use getUpdates or receive updates via a webhook, the update message will contain a field like reply_to_message field. You can use this to compare it to the message you sent.
If you are running your script via webhooks I would assume that it only executes when it receives a message. If so, I would suggest you use something like memcache/redis to store the message you are expecting a reply for and then when the reply comes, you can compare it to the value stored:
<?php
// This script triggers as a webhook
$message = file_get_contents('php://input');
$message = json_decode($message, true);
$cache = new RedisClient('localhost');
if ($message->reply_to_message == $cache->get('original.message.id'))
{
var_dump('message reply received');
}
The example above is some "pseudo" code that you can use in a webhook to check for a reply to a specific message.
I'm currently connecting to Mailgun's webhooks so I can get any emails sent through and pass it somewhere else.
I've managed to deal with the subject, from and body inputs of the API, but I'm currently having trouble grabbing any attachments to an email.
Mailgun says that they send through a multipart file, but I have tried to catch it and write out the info into a file, but it comes back as an empty array...
$file = fopen(__DIR__ . '/files.txt','w') or die("Can't open file.");
ob_start();
var_dump($_FILES);
fwrite($file, ob_get_clean()) or die("Can't save to file.");
fclose($file);
Within the Post information, I get an attachments parameter which has such things like a url, content-type, name etc. I have tried to go the the url of each attachment, but I get a login box asking for a username and password and says "Server says: MG API". I have no idea if I need to pass something here so I can retrieve the attachment as a file to pass it on.
If anyone could help me out here, then I would truly appreciate it.
Thank you.
Great question,
As you said from the inbound webhook coming from mailgun to your server you're being sent a URL from which to fetch the attachment.
In my case it's
https://api.mailgun.net/v2/domains/<<<MYDOMAIN>>>>/messages/WyJjZTL.....SJd/attachments/0
Now if I authenticate to that URL with my username(api) and password, I can fetch the attachement. example:
https://API:PASSWORD#api.mailgun.net/v2/domains/<<<MYDOMAIN>>>>/messages/WyJjZTL.....SJd/attachments/0
The password is simply the API key, that you can find in the control panel on the left. (https://mailgun.com/cp)
hope this works for you as well as its working for me,
best regards
I have successfully configured Google API Calendar Event Change Push Notifications. My notification page gets called as it should be when I set up the notification or change the calendar.
BUT...being a bit of a PHP dunce I don't understand how to see the content of the notification.
SO (MAIN QUESTION)...how do I get at the variables in the notification? The notification calls my designated page. What are the variables? How are they sent? How are they organized?
There is the larger question of how one should determine the content of a call to a page in PHP. I tried all manner of var_dump, print_r, etc. All I get with:
<?php
$arr = get_defined_vars();
error_log(print_r($arr),0);
?>
is: 1 Yes, just the number 1 in my error_log.
The way I read the respose is:
$content = apache_request_headers();
If you print $content the data is self explenatory. However; for more information hwo to read the header check the link below:
https://developers.google.com/google-apps/calendar/v3/push#receiving
Thanks. it's working. i wrote to file.
$content = apache_request_headers();
get_contents($content);
function get_contents($result) {
$fp = fopen('config.json', 'w');
fwrite($fp, json_encode($result));
fclose($fp);
}