6

I've followed this doc and here is my code:

$url = "https://mail.zoho.com/api/accounts/662704xxx/messages";
$param = [  "fromAddress"=> "myemail@mydomain.com",
            "toAddress"=> "somewhere@gmail.com",
            "ccAddress"=> "",
            "bccAddress"=> "",
            "subject"=> "Email - Always and Forever",
            "content"=> "Email can never be dead ..."];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($param));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
die;

And the response is:

{"data":{"errorCode":"INVALID_TICKET","moreInfo":"Invalid ticket"},"status":{"code":400,"description":"Invalid Input"}}

And the response means: (according to this)

BAD REQUEST - The input passed in the Request API is invalid or incorrect. The requestor has to change the input parameters and send the Request again.

Any idea how can I fix it?

Martin AJ
  • 6,261
  • 8
  • 53
  • 111
  • Use PHPMailer for email sending. – Devraj verma Feb 19 '18 at 11:50
  • @Devrajverma I don't have SMTP server installed on my server. – Martin AJ Feb 19 '18 at 11:51
  • Maybe remove `ccAddress` and `bccAddress` if they are null and see if you can get a success.. Looks like everything else is fine but I feel like zoho is breaking because those 2 inputs are null and therefore, invalid – IsThisJavascript Feb 19 '18 at 11:55
  • You likely need to add a `Content-Type: application/json` header telling the receiver that you actually are sending JSON in the request body ... by passing a string to `CURLOPT_POSTFIELDS`, it defaults to `application/x-www-form-urlencoded` – CBroe Feb 19 '18 at 12:27
  • @CBroe I added `curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));`, but sadly the result is still the same. – Martin AJ Feb 19 '18 at 12:30
  • @MartinAJ, did you authenticate before trying to send the email? Also, you don't need a SMTP server on your server to use PHPMailer. I send all my email through Zoho using PHPMailer in a server without any mailing servers. – ishegg Feb 19 '18 at 12:35
  • @ishegg well no, I didn't do anything about authentication. Can you please paste your code (sending email through Zoho using PHPMailer ) in here (or a gist on github) for me? – Martin AJ Feb 19 '18 at 12:39
  • @MartinAJ try [this](https://gist.github.com/ishegg/fb67dc4d78ca9805b1381742dd93f577). – ishegg Feb 19 '18 at 12:44
  • @ishegg Thank you buddy .. but sadly it throws `Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: data not accepted. in C:\xampp\htdocs\myweb\vendor\phpmailer\phpmailer\src\PHPMailer.php:1757` – Martin AJ Feb 19 '18 at 13:14
  • Set `$phpMailer->SMTPDebug = true;` to see further information. – ishegg Feb 19 '18 at 13:16
  • @ishegg Oh great .. I did it .. thank you so much. If you add your code as an answer, I will accept it as the accepted answer. – Martin AJ Feb 19 '18 at 13:16
  • @MartinAJ I submitted an answer with more details. – ishegg Feb 19 '18 at 13:21

3 Answers3

6

In order to send mail with Zoho through its API, you need to first authenticate, as seen on the APIDocs:

Note: You can use the API here to retrieve the accountid for the currently authenitcated user.

That said, and refering to your comment, you don't need an SMTP server installed on your server to be able to send mail with PHPMailer:

Integrated SMTP support - send without a local mail server

Source

Zoho requires you to use TLS and the 587 port, so you can set up your connection like this:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

$phpMailer = new PHPMailer(true);
$phpMailer->SMTPDebug = SMTP::DEBUG_SERVER;
$phpMailer->isSMTP();
$phpMailer->Host = "smtp.zoho.com";
$phpMailer->SMTPAuth = true;
$phpMailer->Username = "your-user";
$phpMailer->Password = "your-password";
$phpMailer->SMTPSecure = "tls"; // or PHPMailer::ENCRYPTION_STARTTLS
$phpMailer->Port = 587;
$phpMailer->isHTML(true);
$phpMailer->CharSet = "UTF-8";
$phpMailer->setFrom("mail-user", "mail-name");

$phpMailer->addAddress("mail-to");
$phpMailer->Subject = "subject";
$phpMailer->Body = "mail-body";
$phpMailer->send();
Niket Pathak
  • 6,323
  • 1
  • 39
  • 51
ishegg
  • 9,685
  • 3
  • 16
  • 31
  • Just one question about PHPMailer. Should always `Username` and the first argument of `setFrom()` function be the same? – Martin AJ Feb 19 '18 at 13:48
  • AFAIK, it doesn't *have* to be the same, though having it be different will greatly increase the chances of it being flagged as spam, specially if the `from` mail is on another domain. For example, I think Google outright denies the mail being sent if those two are different. – ishegg Feb 19 '18 at 13:50
1

In my case everything I found on Google didn't make it work. It took me 3 days. And in the end it's simply like this and it magically works. Hope can help someone like me. What you need to do is remove the line below:

enter image description here

Doan Bui
  • 3,572
  • 25
  • 36
  • This fixed my issue, I ended up going over the configuration and settings a bunch of times. Migrating from Godaddy, the isSMTP() flag is required, but this will cause Zoho to fail. – ant-C Oct 07 '21 at 23:33
0

After hours of debug this solved my problem on my codeigniter php project using phpmailer, ; //$config['protocol'] = 'smtp'; just delete this line. I should mention that zoho also blocked free acounts to use smtp.

  • Your answer could be improved with additional information. for example there is no ``$config['protocol'] = 'smtp'`` in op. – AmirRezaM75 Sep 12 '22 at 16:01