Sending email over SMTP with sendgrid

25
February 12, 2019, at 8:50 PM

I have a html fieldset and a short php file for sending an email via sendgrid when the submit button is clicked.

HTML

            <form id="contact-form" action="scripts/mailer.php" method="post">
                <fieldset form="#contact-form">
                    <legend>Contact Form</legend>
                    <label class="input-field-name">Name:<br />
                        <input class="input-field" type="text" name="name" required/>
                    </label><br />
                    <label class="input-field-name">Email:<br />
                        <input class="input-field" type="text" name="email" required/>
                    </label><br />
                    <label class="input-field-name">Message Title:<br />
                        <input class="input-field" type="text" name="header" required/>
                    </label><br />
                    <label class="input-field-name">Message:<br />
                        <textarea class="message-field" type="text" name="message" required></textarea>                            
                    </label><br />
                    <button id="submit-button" type="submit">Submit</button>
                </fieldset>
            </form>

PHP

<?php
require_once('sendgrid-php/sendgrid-php.php');
$name = $_POST["name"];
$email = $_POST["email"];
$header = $_POST["header"];
$message = $_POST["message"];
$from = new SendGrid\Email(null, $email);
$subject = $header;
$to = new SendGrid\Email(null, "MY EMAIL ADDRESS");
$content = new SendGrid\Content("text/plain", $message);
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$apiKey = 'MY API KEY';
$sg = new \SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
if($response->statusCode() == 202){
    echo "Email sent successfully";
}else{
    echo "Email could not be sent";
}
echo $response->statusCode();
var_dump($response->headers());
echo $response->body(); 

I am hosting my site on GoDaddy and from everything I've been reading I believe that this should work but instead I get http error 500, my site cannot handle this request.

Any ideas of how to fix this? Thanks

READ ALSO
bulk insert working in ssms but not working while executing from php

bulk insert working in ssms but not working while executing from php

I'm trying to insert the data from a text file into my databaseI'm using SQL Server Management Studio 17

37
Laravel/LaraCSV - Global Variable in beforeEach

Laravel/LaraCSV - Global Variable in beforeEach

I have a function in my Controller where I'm trying to export acsv file using LaraCSV

39
Cannot get difference between two timestamps

Cannot get difference between two timestamps

I want return the number of dates which have a difference less than 30 minutesThese are the records stored in the database:

41
Parse SOAP web service XML response

Parse SOAP web service XML response

I´m consuming a WS and it gives me data as XMLWhen the response returns to my PHP it came as Object and not as XML

28