how to send email using infobip service - php

I am now developing a web application and it needs to send emails.
I first used smtp service and for some reason I have chosen infobip.com for emailing service.
It has good documentations on api so I followed the tutorial.
The problem is even though I think I followed all steps I have some error like "bad request"
There is api for sending sms using infobip service.
(https://github.com/pnlinh)
But no api for sending emails.
private $from = "some person";
private $subject = "test subject";
private function setPostData($to, $text)
{
return [
'from' => $this->from,
'to' => (array) $to,
'subject' => $this->subject,
'text' => $text,
];
}
public function testFunction()
{
$header = ['Content-Type:application/json', 'Accept:application/json'];
$postUrl = 'https://api.infobip.com/email/1/send';
$to = 'scar20181228#gmail.com';
$text = 'test email';
$postDataJson = json_encode($this->setPostData($to, $this->subject, $text));
// Set retry time if response is null or empty
$retrytime = 0;
retry_from_here:
$ch = curl_init();
// Setting options
curl_setopt($ch, CURLOPT_URL, $postUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'username'.':'.'password');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postDataJson);
// Response of the POST request
$response = curl_exec($ch);
if ($response === '' && $retrytime <= static::RETRY_TIME) {
$retrytime++;
goto retry_from_here;
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$responseBody = json_decode($response);
curl_close($ch);
return [$httpCode, $responseBody];

Related

I am looking for some simple PHP code to send Twilio SMS message (without a framework required)

OK, I know I am a noob but I have gotten by with PHP for a while by just writing code in a text editor. With Twilio samples everything is prefaced with...
// Update the path below to your autoload.php,
// see https://getcomposer.org/doc/01-basic-usage.md
require_once '/path/to/vendor/autoload.php';
like they show here..
https://www.twilio.com/docs/sms/send-messages
do I need this framework? I just want to send a single SMS message (or maybe loop through a bunch)
Something to get you started. Replace the ... things.
<?php
$twilio_account_sid = "AC...";
$twilio_auth_token = "2d...";
$twilio_phone_number = "+1...";
$payload = [
'From' => $twilio_phone_number,
'To' => '+1...',
'Body' => 'This is the body of the message'
];
$url = 'https://api.twilio.com/2010-04-01/Accounts/' . $twilio_account_sid . '/Messages.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $twilio_account_sid . ':' . $twilio_auth_token);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
var_dump( $status );
var_dump( $response );
?>

JSON RPC 2.0 API Call using CURL in PHP - Changelly API

I am trying to call changelly API with below codes but it is returning "Unauthorized" in response.
Appreciate if someone can help in identifying the mistake I am making in below code.
$API_URL = 'https://api.changelly.com';
$API_KEY = 'XXXXX';
$API_SECRET = 'XXXXX';
$message = array();
$message['jsonrpc'] = '2.0';
$message['method'] = 'getMinAmount';
$message['params'] = array('from' => 'BTC', 'to' => 'LTC');
$message['id'] ='12345';
$data = json_encode($message);
$sign = hash_hmac('SHA512', $data, $API_SECRET);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $API_URL);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_GET, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json",'Authorization:api-key: '.$API_KEY.':sign: '.$sign));
$final_result = curl_exec($ch);
curl_close($ch);
echo '<pre>';
print_r($final_result);
Changelly API guide is at https://changelly.com/developers
Thanks
in your code you have wrong set of headers.
please check this example: https://github.com/changelly/changelly-examples/blob/master/php/example.php, hope it helps.

how to send a notification for a theme from my server to firebase

I am sending a message from my server to firebase for a specific topic, I get back an id, but it does not get a notification to the app when I do it from the console to that topic the notification arrives.
How should I do it for a specific topic but that is a notification?
PHP:
<i>
<?php
function send_notification ($message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'to' => "/topics/news",
'data' => $message
);
$headers = array(
'Authorization: key=AAAApDF81wE:APA91bG7g.....',
'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);
return $result;
}
$message = array("message" => " FCM PUSH NOTIFICATION TEST MESSAGE");
$message_status = send_notification($message);
echo $message_status;
?>
</i>
I just changed this
$fields = array(
'to' => "/topics/news",
'notification' => $message
);
i Was not doing proper handling in the app, i should handle the logic and code implementation on how to handle incoming messages within the onMessageReceived method.

Mailgun API on Google App Engine

I'm trying to send emails via Mailgun API on Google App Engine PHP, however, I keep getting a 401 error on my requests.
define(DOMAIN,'sandbox-----------.mailgun.org');
define(MAILGUN_API,'key-xxxxxxxxxxxxx');
function mg_send($to, $subject, $message) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'api: '.MAILGUN_API);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$plain = strip_tags(nl2br($message));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/'.DOMAIN.'/messages');
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'from' => 'support#'.DOMAIN,
'to' => $to,
'subject' => $subject,
'html' => $message,
'text' => $plain)
);
$j = json_decode(curl_exec($ch));
$info = curl_getinfo($ch);
if($info['http_code'] != 200) {
echo($info['http_code']);
}
curl_close($ch);
return $j;
}
$r = mg_send('authorized#email.com','Test Email','This is the test email.');
I've authorized the email I'm trying to send to, verified my account, triple checked my API Key and made sure curl is enabled in my php.ini. Any insight as to why this is happening?

Website inducing alert on mobile phone

I am building a php webpage to be browsed on computers, which is having some email sending and calendar functions.
I would like to try, when sending email messages or creating calendar events from computer browsers, will it be possible to trigger alerts, such as sounds and vibrations in iPhone and Androld systems?
If not, then I may have to do some apps. Thanks!
I would do something like this for android phones:
function sendMessageToPhone($authCode, $deviceRegistrationId, $msgType, $messageText, $infoType, $messageInfo) {
$headers = array('Authorization: GoogleLogin auth=' . $authCode);
$data = array(
'registration_id' => $deviceRegistrationId,
'collapse_key' => $msgType,
'data.message' => $messageText,
'data.type' => $infoType,
'data.data' => $messageInfo
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
if ($headers)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}

Categories