我有一个mailer.php在我们的网站,连接到sendgrid发送电子邮件从我们的询问表。然而,在我们的设置中,当有人键入一个问题电子邮件时,他们会收到一个错误的页面,当他们点击发送。具有讽刺意味的是,电子邮件仍然会被发送,但它不会将它们发送到我们服务器上的成功页面。
下面是您收到的错误消息。
警告: curl_setopt()希望参数1是资源,在第35行的/home/dekastud/public_html/mailer.php中为null
警告:无法修改标题信息--第53行中的/home/dekastud/public_html/mailer.php :1中已由/home/dekastud/public_html/mailer.php发送的标题。
<?php
// use actual sendgrid username and password in this section
$url = 'https://api.sendgrid.com/';
$user = '***'; // place SG username here
$pass = '***'; // place SG password here
// grabs HTML form's post data; if you customize the form.html parameters then you will need to reference their new new names here
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// note the above parameters now referenced in the 'subject', 'html', and 'text' sections
// make the to email be your own address or where ever you would like the contact form info sent
$params = array(
'api_user' => "$user",
'api_key' => "$pass",
'to' => "***", // set TO address to have the contact form's email content sent to
'subject' => "Contact Form Submission From ***", // Either give a subject for each submission, or set to $subject
'html' => "<html><head><title> Contact Form</title><body>
Name: $name\n<br>
Email: $email\n<br>
Subject: $subject\n<br>
Message: $message <body></title></head></html>", // Set HTML here. Will still need to make sure to reference post data names
'text' => "
Name: $name\n
Email: $email\n
Subject: $subject\n
$message",
'from' => "***", // set from address here, it can really be anything
);
curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
$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);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
// Redirect to thank you page upon successfull completion, will want to build one if you don't alreday have one available
header('Location: /thankyou.html'); // feel free to use whatever title you wish for thank you landing page, but will need to reference that file name in place of the present 'thanks.html'
exit();
// print everything out
print_r($response);?>
发布于 2015-10-22 02:56:38
删除PHP文件顶部的空格。<?php之前的几个空格实际上将输出发送到浏览器。一旦将某些内容输出到浏览器,就不能发送标头。
header("Location: ...");
是在发送消息..。
另外,在调用curl_setopt()之前有一个curl_init()调用,这是无效的。curl_setopt()需要有效的curl资源。见这里。
curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);应该位于对curl_init()的调用之下,$curl变量应该被替换为有效的curl资源,或者在您的例子中是$session。
https://stackoverflow.com/questions/33272386
复制相似问题