【发布时间】:2016-07-15 17:14:12
【问题描述】:
我一直在研究 azure 邮件服务中的许多解决方案,我认为对我来说最好的解决方案是使用 phpMailer。我已经尝试过使用 sendgrid API,但我想使用带有 jquery/javascript 的 Ajax post 方法。
我还找到了另一个涉及 CURL 的解决方案。但是我在
上遇到错误Dotenv::load(__DIR__);
错误似乎来自 sendGrids 自己的 php 文件。
如何在 azure 上解决这些问题。
我使用的代码如下:
<?php
require 'vendor/autoload.php';
Dotenv::load(__DIR__);
$sendgrid_apikey = getenv('YOUR_SENDGRID_APIKEY');
$sendgrid = new SendGrid($sendgrid_apikey);
$url = 'https://api.sendgrid.com/';
$pass = $sendgrid_apikey;
$template_id = '<your_template_id>';
$js = array(
'sub' => array(':name' => array('Elmer')),
'filters' => array('templates' => array('settings' => array('enable' => 1, 'template_id' => $template_id)))
);
$params = array(
'to' => "test@example.com",
'toname' => "Example User",
'from' => "you@youremail.com",
'fromname' => "Your Name",
'subject' => "PHP Test",
'text' => "I'm text!",
'html' => "<strong>I'm HTML!</strong>",
'x-smtpapi' => json_encode($js),
);
$request = $url.'api/mail.send.json';
// Generate curl request
$session = curl_init($request);
// Tell PHP not to use SSLv3 (instead opting for TLS)
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $sendgrid_apikey));
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
// print everything out
print_r($response);
?>
祝你好运,斯坦纳
【问题讨论】:
-
你是说你想用PHPMailer,但你没用?
-
是的,我希望使用它。我只是对如何在 azure web 应用程序上安装库感到困惑,所以如果你知道我为什么会收到错误,我发布了另一个解决方案。我已经检查了 phpinfo No phpmailer 库,除非我正在寻找错误的变量。
标签: php azure curl phpmailer sendgrid