Curl电子邮件错误邮件.php


curl email error mailer.php

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

下面是你收到的错误信息。

警告:curl_setopt()期望参数1为resource, null,在/home/dekastd/public_html/mail .php第35行

警告:无法修改报头信息-报头已经由(输出开始于/home/dekastud/public_html/mailer.php:1)在/home/dekastud/public_html/mailer.php第53行发送

    <?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);?>

删除PHP文件顶部的空格。在<?php实际将输出发送到浏览器之前的那几个空格。一旦有东西输出到浏览器,你就不能再发送报头了。

header("Location: ...");

正在发送一个报头…

在呼叫curl_init()之前有curl_setopt()呼叫,这是无效的。curl_setopt()需要一个有效的curl资源。看到这里。

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