curl email error mailer.php

时间:2015-10-22 02:21:32

标签: php html curl

我的网站上有一个mailer.php,它与sendgrid连接,向我们发送查询表格中的电子邮件。但是,在我们的设置中,当有人在问题中输入电子邮件时,他们会在点击发送时收到错误页面。具有讽刺意味的是,电子邮件仍然会被发送,但它并没有将它们发送到我们服务器上的成功页面。

以下是您收到的错误消息。

警告:curl_setopt()期望参数1为资源,在第35行的/home/dekastud/public_html/mailer.php中给出null

警告:无法修改标题信息 - 已在第53行的/home/dekastud/public_html/mailer.php中发送的输出(/home/dekastud/public_html/mailer.php:1上的输出)



    <?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);?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

删除PHP文件顶部的空格。 <?php之前的那些空格实际上将输出发送到浏览器。将某些内容输出到浏览器后,您就无法发送标题。

header("Location: ...");

正在发送标题......

此外,在拨打curl_setopt()之前,您有一个curl_init()来电,这是无效的。 curl_setopt()需要有效的curl资源。请参阅here

curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);应低于curl_init()的调用,$curl变量应替换为有效的curl资源,或者在您的情况下$session