Gmail show Moved Permanently in mail detail - php

I send an email by php code.
I use api Aws\Ses\SesClient to send email.
This is my code:
function sendOrderInfoToCustomer($sTo){
$ch = curl_init($this->sMainUrl."/content_email_order_info.php?orderID=".$this->iOrderID);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$sContentOrderInfo = curl_exec($ch);
curl_close($ch);
$sSubject = $this->oPublicFunction->getSiteName()." [".$this->oPublicFunction->parseFormatTime("d/m/Y H:i A",time())."]";
$this->oPublicFunction->sendMailAWS($sTo,$sSubject,$sContentOrderInfo);
}
function sendMailAWS($sTo,$sSubject,$sBody){
global $aws_access_key, $aws_secret_access_key, $aws_from;
$client = Aws\Ses\SesClient::factory(array(
'version'=> 'latest',
'region' => 'us-east-1',
'credentials' => array(
'key' => $aws_access_key,
'secret' => $aws_secret_access_key
)
));
$request = array();
$request['Source'] = $aws_from;
$request['Destination']['ToAddresses'] = array($sTo);
$request['Message']['Subject']['Data'] = $sSubject;
$request['Message']['Body']['Html']['Data'] = $sBody;
try {
$result = $client->sendEmail($request);
$messageId = $result->get('MessageId');
//echo("Email sent! Message ID: $messageId"."\n");
} catch (Exception $e) {
echo("The email was not sent. Error message: ");
echo($e->getMessage()."\n");
}
}
Send mail is success. But I check mail on gmail I see email is not good.
Please help me fix it.

This is not a result of email, it's actually the content that your CURL is getting from your own server when building the message body. You probably just have to tell curl to follow redirects:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
You may have a more serious problem here, however. Your app (generally) shouldn't be using web requests to itself. The email content should be created via an internal method call that renders the order template, and not by calling a web page. Here, it appears that you've opened the order detail page to the entire web. I.e., anybody on the web can hit content_email_order_info.php?orderID=123 and get the details for any order because there's no authentication going on there. This could be a very serious security breach.

Related

Change Slack Bot Icon from Post PHP

We post a message to a slack channel every time a customer does a specific task. We want to change the bot Icon based on what is being posted in the channel.
Is this possible?
public static function send_to_slack($message,$title = null){
$body = array();
$body['text'] = '';
$body['icon_url'] = '';
if(!empty($title)) $body['text'] .= "*$title*\n";
$body['text'] .= $message;
$iconURL = "https://img.icons8.com/emoji/96/000000/penguin--v2.png";
$body['icon_url'] .= $iconURL;
$json = json_encode($body);
//Test Slack Channel
$slack = "SLACKURL"
$response = wp_remote_post( $slack, array(
'method' => 'POST',
'body' => $json,
)
);
if ( is_wp_error( $response ) ) {
return true;
} else {
return true;
}
}
From Slack:
You cannot override the default channel (chosen by the user who installed your app), username, or icon when you're using Incoming Webhooks to post messages. Instead, these values will always inherit from the associated Slack app configuration.
*** UPDATE
I just ran into this situation. My old app was using the old web hooks. I had to create a new integration and ran into this issue. The simple solution is to install the slack app called "Incoming Webhooks". It's made by slack. It will allow you to generate an older style webhook that will allow you to post to any channel.
Found the correct way. Make sure you enable chat:write.customize in Slack oAuth Scope.
public static function send_to_slack($message,$title = null){
$ch = curl_init("https://slack.com/api/chat.postMessage");
$data = http_build_query([
"token" => "BOT-TOKEN",
"channel" => "CHANNELID", //"#mychannel",
"text" => $message, //"Hello, Foo-Bar channel message.",
"username" => "MySlackBot",
"icon_url" => $iconURL
]);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

Send FCM Push notifcations to specific devices in android app using MySQL query as identification from a PHP script

I want to send FCM push notifications in specific android users only using their token saved in mysql database as identification. here's my current progress
PHP Script Snippet Code: Report_Status.php (File 1)
//Gets the token of every user and sends it to Push_User_Notification.php
while ($User_Row = mysqli_fetch_array($Retrieve_User, MYSQLI_ASSOC)){
$User_Token = $User_Row['User_Token'];
include "../Android_Scripts/Notifications/Push_User_Notification.php";
$message = "Your Report has been approved! Please wait for the fire fighters to respond!";
send_notification($User_Token, $message);
}
PHP code for File 2: Push_User_Notification.php
<?php //Send FCM push notifications process
include_once("../../System_Connector.php");
function send_notification ($tokens, $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = API_ACCESS_KEY',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
}
?>
Problem:
The page is always stuck in Report_Status.php every time I ran the
script. It is supposed to go in Push_User_Notification and return to Report_Status once the process is done. Am I wrong in the implementation of calling the
Push_User_Notification.php or the receiving parameters to
Push_User_Notification.php?
P.S.
Here's my full source code of Report_Status.php in case anyone wants to check it: Report_Status.php
I think the problem you may be having is that you are sending a lot of notifications to several devices in short amount of time. I think it might be being picked up as spaming. My suggestion is sending one notification to multiple devices.
Try changing your code in report_status.php to this.
include "../Android_Scripts/Notifications/Push_User_Notification.php";
$message = "Your Report has been approved! Please wait for the fire fighters to respond!";
while ($User_Row = mysqli_fetch_array($Retrieve_User, MYSQLI_ASSOC)){
$User_Token[] = $User_Row['User_Token'];
}
$tokens = implode(",", $User_Token);
send_notification($tokens, $message);
the idea is that you will collect the user tokens in $User_Token[] array. Then you would comma seperate the tokens and send the message once to all the devices that associate to the tokens. FCM allows you to send to multiple tokens in one go.
updated
$User_Token needs to be an array. so remove the implode. That was my mistake.
Secondly the $message needs to be in the following format.
$message = array(
'title' => 'This is a title.',
'body' => 'Here is a message.'
);
Also another thing to note is that there are 2 types of messages you can send using FCM. Notification Messages or Data Messages. Read more here: https://firebase.google.com/docs/cloud-messaging/concept-options
I dont know if your app is handling the receipt of messages (i dont know if you have implemented onMessageRecieve method) so i would probably suggest making a small change to the $fields array in send_notification function. Adding the notification field allows android to handle notifications automatically if your app is in the background. So make sure you app is in the background when testing. https://firebase.google.com/docs/cloud-messaging/android/receive
$fields = array(
'registration_ids' => $tokens,
'data' => $message,
'notification' => $message
);
So try the code below. I have tried and tested. It works for me. If it does not work. In send_notification function echo $result to get the error message. echo $result = curl_exec($ch); Then we can work from there to see what is wrong. You can see what the errors mean here: https://firebase.google.com/docs/cloud-messaging/http-server-ref#error-codes
include "../Android_Scripts/Notifications/Push_User_Notification.php";
$message = array(
'title' => 'Report Approved',
'body' => 'Your Report has been approved! Please wait for the fire fighters to respond!'
);
while ($User_Row = mysqli_fetch_array($Retrieve_User, MYSQLI_ASSOC)){
$User_Token[] = $User_Row['User_Token'];
}
send_notification($User_Token, $message);

Send SMS Message using PHP

I have developed a system which will send SMS to the customer after making insert to the database
but the system which I worked on is public ie. when my client asked me to buy the system he must insert his SMS API configuration to send a message to his customers
when I searched on the internet I found that every API have a differnt way to send SMS message
I have account in click tell API but when I send message by curl there is nothing happen
the code is
$url = 'https://platform.clickatell.com/messages/http/send?' . http_build_query(
[
'apiKey' => 'oGjnzDdSRhqdhhgnjFj3ZmzYA==',
// 'api_secret' => '4c98619ffb9af51585',
'to' => '94233698311783',
'content' => 'Hello from Netcom'
]
);
echo($url);echo('<br/>');
echo'https://platform.clickatell.com/messages/http/send?apiKey=oGkmzDdSRgekvjFjaZnzYA==&to=905373545631&content=Test+message+text"';
$ch = curl_init($url);
echo($ch);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo $response;
Where my code is wrong and what is the way to send SMS dynamically depending on the client's API info
For quick fix and testing add this in your curl request and check response.(Because url is https)
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
But it is recomonded to downloading a CA root certificate bundle at the curl website and saving it on your server which protect your site
I am not aware of Clickatell, but Aws SNS is quite simple,
$payload = array(
'Message' => $message,
'PhoneNumber' => $mobile,
'MessageAttributes' => $msgAttributes
);
$result = $sns->publish($payload);

SendGrid - Curl php external file attachment broken

I'm using SendGrid for a customer project with curl method.
All works fine but the(ir) file(s) attached on my email sending with SendGrid are broken.
Here is my code :
$documentList = array(
"DOC1.php" => "http://www.customerdomain.com/my/path/where/my/attachment/file/is/myfile.pdf"
);
$params = array(
'api_user' => $user;
'api_key' => $pass,
'x-smtpapi' => json_encode($json_string),
'from' => $from,
'to' => $to,
'subject' => $subject,
'html' => $mailHtml,
'text' => $mailText
);
if(count($documentList)>0){
foreach($documentList as $fileName=>$documentPath){
$params['files['.$fileName.']'] = $documentPath;
}
}
$request = $url.'api/mail.send.json';
// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
When I don't have extension file on my key of array, I've a text file containing the related value.
I think I'm not alone to have this problem, well, if you've any idea to solve this problem, thanks for your help !
The issue you're experiencing is because you are giving SendGrid a URL for file, rather than the file itself, and SendGrid's API needs the file.
To get your code to work, simply change the $documentList variable to:
$documentList = array(
"DOC1.pdf" => "#" . realpath("/path/where/my/attachment/file/is/myfile.pdf")
);
Instructions on this kind of file upload can be found in this StackOverflow Question, but you might otherwise want to use curl_file_create, to do this.
However, perhaps the best/easiest way to do this is to use SendGrid's PHP Library which makes sending attachments, trivially simple.:
require("path/to/sendgrid-php/sendgrid-php.php");
$sendgrid = new SendGrid('username', 'password');
$email = new SendGrid\Email();
$email->addTo('foo#bar.com')->
setFrom('me#bar.com')->
setSubject('Subject goes here')->
setText('Hello World!')->
setHtml('<strong>Hello World!</strong>')
addAttachment("../path/to/file.txt");
$sendgrid->send($email);
My initial issue was because the path is an autogenerated link and that's why I didn't use an url than realpath.
Anyway, I changed my code and I use now the realpath with mimetype mentioned after the file realpath (and # before).
It seems work fine now.
I'll like to thank you for your help.
Regards

Posting to Tumblr with php & Tumblr API

I am trying to post messages automatically to my Tumblr Blog (which will run daily via Cron)
I am using the Official Tumblr PHP library here:
https://github.com/tumblr/tumblr.php
And using the Authentication method detailed here :
https://github.com/tumblr/tumblr.php/wiki/Authentication
(or parts of this, as I don't need user input!)
I have the below code
require_once('vendor/autoload.php');
// some variables that will be pretttty useful
$consumerKey = 'MY-CONSUMER-KEY';
$consumerSecret = 'MY-CONSUMER-SECRET';
$client = new Tumblr\API\Client($consumerKey, $consumerSecret);
$requestHandler = $client->getRequestHandler();
$blogName = 'MY-BLOG-NAME';
$requestHandler->setBaseUrl('https://www.tumblr.com/');
// start the old gal up
$resp = $requestHandler->request('POST', 'oauth/request_token', array());
// get the oauth_token
$out = $result = $resp->body;
$data = array();
parse_str($out, $data);
// set the token
$client->setToken($data['oauth_token'], $data['oauth_token_secret']);
// change the baseURL so that we can use the desired Methods
$client->getRequestHandler()->setBaseUrl('http://api.tumblr.com');
// build the $postData into an array
$postData = array('title' => 'test title', 'body' => 'test body');
// call the creatPost function to post the $postData
$client->createPost($blogName, $postData);
However, this gives me the following error:
Fatal error: Uncaught Tumblr\API\RequestException: [401]: Not
Authorized thrown in
/home///*/vendor/tumblr/tumblr/lib/Tumblr/API/Client.php
on line 426
I can retrieve blog posts and other data fine with (example):
echo '<pre>';
print_r( $client->getBlogPosts($blogName, $options = null) );
echo '</pre>';
So it seems it is just making a post that I cant manage.
In all honesty, I don't really understand the OAuth Authentication, so am using code that more worthy coders have kindly provided free :-)
I assume I am OK to have edited out parts of the https://github.com/tumblr/tumblr.php/wiki/Authentication as I don't need user input as this is just going to be code ran directly from my server (via Cron)
I have spent days looking around the internet for some answers (have gotten a little further), but am totally stuck on this one...
Any advice is much appreciated!
It looks like the parts that you removed in the code pertained to a portion of the OAuth process that was necessary for the desired action.
// exchange the verifier for the keys
You might try running the Authentication Example itself and removing the parts of the code that you've removed until it no longer works. This will narrow down what's causing the issue. I'm not very familiar with OAuth personally, but this looks as though it would be apart of the problem as one of the main portions you took out was surrounding the OAuth process exchanging the verifier for the OAuth keys.
function upload_content(){
// Authorization info
$tumblr_email = 'email-address#host.com';
$tumblr_password = 'secret';
// Data for new record
$post_type = 'text';
$post_title = 'Host';
$post_body = 'This is the body of the host.';
// Prepare POST request
$request_data = http_build_query(
array(
'email' => $tumblr_email,
'password' => $tumblr_password,
'type' => $post_type,
'title' => $post_title,
'body' => $post_body,
'generator' => 'API example'
)
);
// Send the POST request (with cURL)
$c = curl_init('api.tumblr.com/v2/blog/gurjotsinghmaan.tumblr.com/post');
//api.tumblr.com/v2/blog/{base-hostname}/post
//http://www.tumblr.com/api/write
//http://api.tumblr.com/v2/blog/{base-hostname}/posts/text?api_key={}
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($c);
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
// Check for success
if ($status == 201) {
echo "Success! The new post ID is $result.\n";
} else if ($status == 403) {
echo 'Bad email or password';
} else {
echo "Error: $result\n";
}
}
https://howtodofor.com/how-to-delete-tumblr-account/

Categories