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

如何使用PHP通过ZohoAPI发送电子邮件?,php,rest,api,email,zoho,Php,Rest,Api,Email,Zoho,我遵循了,以下是我的代码: $url = "https://mail.zoho.com/api/accounts/662704xxx/messages"; $param = [ "fromAddress"=> "myemail@mydomain.com", "toAddress"=> "somewhere@gmail.com", "ccAddress"=> "", "bccAddress"=> ""

我遵循了,以下是我的代码:

$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"}}
响应方式为:(根据)

错误请求-请求API中传递的输入无效或不正确。请求者必须更改输入参数并再次发送请求


知道如何修复它吗?

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

注意:您可以使用API检索当前授权用户的accountid

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

集成SMTP支持-不使用本地邮件服务器发送

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

使用PHPMailer发送电子邮件。@Devrajverma我的服务器上没有安装SMTP服务器。如果它们为空,可以删除
ccAddress
bccadAddress
,看看能否成功。。看起来其他一切正常,但我觉得zoho正在崩溃,因为这两个输入为null,因此无效。您可能需要添加一个
内容类型:application/json
头,告诉接收者您实际上正在请求正文中发送json。。。通过将字符串传递给
CURLOPT_POSTFIELDS
,它默认为
application/x-www-form-urlencoded
@CBroe I added
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type:application/json'),但遗憾的是结果仍然是一样的。只有一个关于PHPMailer的问题。
Username
setFrom()
函数的第一个参数是否应该始终相同?好吧,它不一定是相同的,尽管它不同会大大增加它被标记为垃圾邮件的可能性,特别是如果
from
邮件位于另一个域上。例如,我认为如果这两封邮件是不同的,谷歌会断然否认这封邮件。