drtkyykai004574380 2018-02-19 11:48
浏览 205
已采纳

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

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?

  • 写回答

1条回答 默认 最新

  • douluyezhen4512 2018-02-19 13:21
    关注

    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;
    
    $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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波