Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem using Sendgrid with the Contact Form plugin #251

Closed
jeromecoupe opened this issue Oct 2, 2023 · 4 comments
Closed

Problem using Sendgrid with the Contact Form plugin #251

jeromecoupe opened this issue Oct 2, 2023 · 4 comments
Labels

Comments

@jeromecoupe
Copy link

jeromecoupe commented Oct 2, 2023

Description

Trying to use the contact form first party plugin with my sendgrid account:

  • Craft is able to send test emails (locally and in production) with the Sendgrid config / credentials (SMTP) saved in my ENV file by going to settings > email > test. That part seems to work flawlessly in local and in production.
  • Using the same config / account (in local or in production), the contact form sometimes generates a Sendgrid error in the logs and no emails are sent. It is quite erratic: sometimes it works (10%), sometimes it does not (90%).

Looking at the logs, I get the following kind of errors:

2023-10-02 20:19:13 [web.ERROR] [yii\symfonymailer\Mailer::sendMessage] Expected response code "250" but got code "550", with message "550 The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ to see the Sender Identity requirements". {"memory":2314648}

Craft's system email address is verified by SendGrid on this install, otherwise I wouldn't get test emails sent using that very address (I assume the contact for is using the System Email Address). It seems to me there there are no problem on the Sendgrid side / with sender identity here.

  • Is there any difference in the way Craft test emails and the way the contact form plugin work ?
  • Since tests emails are working file, I assume my Sendgrid config is fine ... but the errors I see in the logs are related to Sendgrid.
  • Can someone please advise as to how I could further debug this ? I am at a loss.

Craft CMS 4.5.6.1
Contact Form 3.0.1
Contact Form Honeypot: 2.0.0
PHP: 8.1.21

code for the form in case that helps

{# import macros for form errors #}
{% import '_macros/form_macros' as forms %}

{% set submission = submission ?? null %}
<form class="c-form" method="post" action="" accept-charset="UTF-8">

  {# prevent against cross domain attacks #}
  {{ csrfInput() }}
  {{ actionInput('contact-form/send') }}

  {% if craft.app.session.hasFlash('notice') %}
    <div class="c-form__message">
      {{ contactForm.contactFormSuccess }}
    </div>
  {% elseif craft.app.session.hasFlash('error') %}
    <div class="c-form__message">
      <p>{{ craft.app.session.getFlash('error') }}</p>
    </div>
  {% endif %}

    <div class="l-grid  l-grid--gap  l-grid--2cols@large">
      <div>

        <div class="c-form__field">
          <label class="c-form__blocklabel" for="fromName">{{ "Nom"|t }} <span class="c-form__mandatory">*</span></label>
          <input class="c-form__input" type="text" id="fromName" name="fromName" value="{{ submission.fromName ?? '' }}" required>
          {{submission ? forms.listErrors(submission.getErrors('fromName')) }}
        </div>

      </div>
      <div>

        <div class="c-form__field">
          <label class="c-form__blocklabel" for="societe">{{ "Société"|t }}</label>
          <input class="c-form__input" type="text" id="societe" name="message[societe]" value="{{ submission.message.societe ?? '' }}">
        </div>

      </div>
    </div>

    <div class="c-form__field">
      <label class="c-form__blocklabel" for="fromEmail">{{ "Email"|t }} <span class="c-form__mandatory">*</span></label>
      <input class="c-form__input" type="email" id="fromEmail" name="fromEmail" value="{{ submission.fromEmail ?? '' }}" required>
      {{ submission ? forms.listErrors(submission.getErrors('fromEmail')) }}
    </div>

    <div class="c-form__field">
      <label class="c-form__blocklabel" for="message">{{ "Message"|t }} <span class="c-form__mandatory">*</span></label>
      <textarea class="c-form__input" id="message" name="message[body]" rows="5" required>{{ submission.message.body ?? '' }}</textarea>
      {{ submission ? forms.listErrors(submission.getErrors('message')) }}
    </div>

    <div class="c-form__field">
      {% set newsletterStatus = submission.message.newsletter ?? null %}
      {% set newsletterCheckboxAttr = (newsletterStatus == "on") ? "checked" : "" %}
      <input class="c-form__checkbox" type="checkbox" id="newsletter" name="message[newsletter]" {{ newsletterCheckboxAttr }}>
      <label class="c-form__inlinelabel" for="newsletter">{{ "Je souhaite recevoir la newsletter"|t }}</label>
    </div>

  {# honeypot field #}
  <input class="u-display-none" type="text" id="****" name="****" aria-hidden="true">

  <p><input type="submit" value="{{ "Envoyer"|t }}" class="c-button  c-button--primary"></p>

</form>
@jeromecoupe jeromecoupe added the bug label Oct 2, 2023
@jeromecoupe
Copy link
Author

Not sure what this is caused by. Kind of intermittent behaviour. Closing until I hear more from the server guys.

@jeromecoupe
Copy link
Author

jeromecoupe commented Oct 3, 2023

I have the same errors on the local DDEV install now, using the same credentials and System Email Address. The only thing that changes is the toEmail in my config/contact-form.php file. Truly at a loss here.

The problem seems to occur only with contact form messages / emails. When I test email settings either in local or in production, I get the emails from Craft

@jeromecoupe
Copy link
Author

jeromecoupe commented Oct 3, 2023

I think I narrowed it down. Was using Contact Form Honeypot plugin on the contact form.

  • checked config: config setting and field name / id were identical
  • disabled honeypot plugin
  • commented out the hidden field

The contact form emails started to go through again. Not seeing any more errors in the logs saying that Sendgrid cannot match the Verified Sender Identity.

Posted an issue on the plugin repository

@jeromecoupe jeromecoupe changed the title Problem using Sendgrid and the Contact Form plugin Problem using Sendgrid with the Contact Form plugin Oct 3, 2023
@jeromecoupe
Copy link
Author

jeromecoupe commented Oct 6, 2023

I am going to look like an idiot here but maybe it can help someone else. The solution was quite simple and had nothing to do with the contact form plugin or the honeypot one.

When I switched from using a simple SMTP config with Sendgrid to using the Sendgrid adapter, those errors disappeared and my emails started to get delivered.

TLDR;

  • Use adapters, not an SMTP config with transactional email services. They seem to play nicer with the contact form and allowed Sendgrid to verify sender identity.
  • With Sendgrid at least, it seems to be worth it to validate your domain(s) to improve email delivery.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant