Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send email through ZOHO api using PHP?

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

$url = "https://mail.zoho.com/api/accounts/662704xxx/messages";
$param = [  "fromAddress"=> "[email protected]",
            "toAddress"=> "[email protected]",
            "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?

like image 301
Martin AJ Avatar asked Feb 19 '18 11:02

Martin AJ


People also ask

Can we send email through PHP script?

PHP (Hypertext Preprocessor) is an easier programming language used for faster development. The PHP mail() function allows sending emails directly from a script.

Does Zoho use PHP?

Client app must have PHP(version 7 and above) or above with curl extension enabled.

How do I send an email in Zoho Mail?

Email Composer/ Editor. Click the New Mail button in the left pane, to open the Zoho Mail composer, with Rich Text Formatting options. The composer opens in a tab, where you can draft your email. You can choose the inline editor or the new window option to reply to/forward emails from the Settings.


Video Answer


1 Answers

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();
like image 155
ishegg Avatar answered Oct 05 '22 22:10

ishegg