【问题标题】:How can I send email through ZOHO api using PHP?如何使用 PHP 通过 ZOHO api 发送电子邮件?
【发布时间】:2018-07-29 15:39:49
【问题描述】:

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

知道如何解决吗?

【问题讨论】:

  • 使用 PHPMailer 发送邮件。
  • @Devrajverma 我的服务器上没有安装 SMTP 服务器。
  • 也许删除 ccAddressbccAddress 如果它们为空,看看你是否能获得成功。看起来其他一切都很好,但我觉得 zoho 正在打破,因为这两个输入为空因此,无效
  • 您可能需要添加一个Content-Type: application/json 标头,告诉接收器您实际上是在请求正文中发送JSON ...通过将字符串传递给CURLOPT_POSTFIELDS,默认为application/x-www-form-urlencoded
  • @CBroe 我添加了curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));,但遗憾的是结果还是一样。

标签: php rest api email zoho


【解决方案1】:

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

注意:您可以使用 API here 来检索当前经过身份验证的用户的 accountid。

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

集成 SMTP 支持 - 无需本地邮件服务器即可发送

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

【讨论】:

  • 只有一个关于 PHPMailer 的问题。 UsernamesetFrom() 函数的第一个参数是否应该始终相同?
  • AFAIK,它必须是相同的,虽然不同会大大增加它被标记为垃圾邮件的机会,特别是如果from邮件在另一个域中。例如,如果两者不同,我认为 Google 会直接拒绝发送邮件。
【解决方案2】:

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

【讨论】:

  • 这解决了我的问题,我最终检查了很多次配置和设置。从 Godaddy 迁移时,需要 isSMTP() 标志,但这会导致 Zoho 失败。
猜你喜欢
  • 2013-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-18
  • 2017-01-29
  • 2014-08-27
  • 2010-11-22
相关资源
最近更新 更多