我正在尝试在我的Azure应用上实现SendGrid,并且它一直在浏览器上抛出HTTP ERROR 500。我使用完全相同的代码跟随教程但输入了我的值。我似乎无法找到任何关于如何解决这个问题以及从我能说出的值是正确的。在创建Swift_SmtpTransport::newInstance()
方法时会发生错误。
以下是代码段:
$text = "Hi!\nHow are you?\n";
$html = "<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
</p>
</body>
</html>";
// This is your From email address
$from = array('someone@example.com' => 'Test User');
// Email recipients
$to = array(
'toemail@email.com' => 'Elite Email Address'
);
// Email subject
$subject = 'Example PHP Email';
// Login credentials
$username = 'myusernamefromazure';
$password = 'mypassword';
// Setup Swift mailer parameters
$transport = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 587); <-- this is where the error is thrown...
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
sendgrid网站上的教程看起来有点不同:
<?php
// using SendGrid's PHP Library
// https://github.com/sendgrid/sendgrid-php
// If you are using Composer (recommended)
require 'vendor/autoload.php';
// If you are not using Composer
// require("path/to/sendgrid-php/sendgrid-php.php");
$from = new SendGrid\Email("Example User", "test@example.com");
$subject = "Sending with SendGrid is Fun";
$to = new SendGrid\Email("Example User", "test@example.com");
$content = new SendGrid\Content("text/plain", "and easy to do anywhere, even with PHP");
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
print_r($response->headers());
echo $response->body();
来源: https://sendgrid.com/docs/Integrate/Code_Examples/v3_Mail/php.html