How to deploy sendgrid mail in html webpage

To deploy SendGrid mail in an HTML webpage, you'll need to follow these steps:

Step 1: Create a SendGrid account and set up a new API key

  1. Go to SendGrid's website and sign up for an account if you haven't already.
  2. Log in to your SendGrid account and navigate to the "Settings" tab.
  3. Click on "API Keys" and then click on "Create API Key".
  4. Choose the "Mail" API key and give it a name (e.g., "My HTML Webpage").
  5. Click "Create" to generate the API key.

Step 2: Set up your HTML webpage

  1. Create a new HTML file (e.g., index.html) and add the following code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>SendGrid Mail Example</title>
    </head>
    <body>
    <form action="sendmail.php" method="post">
     <label for="name">Name:</label>
     <input type="text" id="name" name="name"><br><br>
     <label for="email">Email:</label>
     <input type="email" id="email" name="email"><br><br>
     <input type="submit" value="Send Email">
    </form>
    </body>
    </html>

    This code creates a simple form with fields for name and email.

Step 3: Create a PHP script to send the email

  1. Create a new PHP file (e.g., sendmail.php) and add the following code:
    
    <?php
    require 'vendor/autoload.php';

use SendGrid\Mail\Mail;

// Set up your SendGrid API key $apiKey = 'YOUR_SENDGRID_API_KEY';

// Set up the email recipient $toEmail = '[email protected]';

// Set up the email subject and body $subject = 'Test Email from HTML Webpage'; $body = 'This is a test email sent from an HTML webpage using SendGrid.';

// Create a new SendGrid Mail object $mail = new Mail();

// Set the from email address $mail->setFrom('[email protected]', 'Your Name');

// Set the to email address $mail->addTo($toEmail);

// Set the subject and body $mail->setSubject($subject); $mail->setText($body);

// Set the API key $mail->setApiKey($apiKey);

// Send the email try { $response = $mail->send(); echo 'Email sent successfully!'; } catch (Exception $e) { echo 'Error sending email: '. $e->getMessage(); } ?>

This code uses the SendGrid PHP library to send an email to the recipient specified in the `toEmail` variable.

**Step 4: Link the HTML form to the PHP script**

1. In your HTML file (`index.html`), update the `action` attribute of the form to point to the PHP script (`sendmail.php`):
```html
<form action="sendmail.php" method="post">
 ...
</form>

Step 5: Deploy your HTML webpage

  1. Upload your HTML file (index.html) and PHP script (sendmail.php) to your web server or hosting platform.
  2. Make sure the PHP script is executable and the SendGrid API key is secure.

That's it! When a user submits the form, the PHP script will send an email using SendGrid.