gpt4 book ai didi

php - Sendgrid Azure PHP

转载 作者:行者123 更新时间:2023-12-03 03:13:46 25 4
gpt4 key购买 nike

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

?>

这是我遇到的错误。 bool (真)注意: undefined variable :第 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);错误停止了,但表单仍然没有发送/接收。让我知道我做错了什么。

最佳答案

由于您在使用 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' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6a3bea7abb6aaa3f586b5a3a8a2a1b4afa2e8a5a9ab" rel="noreferrer noopener nofollow">[email protected]</a>',
'subject' => 'testing from curl',
'html' => 'testing body',
'text' => 'testing body',
'from' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6f0a170e021f030a2f1c0a010b081d060b410c0002" rel="noreferrer noopener nofollow">[email protected]</a>',
);


$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_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 Server。

关于php - Sendgrid Azure PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32723741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com