如何使用 PHP 通过 ZOHO api 发送电子邮件?

How can I send email through ZOHO api using PHP?

我已关注 this doc,这是我的代码:

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

响应是:

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

响应的意思是:(根据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.

知道如何解决吗?

为了通过 Zoho API 发送邮件,您需要先进行身份验证,如 the APIDocs 所示:

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

也就是说,参考您的评论,您不需要在服务器上安装 SMTP 服务器就可以使用 PHPMailer 发送邮件:

Integrated SMTP support - send without a local mail server

Source

Zoho 要求您使用 TLS 和 587 端口,因此您可以像这样设置您的连接:

<?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();

就我而言,我在 Google 上找到的所有内容都无法正常工作。我花了 3 天时间。最后就是这样,而且神奇地起作用了。希望可以帮助像我这样的人。您需要做的是删除下面的行: