I have been using the mailgun API to send emails in my PHP application, When I add the attachment parameter as per the documentation, I got an error:Invalid resource type: array in /var/www/html/vendor/guzzlehttp/psr7/src/functions.php
Can someone assist?
$mgClient = new Mailgun('xxx');
$domain = "xxx";
$parameters = array(
'from' => 'xxx',
'to' => $to,
'subject' => $subject,
'text' => $text,
'attachment' = [
[
'filePath' => $attachment,
'filename' => $file_name,
]
];
);
$result = #$mgClient->sendMessage("$domain", $parameters);
I found a solution by adding the attachment array as a third parameter of the sendMessage()
$mgClient->sendMessage("$domain", $parameters, ['attachment' => ['filePath' => $file_path]]);
Related
The following code subscribes to an SNS HTTP Endpoint:
$protocol = 'http';
$endpoint = 'http://test.com/endpoint.php';
$filterPolicyTest = array(
'test' => ['1','2','3']
);
$filterPolicyString = json_encode($filterPolicyTest);
$result = $this->awsClient->subscribe([
'Protocol' => $protocol,
'Endpoint' => $endpoint,
'ReturnSubscriptionArn' => true,
'TopicArn' => 'myTopicARN',
'Attributes' => [
'FilterPolicy' => $filterPolicyString,
],
]);
After confirming the subscription, I can view the subscription in the AWS console and see the Filter policy.
Now I try the same thing but with an email:
$protocol = 'email';
$endpoint = 'myemail#gmail.com';
$filterPolicyTest = array(
'test' => ['1','2','3']
);
$filterPolicyString = json_encode($filterPolicyTest);
$result = $this->awsClient->subscribe([
'Protocol' => $protocol,
'Endpoint' => $endpoint,
'ReturnSubscriptionArn' => true,
'TopicArn' => 'myTopicARN',
'Attributes' => [
'FilterPolicy' => $filterPolicyString,
],
]);
No errors are thrown. I successfully receive a confirmation email and click the link to subscribe. But, in the AWS console for this subscription, I see:
The FilterPolicy attribute is completely ignored.
I am using the PHP SDK version 3. Here is the documentation for this request. I tried submitting a FilterPolicy both as an array and json encoded string with no luck.
I am trying to send email to multiple receivers and I get the error below
Address in mailbox given [2#test.com,3#test.com,4#test.com] does not
comply with RFC 2822, 3.6.2.
This is how my data looks like:
array:6 [▼
"_token" => "xlhKNDnqAuvQpurPpDM2eGseNanUm5P1NBbPA71Z"
"from" => "test#test.com"
"mainpeople" => "1#test.com" //to
"ccpeople" => "2#test.com,3#test.com,4#test.com" //cc
"subject" => "testing mail"
"messagenote" => "<p>this is a test mail.<br></p>"
]
Code
$data = array(
'from' => $request->from,
'to' => $request->mainpeople,
'ccpeople' => array($request->ccpeople),
'subject' => $request->subject,
'messagenote' => $request->messagenote,
);
Mail::to($data['to'])->cc($data['ccpeople'])->send(new ComposeMail($data));
Also, I tried to loop CC emails but the result was the same:
$data = array(
'from' => $request->from,
'to' => $request->mainpeople,
'ccpeople' => array($request->ccpeople),
'subject' => $request->subject,
'messagenote' => $request->messagenote,
);
foreach($data['ccpeople'] as $people){
$cc = $people;
}
Mail::to($data['to'])->cc($cc)->send(new ComposeMail($data));
Any idea how to fix that?
You can't use array() directly on string to convert it to array:
your $data should be as it is:
$data = array(
'from' => $request->from,
'to' => $request->mainpeople,
'ccpeople' => $request->ccpeople,
'subject' => $request->subject,
'messagenote' => $request->messagenote,
);
Instead of
foreach($data['ccpeople'] as $people){
$cc = $people;
}
try explode() like below :
$cc = explode(',',$data['ccpeople']);
and pass it to your mail function:
Mail::to($data['to'])->cc($cc)->send(new ComposeMail($data));
I'm trying to send notification using a cron job. I migrated from GCM to FCM. In my server side, I changed https://android.googleapis.com/gcm/send to https://fcm.googleapis.com/fcm/send and also updated on how to request the data changing registration_ids to to. Checking the json passed it is a valid json but I'm having an error Field "to" must be a JSON string. Is there anyway to solve this?
Here is my code
function sendNotificationFCM($apiKey, $registrationIDs, $messageText,$id) {
$headers = array(
'Content-Type:application/json',
'Authorization:key=' . $apiKey
);
$message = array(
'to' => $registrationIDs,
'data' => array(
"message" => $messageText,
"id" => $id,
),
);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'https://fcm.googleapis.com/fcm/send',
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode($message)
));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
Try explicitly converting $registrationIDs to string.
$message = array(
'to' => (string)$registrationIDs,
'data' => array(
"message" => $messageText,
"id" => $id,
),
);
Edited Answer
The 'to' parameter requires a string - which is the recipient of the message.
The $registrationIDs could be passed a separate parameter (as string array) to 'registration_ids'
Edit your code to something like this:
$recipient = "YOUR_MESSAGE_RECIPIENT";
$message = array(
'to' => $recipient,
'registration_ids' => $registrationIDs,
'data' => array(
"message" => $messageText,
"id" => $id,
),
);
Where $recipient is
a registration token, notification key, or topic.
Refer this:
Firebase Cloud Messaging HTTP Protocol
Try making to as a string:
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
...
},
}
// if you are using an array like
$fcm_ids = array();
$fcm_ids[] = "id_1";
$fcm_ids[] = "id_2";
$fcm_ids[] = "id_3";
//.
//.
//.
$fcm_ids[] = "id_n";
// note: limit is 1000 to send notifications to multiple ids at once.
// in your messsage send notification function use ids like this
$json_data = [
"to" => implode($fcm_ids),
"notification" => [
"title" => $title,
"body" => $body,
],
"data" => [
"str_1" => "something",
"str_2" => "something",
.
.
.
"str_n" => "something"
]
];
I had a similar problem. It turns out when I retrieved the Registration Token from my Firebase database (using this Firebase PHP Client), it was returned with double quotes at the beginning and at the end of the token. So I had to remove the first and last characters from the token before I could use it. The code below solved my problem
substr($registrationToken, 1, -1)
I am trying to send email using AWS SES using the following PHP script:
<?php
require_once("phar://aws.phar");
use Aws\Ses\SesClient;
//Open client
$client = SesClient::factory(array(
"key" => "key",
"secret" => "secret",
"region" => "region"
));
$subject = "subject";
$messageText = "text message";
$messageHtml = "<h1>formatted message</h1>";
//Send email
try{
$response = $client->sendEmail(
array(
'Source' => 'verified_email#domain.com',
'Destination' => array(
'ToAddresses' => array('an_address#domain.com')
),
'Message' => array(
'Subject' => array('Data' => $subject),
'Body' => array('Text' => array('Data' => $messageText)),
'Html' => array('Data' => $messageHtml)
)
)
);
}catch(Exception $e){
//An error happened and the email did not get sent
echo($e->getMessage());
}
?>
Whenever I run this, it goes to the catch clause and prints to the screen the message:
Unable to determine service/operation name to be authorized
This doesn't really give me any information on what's wrong and there's no documentation on the API page about this. Any ideas?
I am currently building a e-mail client (inbound and outbound sending) using Mandrill as the e-mail sending / inbound service and Laravel 3.x.
In order to send messages, I am using the HTTPful bundle with the Mandrill using the following code in my mail/compose POST method.
$url = 'https://mandrillapp.com/api/1.0/messages/send.json';
$data = array(
'key' => '{removedAPIkey}',
'message' => array (
'to' => array( array( "email" => $_to ) ),
'from_name' => Auth::user()->name,
'from_email' => Auth::user()->email,
'subject' => $_subject,
'html' => $_body
),
'async' => true
);
$request = Httpful::post($url)->sendsJson()->body($data)->send();
Link to better formatted code above: http://paste.laravel.com/m79
Now as far as I can tell from the API log, the request is correctly made (with the expected JSON) and a response of the following format is sent back:
[
{
"email": "test#test.com",
"status": "queued",
"_id": "longmessageID"
}
]
However, what I am trying to do is access the response from the request (specifically the _id attribute), which is in JSON. Now as far as I'm aware, the HTTPful class should do this automatically (using json_decode()). However, accessing:
$request->_id;
is not working and I'm not entirely sure how to get this data out (it is required so I can record this for soft-bounce, hard-bounce and rejection messages for postmaster-like functionality)
Any assistance would be appreciated.
Edit
Using the following code, results in the mail being sent but an error returned:
$url = 'https://mandrillapp.com/api/1.0/messages/send.json';
$data = array(
'key' => '{removedAPIkey}',
'message' => array (
'to' => array( array( "email" => $_to ) ),
'from_name' => Auth::user()->name,
'from_email' => Auth::user()->email,
'subject' => $_subject,
'html' => $_body
),
'async' => true
);
$request = Httpful::post($url)->sendsJson()->body($data)->send();
if ( $request[0]->status == "queued" ) {
$success = true;
}
Results in an exception being thrown: Cannot use object of type Httpful\Response as array
I must say, a huge thanks to Aiias for his assistance. I managed to fix this myself (I must have spent hours looking at this). For anyone who wants to know, the HTTPful bundle has a body array, where the response is kept. Therefore, the code below works:
$url = 'https://mandrillapp.com/api/1.0/messages/send.json';
$data = array(
'key' => '{removedAPIkey}',
'message' => array (
'to' => array( array( "email" => $_to ) ),
'from_name' => Auth::user()->name,
'from_email' => Auth::user()->email,
'subject' => $_subject,
'html' => $_body
),
'async' => true
);
$request = Httpful::post($url)->sendsJson()->body($data)->send();
if ( $request->body[0]->status == "queued" ) {
$success = true;
}
Again, huge thanks to Aiias for clearing some major confusion up for me!