Create a gist now

Instantly share code, notes, and snippets.

@Burrer /Form.css
Last active Jun 8, 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 */ p {
font-size: 15px;
padding: 0 0 15px 0;
margin: 0;
} input, textarea {
font-family: Arial;
font-size: 15px;
margin: 0 0 20px 0;
} textarea {
background: #f5f5f5;
padding: 5px;
border: 1px solid #bbb;
border-radius: 15px;
width: 400px;
height: 150px;
} input {
background: #f5f5f5;
padding: 5px;
border: 1px solid #bbb;
border-radius: 15px;
} input.send {
color: #fff;
background: #222;
border: #222;
padding: 10px 25px 10px 25px;
cursor: pointer;
/* End form.html's CSS */
<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" />
<input type="text" name="email" />
<input type="text" name="subject" />
<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-->
<!--end contact form-->
// use actual sendgrid username and password in this section
$url = '';
$user = 'username'; // place SG username here
$pass = 'password'; // 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", // 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
'from' => "", // set from address here, it can really be anything
$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);
// Redirect to thank you page upon successfull completion, will want to build one if you don't alreday have one available
header('Location: thanks.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'
// print everything out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment