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

时间:2018-02-19 11:48:36

标签: php rest api email zoho

我已关注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 - Request API中传递的输入无效或不正确。请求者必须更改输入参数并再次发送请求。

我知道如何解决它?

1 个答案:

答案 0 :(得分:2)

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

  

注意:您可以使用API​​ here来检索当前已验证用户的accountid。

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

  

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

Source

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

<?php
use PHPMailer\PHPMailer\PHPMailer;

$phpMailer = new PHPMailer(true);
$phpMailer->isSMTP();
$phpMailer->Host = "smtp.zoho.com";
$phpMailer->SMTPAuth = true;
$phpMailer->Username = "your-user";
$phpMailer->Password = "your-password";
$phpMailer->SMTPSecure = "tls";
$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();