Skip to content

Instantly share code, notes, and snippets.

@Burrer
Last active May 25, 2023 05:28
  • Star 10 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Burrer/1581edff337eb89ee1e1 to your computer and use it in GitHub Desktop.
Contact Form: Using SendGrid WebAPI and cURL PHP
/* Form.html CSS, just copy and paste any where in your style.css file, or customize to fit with your domain */
form.contact p {
font-size: 15px;
padding: 0 0 15px 0;
margin: 0;
}
form.contact input, form.contact textarea {
font-family: Arial;
font-size: 15px;
margin: 0 0 20px 0;
}
form.contact textarea {
background: #f5f5f5;
padding: 5px;
border: 1px solid #bbb;
border-radius: 15px;
width: 400px;
height: 150px;
}
form.contact input {
background: #f5f5f5;
padding: 5px;
border: 1px solid #bbb;
border-radius: 15px;
}
form.contact input.send {
color: #fff;
background: #222;
border: #222;
padding: 10px 25px 10px 25px;
cursor: pointer;
}
/* End form.html's CSS */
<!-- 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="mailer.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
$curl = curl_init();
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sendgrid.com/v3/mail/send",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\n \"personalizations\": [\n {\n \"to\": [\n {\n \"email\": \"[Email Address to send Contact to]\"\n }\n ],\n \"subject\": \"New Contact\"\n }\n ],\n \"from\": {\n \"email\": \"[FROM EMAIL]\"\n },\n \"content\": [\n {\n \"type\": \"text/html\",\n \"value\": \"$name<br>$email<br>$subject<br>$message\"\n }\n ]\n}",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer [SG API key]",
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
header('Location: thanks.html');
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
Copy link

ghost commented Aug 10, 2019

{\n \"type\": \"text/html\",\n \"value\": \"$name<br>$email<br>$subject<br>$message\"\n }

This does not work. My email arrives blank. I verified that my email client allowed HTML and exported the email to an external file... There is no message contents. When I change the value type to text/plain I receive the <br> tags only. Is this an issue with code above or with sendgrid? Thank you in advance for your help.

Copy link

ghost commented Aug 10, 2019

Further investigation shows that the <br> tags are working as I have three blank lines in the email. The PHP variables are not coming through. I have tried adding things like <?= $message; ?> and other variants but that doesn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment