Create a gist now

Instantly share code, notes, and snippets.

@Burrer /Form.css
Last active Dec 5, 2016

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;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment