4

I'm trying to use ZohoMail's API to send email through my application. But it keeps giving me:

"{errorCode":"INVALID_METHOD"},"status":{"code":404,"description":"Invalid Input"}}

Here's the link to the Call that I'm trying to make: https://www.zoho.com/mail/help/api/post-send-an-email.html#Request_Body

Here's my function:

public static function sendEmail ($AccountId, $AuthCode, $FromAddress, $ToAddress, $Subject, $Content){

    $client = new Client(); //GuzzleHttp\Client
    $URI = 'http://mail.zoho.com/api/accounts/' . $AccountId . '/messages';
    $headers = ['Content-Type' => 'application/json', 'Authorization' => 'Zoho-authtoken ' . $AuthCode];
    $body = array('fromAddress' => $FromAddress, 'toAddress' => $ToAddress, 'subject' => $Subject, 'content' => $Content);
    $Nbody = json_encode($body);
    $response = $client->post($URI, $headers, $Nbody);
    echo "DONE!";

}

I've tried changing the way I'm making the call but it doesn't seem like that's the problem. I've tested the call in PostMan and it works fine so there is probably something wrong with the way I'm making the call. Any help would be much appreciated.

0

6 Answers 6

5

You need to create data and headers in the same array and pass as a second argument. Use like this.

$client = new Client();
$URI = 'http://mail.zoho.com/api/accounts/'.$AccountId.'/messages';
$params['headers'] = ['Content-Type' => 'application/json', 'Authorization' => 'Zoho-authtoken ' . $AuthCode];
$params['form_params'] = array('fromAddress' => $FromAddress, 'toAddress' => $ToAddress, 'subject' => $Subject, 'content' => $Content);
$response = $client->post($URI, $params);
echo "DONE!";

Good Luck!

Sign up to request clarification or add additional context in comments.

6 Comments

Here's what I get: Passing in the "body" request option as an array to send a POST request has been deprecated. Please use the "form_params" request option to send a application/x-www-form-urlencoded request, or the "multipart" request option to send a multipart/form-data request.
Yes. Sorry it is deprecated. Change body to form_params and test again.
Try changing it to json. There is another method for json body. so it would be $params['json'] = [content];
Test with Postman. To understand what causes the issue. If its from API or from Guzzle.
Works fine with postman, tried just using toAddress and FromAddress since only those two fields are required but the call still doesn't go through.
|
4
$client = new \GuzzleHttp\Client();
$response = $client->post(
    'url',
    [
        GuzzleHttp\RequestOptions::JSON => 
        ['key' => 'value']
    ],
    ['Content-Type' => 'application/json']
);

$responseJSON = json_decode($response->getBody(), true);

Comments

1
 $this->clients = new Client(['base_uri' => 'Url', 'timeout'  => 2.0]);
    $params['headers'] = ['Content-Type' => 'application/json'];
    $params['json'] = array(
      'parama1'=>$req->parama1,
      'parama1'=>$req->parama2,
      'parama3'=>$req->parama3,
    );
    $response = $this->clients->get('SearchBiz',$params);
    $business = $response->getBody();
    return View("myviewbiz")->with('business',json_decode($business));

Comments

0

Ty to use:

$response = $client->post($URI, $headers, ['json' => $body]);

instead of

$Nbody = json_encode($body);
$response = $client->post($URI, $headers, $Nbody);

3 Comments

Have you verified that data in $body are valid? I mean e-mail is really email and not foe example array
I've tried these parameters in postman. Here's what I'm passing: sendEmail("MyAccountID", "AuthCode", "MyEmail", "MyEmail", "I like Tacos", "Ilike Tacos");
And the json object that I'm getting is: {"fromAddress":"MyAddress","toAddress":"MyAddress","subject":"I like Tacos","content":"Ilike Tacos"}
0

After testing with cURL, I found that the URL had been 'moved' to https instead of http. Using just http, the call was going through in Postman but not with Guzzle. The only change I made was to make the URL:

https://mail.zoho.com/api/accounts/

The website lists it as just http and the request does go through with PostMan. I have made prior calls with just http in Guzzle from the same API and they went through. If someone could help me understand why this happened and why this specific call when using http works in PostMan and not in Guzzle, that'd be great.

Comments

0

This works anywhere place


use GuzzleHttp\Client;

$client   = new Client();
$options = [];
$options['form_params'] = $data;
$options['http_errors'] = false; // for get exception y api response
$options['timeout'] = 5; // milliseconds

$client->request('PUT', $uri , $options);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.