rails sendgrid setup

Step 1: Add the SendGrid Gem to Your Rails Application

# Add the following line to your Gemfile
gem 'sendgrid-ruby'

# Run the following command to install the gem
bundle install

Step 2: Set Up SendGrid API Key

Generate a SendGrid API key from the SendGrid dashboard.

Step 3: Configure SendGrid in Your Rails Application

# Add the following lines to your config/environments/production.rb file
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address: 'smtp.sendgrid.net',
  port: 587,
  authentication: :plain,
  user_name: 'apikey',
  password: 'YOUR_SENDGRID_API_KEY',
  domain: 'yourdomain.com',
  enable_starttls_auto: true
}

Replace 'YOUR_SENDGRID_API_KEY' with the actual SendGrid API key you generated.

Step 4: Create a Mailer

Generate a new mailer using the following command:

rails generate mailer YourMailerName

Update the mailer class in app/mailers/your_mailer_name.rb with your email content.

Step 5: Trigger Email Sending

Call the mailer method to trigger email sending. For example, in your controller or other relevant part of your code:

YourMailerName.your_email_method.deliver_now

Replace 'your_email_method' with the actual method you defined in your mailer.

Step 6: Test

Test your email sending functionality in your Rails application.

Step 7: Deploy

Ensure that your SendGrid configuration is applied in the production environment, and deploy your Rails application.