【问题标题】:Sendgrid Azure PHPSendgrid Azure PHP
【发布时间】:2015-09-22 17:59:25
【问题描述】:

我是 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);

?> 

这是我遇到的错误。 布尔(真) 注意:未定义的变量:第 36 行 D:\home\site\wwwroot\a_test_mailer_processor.php 中的 curl 警告:curl_setopt() 期望参数 1 是资源,D:\home\site\wwwroot\a_test_mailer_processor.php 中给出的 null第 36 行

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

【问题讨论】:

  • 我找到了解决方案并更新了我的答案。请检查并尝试。

标签: php azure sendgrid


【解决方案1】:

由于您在使用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找到详细信息:

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) 或 CURL_SSLVERSION_TLSv1_2 (6) 之一。

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

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

curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);

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

【讨论】:

  • 我更新了我的 PHP,如我的问题所示。我仍然没有收到电子邮件。
  • 您的账户配置成功了吗?
  • 我想是的。我已经联系了他们的支持,他们没有说我没有被配置。有没有办法让我确定?
  • 登录sendgrid.com,会有一个高亮条告诉你,如果是这样,你的帐户配置失败。
  • 我终于让它工作了。我把你的 curl_setopt($session, CURLOPT_SSLVERSION, 6);和 curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);并删除 curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);谢谢加里!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-19
  • 1970-01-01
相关资源
最近更新 更多