Sendgrid Azure PHP

Sendgrid Azure PHP

我是 php、azure 和 sendgrid 的新手。

这是我发现的一些代码,我正尝试将其用于 html 表单。

<!-- BEGINNING OF CONTACT FORM -->
<div class="section-page-landing" id="contact">
   <div class="inner-section">
    <div class="contain">
     <center><h2>Contact Me</h2>
<form class="contact" action="a_test_mailer_processor.php" method="post">
<p>Name:</p> <!-- Can choose to customize form.html inputs starting here as needed, but be sure to reference any changes in mailer.php post fields-->
<input type="text" name="name" />
 <p>E-mail:</p>
<input type="text" name="email" />
<p>Subject:</p>
<input type="text" name="subject" />
<p>Message:</p>
<textarea name="message" syle="width: 45%; text-align: center;">Please leave a short message here</textarea></p>
<input class="send" type="submit" value="Send"> <!-- Send button-->
</form></center>
    </div>
   </div>
  </div>
  <!--end contact form-->

这是我正在尝试使用的PHP

我按如下方式更新了代码,换掉了我的凭据和电子邮件地址。没有错误,但仍然不适合我。还有什么我可以测试的吗?

<?php

ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
var_dump(function_exists('curl_version'));

$url = 'https://api.sendgrid.com/';
$user = 'MYSENDGRIDUSERNAME';
$pass = 'MYSENDGRIDPASSWORD';

$params = array(
    'api_user'  => $user,
    'api_key'   => $pass,
    'to'        => 'MYEMAILADDRESS',
    'subject'   => 'testing from curl',
    'html'      => 'testing body',
    'text'      => 'testing body',
    'from'      => 'MYEMAILADDRESS',
  );


$request =  $url.'api/mail.send.json';

// Generate curl request
$session = curl_init($request);
// 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);
// Tell PHP not to use SSLv3 (instead opting for TLS)
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// obtain response
$response = curl_exec($session);
curl_close($session);

// print everything out
print_r($response);

?> 

这是我遇到的错误。 布尔(真) 注意:未定义的变量:curl in D:\home\site\wwwroot\a_test_mailer_processor.php on line 36 Warning: curl_setopt() expects parameter 1 to be resource, null given in D:\home\site\wwwroot\a_test_mailer_processor. php 第 36 行

我尝试简单地删除第 36 行,curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);并且错误停止了,但表单仍然没有得到 sent/received。让我知道我做错了什么。

由于在使用curl_setopt设置参数之前没有定义$curl,这里是SendGrid code examples page上的代码示例,请尝试一下:

<?php

$url = 'https://api.sendgrid.com/';
$user = 'USERNAME';
$pass = 'PASSWORD';

$params = array(
    'api_user'  => $user,
    'api_key'   => $pass,
    'to'        => 'example3@sendgrid.com',
    'subject'   => 'testing from curl',
    'html'      => 'testing body',
    'text'      => 'testing body',
    'from'      => 'example@sendgrid.com',
  );


$request =  $url.'api/mail.send.json';

// Generate curl request
$session = curl_init($request);
// 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);
// Tell PHP not to use SSLv3 (instead opting for TLS)
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// obtain response
$response = curl_exec($session);
curl_close($session);

// print everything out
print_r($response);

?> 

--更新--

如果你得到的错误信息是not defined constant CURL_SSLVERSION_TLSv1_2,我们可以直接将整型变量设置为CURLOPT_SSLVERSION。 我们可以在 http://php.net/manual/en/function.curl-setopt.php:

中找到详细信息

One of CURL_SSLVERSION_DEFAULT (0), CURL_SSLVERSION_TLSv1 (1), CURL_SSLVERSION_SSLv2 (2), CURL_SSLVERSION_SSLv3 (3), CURL_SSLVERSION_TLSv1_0 (4), CURL_SSLVERSION_TLSv1_1 (5) or CURL_SSLVERSION_TLSv1_2 (6).

设置为:curl_setopt($session, CURLOPT_SSLVERSION, 6);

而且我必须设置一个附加选项:

curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);

这样我就可以成功将我的请求设置到 SendGrid 服务器。