I am trying to use my localhost to connect to another server remotely and execute a mail function so i use get method to send the email and the subject
here is my localhost file :
<?php
for ($email=0; $email< $n_emails; $email++){
$urlsmtp = $link.'?email='.$destino.'&subject='.$tornado.'&msg='.$msgrand.'&name='.$_POST['naming'].'&from='.$_POST['localting'];
//this link is something like http://test.com/RemoteSmpt.php?email=test#mail.com &subject=test&msg=test&name=test&from=test
$urlsmtp = str_replace(" ", "%20", $urlsmtp);
$response = file_get_contents($urlsmtp);
}
?>
and the file RemoteSmpt.php which i will put it in the server contains this code :
<?php
if ( isset( $_GET['email']) && $_GET['subject'] && $_GET['msg'] && $_GET['name'] && $_GET['from']) {
$email = $_GET['email'];
$subject = $_GET['subject'];
$msg = $_GET['msg'];
$name = $_GET['name'];
$from = $_GET['from'];
$headers = 'From: '.$name.'<'.$from.'>\n';
$enviar = mail($email, $subject, $msg, $headers);
if ($enviar){
echo ('<font color="green"> -'. $email .' 0k -- With Subject : '.$subject.'</font><br>');
} else {
echo ('<font color="red"> -'. $email .' Not Sended -- With Subject : '.$subject.'</font><br>');
}
}else
{echo 'Invalid Data !<br>';}
?>
so after i upload the RemoteSmpt.php file and try this is what i got :
Warning: file_get_contents(http://...#mail.com&subject=tset&msg=test&name=test&from=test): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in Mylocalhost file !
Please any idea how to fix this code !
You need to properly encode special characters in the URL parameters, which can be done using urlencode().
You can use the http_build_query() function to build a query string from an associative array, and properly encode everything.
$params = http_build_query([
'email' => $destino,
'subject' => $tornado,
'msg' => $msgrand,
'name' => $_POST['naming'],
'from' => $_POST['localting']
]);
$urlsmtp = $link . '?' . $params;
Related
I'm trying to create a php that sends via email all possible information about the request. Currently I'm using something like:
Email request php:
<?php
$remoteIp = $_SERVER['REMOTE_ADDR'];
$remoteHost = $_SERVER['HTTP_HOST'];
$remoteRef = $_SERVER['HTTP_REFERER'];
$remoteUrl = $_SERVER['REQUEST_URI'];
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$yourEmailAddress = "user#mail.com";
$emailSubject = "New request from: ".$remoteIp;
$emailContent = "The URL Request was made to: $remoteUrl
The request REFERER was: $remoteRef
The IP was $remoteIp
The User Agent was: $userAgent";
// send the message
mail($yourEmailAddress, $emailSubject, $emailContent);
?>
I like to add the Entire http request (GET OR POST) to the email been send so $emailContent it look like this:
$emailContent = "The URL Request was made to: $remoteUrl
The request REFERER was: $remoteRef
The IP was $remoteIp
The User Agent was: $userAgent"
The Full request was:
$fullrequest";
Looking around I found this https://gist.github.com/magnetikonline/650e30e485c0f91f2f40 which allows you to create file but I'm not sure on how to put it together with my PHP so it sends me an email with the request. (Creating the file is not necessary)
This PHP I like to integrate to my Email request php
<?php
// https://gist.github.com/magnetikonline/650e30e485c0f91f2f40
class DumpHTTPRequestToFile {
public function execute($targetFile) {
$data = sprintf(
"%s %s %s\n\nHTTP headers:\n",
$_SERVER['REQUEST_METHOD'],
$_SERVER['REQUEST_URI'],
$_SERVER['SERVER_PROTOCOL']
);
foreach ($this->getHeaderList() as $name => $value) {
$data .= $name . ': ' . $value . "\n";
}
$data .= "\nRequest body:\n";
file_put_contents(
$targetFile,
$data . file_get_contents('php://input') . "\n"
);
echo("Done!\n\n");
}
private function getHeaderList() {
$headerList = [];
foreach ($_SERVER as $name => $value) {
if (preg_match('/^HTTP_/',$name)) {
// convert HTTP_HEADER_NAME to Header-Name
$name = strtr(substr($name,5),'_',' ');
$name = ucwords(strtolower($name));
$name = strtr($name,' ','-');
// add to list
$headerList[$name] = $value;
}
}
return $headerList;
}
}
(new DumpHTTPRequestToFile)->execute('./dumprequest.txt');
Can any one help me to add class DumpHTTPRequestToFile to $fullrequest?
Something like
<?php
$message="The following request was made:\n";
foreach($_REQUEST as $k=>$v){
$message.=$k." : ".$v."\n\n";
}
mail($to_address, $subject, $message, $headers);
?>
Replace $_REQUEST with the super global of your choice. Or run similar loops across multiple superglobals ($_SERVER, etc) if $_REQUEST doesn't have all you want in it.
My code is working when I am using a direct internet (open network). but when I tried to use a private network (which i am currently using in my company) I am getting this error.
Warning: Invalid argument supplied for foreach() in E:\Files\xampp\xampp\htdocs\nexmo\src\NexmoMessage.php on line 228
Below is my code for sending the message..
<?php
include ( "src/NexmoMessage.php" );
if(isset($_POST['sendMessage'])){
$to = $_POST['to'];
$message = $_POST['smsMessage'];
// Step 1: Declare new NexmoMessage.
$nexmo_sms = new NexmoMessage('xxxxxxxx', 'xxxxxxxxx');
// Step 2: Use sendText( $to, $from, $message ) method to send a message.
$info = $nexmo_sms->sendText( $to, 'NexmoWorks', $message );
// Step 3: Display an overview of the message
//echo $nexmo_sms->displayOverview($info);
// Done!
}
?>
and below is the code where i am having an error.
foreach($obj as $key => $val){
// If we come across another class/array, normalise it
if ($val instanceof stdClass || is_array($val)) {
$val = $this->normaliseKeys($val);
}
// Replace any unwanted characters in they key name
if ($is_obj) {
$new_obj->{str_replace('-', '', $key)} = $val;
} else {
$new_obj[str_replace('-', '', $key)] = $val;
}
}
I've got a basic bot set up that will send text back to the user, now I also want to send the user an audio message. But that I cannot do, here is the code.
I'm also using this https://github.com/Eleirbag89/TelegramBotPHP to send the audio
include("Telegram.php);
define('BOT_TOKEN', 'tokentoken');
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
$telegram = new Telegram(BOT_TOKEN);
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$chatID = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];
$reply = sendMessage($message);
$sendto =API_URL."sendmessage?chat_id=".$chatID."&text=".$reply;
file_get_contents($sendto);
function sendMessage(&$string) {
switch ($string) {
case "Hi":
$message = "Hi Back";
sendAudio();
break;
case "Bye":
$message = "Bye Bye";
break;
default:
$message = "Default";
}
return $message
}
func sendAudio() {
$sound = curl_file_create('sampleAudio.mp3', 'audio/mp3');
$newContent = array('chat_id' => $chatID, 'audio' => $sound);
$telegram->sendAudio($newContent);
}
Calling the code outside of the functions works, but than the user gets the file each time they type something. I experimenting so a bit of explanation would be great.
You have some errors:
First line " is unclosed
Last function, you've typed "func" instead of "function"
In sendAudio() you've used $chatID but you've to pass it as a parameter of sendAudio() or to set is as global.
i want to send parameters to url with GET method , i used file_get_content to do this.
my code is :
$app_url = "http://localhost/index.php?number=%s&service_id=%s&shortcode=%s&msg=%s" ;
$msg = "Thank you for joining us! خوش آمدید";
$msg = urlencode($msg);
$address = sprintf($app_url , $current_user,$service_id,$scode,$msg);
$ret = file_get_contents($address);
echo $ret
it works , and dont have problem , but when my $msg is long, i get warning message and parameters don't send to url:
Warning: file_get_contents(http://localhost/send_sms/index.php?number=123&service_id=test&shortcode=123&msg=Thank+u%%%%s%21%0A%D%%%%%8%B4%D9%85%D8%A7+%%%%%%D8%A8%D8%A7+%D9%85%%%%D9%%%%88%D9%81%D9%8
I'm trying to send a json object as a POST command using the following:
$uri = "http://amore-luce.com/product_create";
$product = $observer->getEvent()->getProduct();
$json = Mage::helper('core')->jsonEncode($product);
Mage::log(" Json={$json}", null,'product-updates.txt');
// new HTTP request to some HTTP address
$client = new Zend_Http_Client('http://amore-luce.com/product_create');
// set some parameters
$client->setParameterPost('product', $json);
// POST request
$response = $client->request(Zend_Http_Client::POST);
When I view the $json the data is there and all looks good - however the POST is not sending the json data. I'm capturing it using a simple email form that should send me the response:
<?php
$webhookContent = "";
$ref = "";
$webhook = fopen('php://input' , 'rb');
while (!feof($webhook)) {
$webhookContent .= fread($webhook, 4096);
}
fclose($webhook);
$headers = array();
foreach($_SERVER as $key => $value) {
if (substr($key, 0, 5) <> 'HTTP_') {
continue;
}
$header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
$headers[$header] = $value;
}
foreach ($headers as $header => $value) {
$ref .= "$header: $value <br />\n";
}
$post = file_get_contents('php://input');
$to = "address#my-email.com"; //the address the email is being sent to
$subject = "This is the subject"; //the subject of the message
$msg = "This is the message - Webhook content: ".$webhookContent." This is url: ".$ref; //the message of the email
mail($to, $subject, $msg, 'From: PHP Scriptv2 <noreply#domain.com>'); //send the email.
echo ($_SERVER['HTTP_REFERER']."<br>".$_SERVER['REQUEST_URI']);
?>
This page works with other json POST requests I've sent to it. I'm fairly new at sending POST requests so any help would be greatly appreciated.
Try change :
$client->setParameterPost('product', $json);
to :
$client->setHeaders('Content-type','application/json');
$client->setParameterPost('product', $json);
or use:
$client->setRawData($json, 'application/json');
So Update / Answer changing how I did it to these lines of code:
$uri = "http://requestb.in/p6p4syp6";
$product = $observer->getEvent()->getProduct();
$json = Mage::helper('core')->jsonEncode($product);
Mage::log(" Json={$json}", null,'product-updates.txt');
$client = new Zend_Http_Client($uri);
$client->setRawData($json, null)->request('POST');
Changing the SetRawData($json, null) was the bit - using SetRawDate($json, 'application/json') caused it to fail.
Thanks Voodoo417 - I'll also try your suggestion and see if that lets me set the correct type