我现在正在开发一个Web应用程序,它需要发送电子邮件。 我首先使用了smtp服务,由于某种原因,我选择了infobip.com作为电子邮件服务。 它有关于api的很好的文档,所以我遵循了该教程。 问题是,即使我认为我已按照所有步骤操作,但遇到诸如“错误请求”之类的错误
有用于使用infobip服务发送短信的api。 (https://github.com/pnlinh) 但是没有用于发送电子邮件的API。
private $from = "some person";
private $subject = "test subject";
private function setPostData($to, $text)
{
return [
'from' => $this->from,
'to' => (array) $to,
'subject' => $this->subject,
'text' => $text,
];
}
public function testFunction()
{
$header = ['Content-Type:application/json', 'Accept:application/json'];
$postUrl = 'https://api.infobip.com/email/1/send';
$to = 'scar20181228@gmail.com';
$text = 'test email';
$postDataJson = json_encode($this->setPostData($to, $this->subject, $text));
// Set retry time if response is null or empty
$retrytime = 0;
retry_from_here:
$ch = curl_init();
// Setting options
curl_setopt($ch, CURLOPT_URL, $postUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'username'.':'.'password');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postDataJson);
// Response of the POST request
$response = curl_exec($ch);
if ($response === '' && $retrytime <= static::RETRY_TIME) {
$retrytime++;
goto retry_from_here;
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$responseBody = json_decode($response);
curl_close($ch);
return [$httpCode, $responseBody];