SendGrid with dynamic PHP params

喜你入骨 提交于 2019-12-25 03:24:07

问题


Hi All I am trying to do is get a simple PHP contact form on my site to work. I am using bootstrap 3 and SendGrid. I can get it to send an email but I can not seem to get it to pass the contact form params to SendGrid to then send to me.

This is what I have. I just don't understand why something so simple is so complicated.

    <?php

$url = 'https://api.sendgrid.com/';
$user = 'XXXXXXXX';
$pass = 'XXXXXXXX';
$first_name = $_POST["firstname"];
$last_name = $_POST["lastname"];
$email = $_POST["email"];
$phone = $_POST["phone number"];
$location = $_POST["locations"];
$bio = $_POST["bio"];


$params = array(
    'api_user'  => $user,
    'api_key'   => $pass,
    'to'        => 'myemail@gmail.com',
    'subject'   => 'I Want To Be A Scout',
    'html'      => $first_name,
    'text'      => '%first_name%',
    'from'      => $email,
  );


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

// print everything out
print_r($response);

?>

By the way if I pass $email in the body of the email it comes through fine, but not any of my other params.

Help! Thanks.


回答1:


You've got these variables $first_name, $last_name, $email, $phone, $location, $bio. They presumably contain the values submitted by the user. But you're not giving them (except $first_name) to SendGrid.

You probably want to render those variables into a string and pass that as your email body, something like this:

$message = "Hey looloobs, somebody named $first_name $last_name from $location "
         . "filled out the form on the website, you can reach them at $phone "
         . "\n\n $bio";

Then pass $message to SendGrid like this:

...
'subject'   => 'I Want To Be A Scout',
'html'      => $message,
'text'      => $message,
'from'      => $email,
...


来源:https://stackoverflow.com/questions/25388281/sendgrid-with-dynamic-php-params

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!