<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: effectiveone</title>
    <description>The latest articles on DEV Community by effectiveone (@effectiveone).</description>
    <link>https://dev.to/effectiveone</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F848003%2Fdfce98b1-29bc-4d97-b901-184a4fbe2d69.png</url>
      <title>DEV Community: effectiveone</title>
      <link>https://dev.to/effectiveone</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/effectiveone"/>
    <language>en</language>
    <item>
      <title>How to integrate SendGrid with nextjs?</title>
      <dc:creator>effectiveone</dc:creator>
      <pubDate>Fri, 15 Apr 2022 13:49:11 +0000</pubDate>
      <link>https://dev.to/effectiveone/how-to-integrate-sendgrid-with-nextjs-3jln</link>
      <guid>https://dev.to/effectiveone/how-to-integrate-sendgrid-with-nextjs-3jln</guid>
      <description>&lt;p&gt;I am having trouble sending e-mails from localhost via SendGrid. The whole environment, like verifying the e-mail address or domains, has been made new. I also have a new API program. What the scripture is that I know&lt;/p&gt;

&lt;p&gt;&lt;code&gt;node pages/mail.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;However, despite using tutorials found on the Internet, I have a problem with sending the first email.&lt;/p&gt;

&lt;p&gt;components / call / contactform&lt;br&gt;
`import React, { useState } from "react";&lt;/p&gt;

&lt;p&gt;export default function Contactform() {&lt;/p&gt;

&lt;p&gt;const [fullname, setFullname] = useState("");&lt;br&gt;
  const [email, setEmail] = useState("");&lt;br&gt;
  const [subject, setSubject] = useState("");&lt;br&gt;
  const [message, setMessage] = useState("");&lt;/p&gt;

&lt;p&gt;//   Form validation&lt;br&gt;
  const [errors, setErrors] = useState({});&lt;/p&gt;

&lt;p&gt;//   Setting button text&lt;br&gt;
  const [buttonText, setButtonText] = useState("Send");&lt;/p&gt;

&lt;p&gt;const [showSuccessMessage, setShowSuccessMessage] = useState(false);&lt;br&gt;
  const [showFailureMessage, setShowFailureMessage] = useState(false);&lt;/p&gt;

&lt;p&gt;const handleValidation = () =&amp;gt; {&lt;br&gt;
    let tempErrors = {};&lt;br&gt;
    let isValid = true;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (fullname.length &amp;lt;= 0) {
  tempErrors["fullname"] = true;
  isValid = false;
}
if (email.length &amp;lt;= 0) {
  tempErrors["email"] = true;
  isValid = false;
}
if (subject.length &amp;lt;= 0) {
  tempErrors["subject"] = true;
  isValid = false;
}
if (message.length &amp;lt;= 0) {
  tempErrors["message"] = true;
  isValid = false;
}

setErrors({ ...tempErrors });
console.log("errors", errors);
return isValid;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;//   const [form, setForm] = useState(false);&lt;/p&gt;

&lt;p&gt;const handleSubmit = async (e) =&amp;gt; {&lt;br&gt;
    e.preventDefault();&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let isValidForm = handleValidation();

if (isValidForm) {
  setButtonText("Sending");
  const res = await fetch("pages/api/sendgrid", {
    body: JSON.stringify({
      email: email,
      fullname: fullname,
      subject: subject,
      message: message,
    }),
    headers: {
      "Content-Type": "application/json",
    },
    method: "POST",
  });

  const { error } = await res.text();
  if (error) {
    console.log(error);
    setShowSuccessMessage(false);
    setShowFailureMessage(true);
    setButtonText("Send");

    // Reset form fields
    setFullname("");
    setEmail("");
    setMessage("");
    setSubject("");
    return;
  }
  setShowSuccessMessage(true);
  setShowFailureMessage(false);
  setButtonText("Send");
  // Reset form fields
  setFullname("");
  setEmail("");
  setMessage("");
  setSubject("");
}
console.log(fullname, email, subject, message);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;return (&lt;br&gt;
   &amp;lt;&amp;gt;&lt;br&gt;
      &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      onSubmit={handleSubmit}&amp;lt;br&amp;gt;
      className="rounded-lg shadow-xl flex flex-col px-8 py-8 bg-white dark:bg-blue-500"&amp;lt;br&amp;gt;
    &amp;amp;gt;&amp;lt;br&amp;gt;
      &amp;lt;h1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;br&gt;&lt;br&gt;
            Send a message&lt;br&gt;&lt;br&gt;
          &lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      &amp;lt;label
        htmlFor="fullname"
        className="text-gray-500 font-light mt-8 dark:text-gray-50"
      &amp;gt;
        Full name&amp;lt;span className="text-red-500 dark:text-gray-50"&amp;gt;*&amp;lt;/span&amp;gt;
      &amp;lt;/label&amp;gt;
      &amp;lt;input
        type="text"
        value={fullname}
        onChange={(e) =&amp;gt; {
          setFullname(e.target.value);
        }}
        name="fullname"
        className="bg-transparent border-b py-2 pl-4 focus:outline-none focus:rounded-md focus:ring-1 ring-green-500 font-light text-gray-500"
      /&amp;gt;
      {errors?.fullname &amp;amp;&amp;amp; (
        &amp;lt;p className="text-red-500"&amp;gt;Fullname cannot be empty.&amp;lt;/p&amp;gt;
      )}

      &amp;lt;label
        htmlFor="email"
        className="text-gray-500 font-light mt-4 dark:text-gray-50"
      &amp;gt;
        E-mail&amp;lt;span className="text-red-500"&amp;gt;*&amp;lt;/span&amp;gt;
      &amp;lt;/label&amp;gt;
      &amp;lt;input
        type="email"
        name="email"
        value={email}
        onChange={(e) =&amp;gt; {
          setEmail(e.target.value);
        }}
        className="bg-transparent border-b py-2 pl-4 focus:outline-none focus:rounded-md focus:ring-1 ring-green-500 font-light text-gray-500"
      /&amp;gt;
      {errors?.email &amp;amp;&amp;amp; (
        &amp;lt;p className="text-red-500"&amp;gt;Email cannot be empty.&amp;lt;/p&amp;gt;
      )}

      &amp;lt;label
        htmlFor="subject"
        className="text-gray-500 font-light mt-4 dark:text-gray-50"
      &amp;gt;
        Subject&amp;lt;span className="text-red-500"&amp;gt;*&amp;lt;/span&amp;gt;
      &amp;lt;/label&amp;gt;
      &amp;lt;input
        type="text"
        name="subject"
        value={subject}
        onChange={(e) =&amp;gt; {
          setSubject(e.target.value);
        }}
        className="bg-transparent border-b py-2 pl-4 focus:outline-none focus:rounded-md focus:ring-1 ring-green-500 font-light text-gray-500"
      /&amp;gt;
      {errors?.subject &amp;amp;&amp;amp; (
        &amp;lt;p className="text-red-500"&amp;gt;Subject cannot be empty.&amp;lt;/p&amp;gt;
      )}
      &amp;lt;label
        htmlFor="message"
        className="text-gray-500 font-light mt-4 dark:text-gray-50"
      &amp;gt;
        Message&amp;lt;span className="text-red-500"&amp;gt;*&amp;lt;/span&amp;gt;
      &amp;lt;/label&amp;gt;
      &amp;lt;textarea
        name="message"
        value={message}
        onChange={(e) =&amp;gt; {
          setMessage(e.target.value);
        }}
        className="bg-transparent border-b py-2 pl-4 focus:outline-none focus:rounded-md focus:ring-1 ring-green-500 font-light text-gray-500"
      &amp;gt;&amp;lt;/textarea&amp;gt;
      {errors?.message &amp;amp;&amp;amp; (
        &amp;lt;p className="text-red-500"&amp;gt;Message body cannot be empty.&amp;lt;/p&amp;gt;
      )}
      &amp;lt;div className="flex flex-row items-center justify-start"&amp;gt;
        &amp;lt;button
          type="submit"
          className="px-10 mt-8 py-2 bg-[#130F49] text-gray-50 font-light rounded-md text-lg flex flex-row items-center"
        &amp;gt;
          {buttonText}
          &amp;lt;svg
            width="24"
            height="24"
            viewBox="0 0 24 24"
            className="text-cyan-500 ml-2"
            fill="currentColor"
            xmlns="http://www.w3.org/2000/svg"
          &amp;gt;
            &amp;lt;path
              d="M9.00967 5.12761H11.0097C12.1142 5.12761 13.468 5.89682 14.0335 6.8457L16.5089 11H21.0097C21.562 11 22.0097 11.4477 22.0097 12C22.0097 12.5523 21.562 13 21.0097 13H16.4138L13.9383 17.1543C13.3729 18.1032 12.0191 18.8724 10.9145 18.8724H8.91454L12.4138 13H5.42485L3.99036 15.4529H1.99036L4.00967 12L4.00967 11.967L2.00967 8.54712H4.00967L5.44417 11H12.5089L9.00967 5.12761Z"
              fill="currentColor"
            /&amp;gt;
          &amp;lt;/svg&amp;gt;
        &amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div className="text-left"&amp;gt;
        {showSuccessMessage &amp;amp;&amp;amp; (
          &amp;lt;p className="text-green-500 font-semibold text-sm my-2"&amp;gt;
            Thankyou! Your Message has been delivered.
          &amp;lt;/p&amp;gt;
        )}
        {showFailureMessage &amp;amp;&amp;amp; (
          &amp;lt;p className="text-red-500"&amp;gt;
            Oops! Something went wrong, please try again.
          &amp;lt;/p&amp;gt;
        )}
      &amp;lt;/div&amp;gt;
    &amp;lt;/form&amp;gt;
    &amp;lt;/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;);&lt;br&gt;
}`&lt;/p&gt;

&lt;p&gt;By checking in the controller it shows that my message gets the status 202, so it gets sent somewhere.&lt;/p&gt;

&lt;p&gt;I suspect that the second part of the code is written incorrectly.&lt;/p&gt;

&lt;p&gt;pages / api / sendgrid&lt;/p&gt;

&lt;p&gt;`&lt;br&gt;
const sgMail = require('@sendgrid/mail')&lt;br&gt;
sgMail.setApiKey(process.env.SENDGRID_API_KEY)&lt;/p&gt;

&lt;p&gt;function message (req, res) {&lt;br&gt;
    var message = req.body.message;&lt;br&gt;
    var email = req.body.email;&lt;br&gt;
    var subject = req.body.subject;&lt;br&gt;
    var fullname = req.body.fullname;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;const msg = {&lt;br&gt;
  to: '&lt;a href="mailto:kon@gmail.com"&gt;kon@gmail.com&lt;/a&gt;', // Change to your recipient&lt;br&gt;
  from: 'ssss@dddd', // Change to your verified sender&lt;br&gt;
  subject: 'tssssest najwyzszego orderu',&lt;br&gt;
  text: 'bla bla bla',&lt;br&gt;
  html: 'and easy ${req.body.email} to do anywhere, ${req.body.subject} even with Node.js',&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;sgMail&lt;br&gt;
  .send(msg)&lt;br&gt;
  .then((response) =&amp;gt; {&lt;br&gt;
    console.log(response[0].statusCode)&lt;br&gt;
    console.log(response[0].headers)&lt;br&gt;
  })&lt;br&gt;
  .catch((error) =&amp;gt; {&lt;br&gt;
    console.error(error)&lt;br&gt;
  })`&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>node</category>
    </item>
  </channel>
</rss>
