Сейчас я занимаюсь разработкой веб-приложения, и оно должно отправлять электронные письма.
Сначала я использовал службу SMTP и по какой-то причине я выбрал infobip.com для работы с электронной почтой.
Он имеет хорошую документацию по API, поэтому я следовал за учебником.
Проблема в том, что, несмотря на то, что я выполнил все шаги, у меня есть какая-то ошибка типа «неверный запрос»
Существует API для отправки смс с помощью сервиса infobip.
(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];