Mailgun API on Google App Engine - php

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?

Related

how to send email using infobip service

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];

Get error code 14 when sending SMS using smsapi

I am trying to send SMS using the API from smsapi.pl. Currently I am getting error code 14 which means "Invalid sender field".
Code:
$access_token = 'XXXXXX'; //sms api access token
$numbers = '+7XXXXXXXX';
$params = array(
'to' => $numbers,
'from' => 'Info',
'message' => 'message text',
);
$c = curl_init();
curl_setopt($c, CURLOPT_URL, 'https://api.smsapi.pl/sms.do');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $params);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer $access_token"
));
$content = curl_exec($c);
$http_status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
print_r($content);
The translated documentation says:
Only verified names are accepted. (& from = aktywna_nazwa). The sender field should be added after logging in to the SMSAPI website, Sender Field .
Did you add the sender name Info on the website?

Codeigniter Plivo PHLO - Curl sending json

I would like to understand why the "HTTPHEADER" option is not interpreted correctly. The sending is going well, but the Plivo server can't read my request which doesn't arrive in JSON.
Below, the helper Codeigniter:
function launchCall($from, $to, $position, $message){
$CI = &get_instance();
$CI->load->config('plivo');
$data = array("from" => $from, "to" => $to, "position" => $position, "message" => $message);
$data_string = json_encode($data);
$url = 'https://phlorunner.plivo.com/v1/account/YYYYYYYYYYYYYYYYYYYYYYYYYYYXXXXXXXXXXXXXXX';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json','Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_USERPWD, $CI->config->item('AUTH_ID') . ":" . $CI->config->item('AUTH_TOKEN'));
$result = curl_exec($ch);
return $result;
}
Thank you in advance for your help.
There are no issues with your code. The payload is received fine. The reason your attempt is failing is minor and could be fixed in a jiffy.
The Caller ID (from) needs to be a valid phone number in E.164 phone number format. Add Country code to your Caller ID and your PHLO should work fine.

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.

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