使用sendgrid通过SMTP发送电子邮件

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

One of the servers that I administer is with GoDaddy, and when sending emails using SendGrid's SMTP service, it is key to set the following additional fields:

$username = 'apikey';
$host = 'ssl://smtp.sendgrid.net';
$port = 587;

When setting the username, use the string 'apikey' as shown above, and NOT your actual apikey. GoDaddy seems to work well when using port 587, which when setting the host to the above, will send your emails securely.

Your emails should work after this. Good luck.