如何在 azure Web App 上安装 phpMailer


How to install phpMailer on azure Web App

我一直在研究Azure邮件服务中的许多解决方案,我认为最好的解决方案是使用phpMailer。我已经尝试使用sendgrid API,但我想将Ajax post方法与jquery/javascript一起使用。

我还找到了另一种涉及 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是一个邮件服务器,它需要PHP运行时启用SMTP服务器,并且需要启用25端口。遗憾的是,我们无权在 Azure Web 应用上执行此操作。

我建议使用第 3 部分邮件服务器从 Azure Web 应用上的应用程序发送电子邮件。在 Azure 上,可以轻松使用 SendGrid 来满足这一点,有关详细信息,请参阅 https://azure.microsoft.com/en-us/documentation/articles/store-sendgrid-php-how-to-send-email/。