### Get Configuration Instance Example Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/LetterOpener.md Example of retrieving and using the configuration object. ```ruby require "letter_opener" current_location = LetterOpener.configuration.location puts "Messages stored at: #{current_location}" ``` -------------------------------- ### Dockerfile Configuration Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md A Dockerfile example for setting up a Ruby environment with Letter Opener. ```dockerfile FROM ruby:3.0 WORKDIR /app COPY Gemfile Gemfile.lock ./ RUN bundle install COPY . . ENV LAUNCHY_DRY_RUN=true ENV BROWSER=/dev/null EXPOSE 3000 CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"] ``` -------------------------------- ### Runtime Configuration Example Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/configuration.md Demonstrates how to get and modify Letter Opener's configuration at runtime. ```ruby # Get current configuration current_location = LetterOpener.configuration.location # Modify configuration LetterOpener.configuration.location = '/new/path' LetterOpener.configuration.message_template = :light # Or use configure block LetterOpener.configure do |config| config.message_template = :default end ``` -------------------------------- ### Docker Compose Configuration Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md A docker-compose.yml example for setting up services, volumes, and environment variables for a Dockerized application using Letter Opener. ```yaml version: '3' services: web: build: . ports: - "3000:3000" volumes: - ./tmp/letter_opener:/app/tmp/letter_opener environment: LAUNCHY_DRY_RUN: "true" BROWSER: "/dev/null" ``` -------------------------------- ### Manual Setup for ActionMailer Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md Manually sets up Letter Opener as the delivery method for Action Mailer. ```ruby require 'letter_opener' require 'action_mailer' # Configure ActionMailer ActionMailer::Base.smtp_settings = { address: 'smtp.example.com' } # Add letter_opener delivery method ActionMailer::Base.add_delivery_method( :letter_opener, LetterOpener::DeliveryMethod, location: File.expand_path('../tmp/letter_opener', __FILE__) ) # Set as default delivery method ActionMailer::Base.delivery_method = :letter_opener # Define mailer class WelcomeMailer < ActionMailer::Base def welcome(user_email) mail( to: user_email, from: 'noreply@example.com', subject: 'Welcome!', body: 'Welcome to our service!' ) end end # Send email WelcomeMailer.welcome('user@example.com').deliver_now # Opens in browser ``` -------------------------------- ### RSpec with Letter Opener Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md Example of setting up RSpec to work with Letter Opener. ```ruby ``` -------------------------------- ### file_uri_scheme Examples Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/types.md Examples of setting the file_uri_scheme attribute for standard, WSL, and custom scheme setups. ```ruby # Standard setup (no scheme) config.file_uri_scheme = nil # Opens: /tmp/mail.html # WSL setup config.file_uri_scheme = 'file://///wsl$/Ubuntu-18.04' # Opens: file://///wsl$/Ubuntu-18.04/tmp/mail.html # Custom scheme config.file_uri_scheme = 'file://' # Opens: file:///tmp/mail.html ``` -------------------------------- ### Rails Basic Setup Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md Integrates Letter Opener into a Rails application by adding the gem to the Gemfile and configuring Action Mailer in development environment. ```ruby # Gemfile group :development do gem 'letter_opener' end # config/environments/development.rb Rails.application.configure do # Enable email delivery and use letter_opener config.action_mailer.perform_deliveries = true config.action_mailer.delivery_method = :letter_opener end ``` -------------------------------- ### Email Size Limits Warning Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md An example of how to add a check to warn about large emails generated in the development environment. ```ruby # Warn about large emails in development class MyMailer < ApplicationMailer after_deliver do if Rails.env.development? size = File.size(mail['location_rich'].value || mail['location_plain'].value) if size > 10.megabytes Rails.logger.warn("Large email generated: #{size / 1024 / 1024}MB") end end end end ``` -------------------------------- ### Default Values Example Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/Configuration.md Example of initializing and configuring LetterOpener. ```ruby require "letter_opener" config = LetterOpener::Configuration.new config.location = File.expand_path('../tmp/my_mails', __FILE__) config.message_template = :light ``` -------------------------------- ### Example File Path Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/quick-reference.md An example of a file path for an email. ```plaintext tmp/letter_opener/1234567890.123456_abc123def456/ ``` -------------------------------- ### Configure LetterOpener Example Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/LetterOpener.md Example of configuring LetterOpener. ```ruby require "letter_opener" LetterOpener.configure do |config| config.location = File.expand_path('../tmp/mails', __FILE__) config.message_template = :default end ``` -------------------------------- ### Settings Accessor Example Output Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/DeliveryMethod.md Example output of the settings accessor. ```ruby delivery = LetterOpener::DeliveryMethod.new( location: '/tmp/mails', message_template: :light ) puts delivery.settings # => {:location=>"/tmp/mails", :message_template=>:light, :file_uri_scheme=>nil} ``` -------------------------------- ### Pony Gem Basic Setup Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md Integrates Letter Opener with the Pony gem for sending emails, configuring Pony to use Letter Opener as its delivery method. ```ruby require 'letter_opener' require 'pony' Pony.options = { via: LetterOpener::DeliveryMethod, via_options: { location: File.expand_path('../tmp/letter_opener', __FILE__) } } Pony.mail( to: 'user@example.com', from: 'noreply@example.com', subject: 'Hello', body: 'This opens in the browser' ) ``` -------------------------------- ### Rails Minimum Setup Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/quick-reference.md Configure Letter Opener for Rails development. ```ruby # Gemfile gem 'letter_opener', group: :development # config/environments/development.rb config.action_mailer.delivery_method = :letter_opener config.action_mailer.perform_deliveries = true ``` -------------------------------- ### Per-Instance Configuration Example Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/configuration.md Shows how individual Letter Opener DeliveryMethod instances can override global configuration. ```ruby # Global config LetterOpener.configure do |config| config.location = '/tmp/default' config.message_template = :default end # Create a specific delivery method with different settings dev_delivery = LetterOpener::DeliveryMethod.new( location: '/tmp/development', message_template: :light ) # Original global config unchanged prod_delivery = LetterOpener::DeliveryMethod.new( location: '/tmp/production' ) # Uses global message_template: :default ``` -------------------------------- ### type Example Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/Message.md Example demonstrating how to get the message type. ```ruby mail = Mail.new do content_type 'text/html' body '

Test

' end message = LetterOpener::Message.new(mail, location: '/tmp', message_template: :default) puts message.type # => "rich" ``` -------------------------------- ### User Mailer Test Example Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md An example RSpec test for a mailer that checks if the email is rendered to disk and verifies its content. ```ruby describe UserMailer do it 'sends welcome email' do mail = UserMailer.welcome(user) # Mail is rendered to disk expect(File.exist?(mail['location_plain'].value)).to be_truthy # Check rendered content rendered = File.read(mail['location_plain'].value) expect(rendered).to include('Welcome') expect(rendered).to include(user.email) end end ``` -------------------------------- ### message_template Examples Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/types.md Examples of setting the message_template attribute using both symbols and strings, and an example of per-instance override. ```ruby # Using symbol config.message_template = :default # Using string config.message_template = 'light' # Per-instance override delivery = LetterOpener::DeliveryMethod.new( location: '/tmp/mails', message_template: :light ) ``` -------------------------------- ### Location Attribute Examples Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/Configuration.md Examples of setting the location for rendered email HTML files. ```ruby config.location = Rails.root.join('tmp', 'letter_opener') config.location = File.expand_path('../tmp/my_mails', __FILE__) ``` -------------------------------- ### Document Format Example Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/MANIFEST.md An example of the consistent Markdown formatting used for all documentation files. ```markdown # Title Brief overview. ## Section Content and code. ### Subsection Details. **Type**: String **Default**: value **Example**: ```ruby code here ``` **Source**: `lib/file.rb:10-15` ``` -------------------------------- ### mail.from Examples Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/types.md Examples of accessing the mail.from attribute, showing single address and array of addresses. ```ruby mail.from # => ["sender@example.com"] mail.from # => "sender@example.com" ``` -------------------------------- ### File URI Scheme Attribute Examples Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/Configuration.md Examples of setting a custom file URI scheme prefix for opening files with Launchy. ```ruby config.file_uri_scheme = 'file://///wsl$/Ubuntu-18.04' config.file_uri_scheme = 'file://' # or any other custom scheme ``` -------------------------------- ### Constructor Examples Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/DeliveryMethod.md Initializes a new DeliveryMethod instance with the provided options. Merges provided options with global configuration values. ```ruby LetterOpener::DeliveryMethod.new(location: '/tmp/mails') LetterOpener::DeliveryMethod.new( location: '/tmp/mails', message_template: :light, file_uri_scheme: 'file://' ) ``` -------------------------------- ### Development vs Test Environment Configuration Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md Shows how to configure Action Mailer for development (using Letter Opener) and test environments. ```ruby # config/environments/test.rb config.action_mailer.delivery_method = :test # Captures in memory config.action_mailer.perform_deliveries = false # config/environments/development.rb config.action_mailer.delivery_method = :letter_opener # Opens in browser config.action_mailer.perform_deliveries = true # Then in tests: # expect(ActionMailer::Base.deliveries.length).to eq(1) # In development: # Email opens in browser when sent ``` -------------------------------- ### File System Performance Considerations Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md Provides guidance on optimizing Letter Opener's file system usage, including using SSDs, regular cleanup, and light templates. ```ruby # 1. Use SSD for tmp/ if handling many emails # 2. Regularly clean up old emails namespace :emails do task cleanup: :environment do location = LetterOpener.configuration.location threshold = 7.days.ago Dir["#{location}/*"].each do |dir| next unless File.directory?(dir) if File.mtime(dir) < threshold FileUtils.rm_rf(dir) end end puts "Cleaned up old emails" end end # 3. Use light template for minimal overhead LetterOpener.configure { |c| c.message_template = :light } ``` -------------------------------- ### Non-Rails Minimum Setup Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/quick-reference.md Configure Letter Opener for non-Rails Ruby projects. ```ruby require 'letter_opener' require 'mail' Mail.defaults do delivery_method LetterOpener::DeliveryMethod, location: '/tmp/mails' end Mail.deliver do from 'sender@example.com' to 'user@example.com' body 'Hello' end ``` -------------------------------- ### Message Template Attribute Examples Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/Configuration.md Examples of setting the message template for rendering email messages. ```ruby config.message_template = :default # Shows headers, subject, attachments config.message_template = :light # Body only, minimal HTML ``` -------------------------------- ### Settings Accessor Examples Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/DeliveryMethod.md Accessing the settings hash passed to the constructor. ```ruby delivery.settings[:location] delivery.settings[:message_template] delivery.settings[:file_uri_scheme] ``` -------------------------------- ### <=> Example Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/Message.md Example demonstrating the sorting behavior of messages. ```ruby plain = LetterOpener::Message.new(mail, location: '/tmp', message_template: :default) rich = LetterOpener::Message.new(html_mail, location: '/tmp', message_template: :default) sorted = [plain, rich].sort puts sorted.first.type # => "rich" puts sorted.last.type # => "plain" ``` -------------------------------- ### attachment_filename Example Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/Message.md Example showing the output of attachment_filename for a given input. ```text # For attachment with filename: "invoice (2024) @ 50% off!.pdf" # Returns: "invoice -2024--50- off-.pdf" ``` -------------------------------- ### Get Current Configuration Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/quick-reference.md Retrieve the current configuration values. ```ruby LetterOpener.configuration.location LetterOpener.configuration.message_template LetterOpener.configuration.file_uri_scheme ``` -------------------------------- ### file_uri_scheme Example Transformation Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/types.md An example transformation showing how a file_uri_scheme and filepath are combined into a final URI. ```ruby file_uri_scheme = 'file://///wsl$/Ubuntu-18.04' filepath = '/home/user/mail.html' final_uri = 'file://///wsl$/Ubuntu-18.04/home/user/mail.html' ``` -------------------------------- ### Deliver Method Example Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/DeliveryMethod.md Example of using the deliver! method with the Mail gem. ```ruby require "letter_opener" require "mail" Mail.defaults do delivery_method LetterOpener::DeliveryMethod, location: '/tmp/mails' end Mail.deliver do from 'sender@example.com' to 'recipient@example.com' subject 'Test Message' body 'This is a test' end ``` -------------------------------- ### Global Configuration Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/README.md Example of configuring Letter Opener globally for all instances. ```ruby LetterOpener.configure { |c| c.location = '/tmp/mails' } ``` -------------------------------- ### Configuration for WSL Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md Configures Letter Opener for WSL environments, including setting the file URI scheme based on the distribution. ```ruby # config/initializers/letter_opener.rb LetterOpener.configure do |config| config.location = Rails.root.join('tmp', 'letter_opener') # Configure for your WSL distribution if ENV['WSL_DISTRO_NAME'].present? || ENV['WSL_HOST'].present? # For WSL1 with Ubuntu distribution config.file_uri_scheme = 'file://///wsl$/Ubuntu-18.04' # For WSL2 # config.file_uri_scheme = 'file://///wsl.localhost/Ubuntu' # Or detect automatically distro = `wsl -l -v 2>/dev/null | grep '* ' | awk '{print $1}'`.strip.downcase case distro when 'ubuntu' config.file_uri_scheme = 'file://///wsl.localhost/Ubuntu' when 'debian' config.file_uri_scheme = 'file://///wsl.localhost/Debian' end end end ``` -------------------------------- ### mail.subject Example Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/types.md Example of accessing the mail.subject attribute. ```ruby mail.subject # => "Welcome to our service" ``` -------------------------------- ### Environment Variables for WSL Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md Sets environment variables for running Letter Opener in a WSL environment. ```bash # .env.local or shell profile export LAUNCHY_DRY_RUN=true export BROWSER=/dev/null ``` -------------------------------- ### Get Configuration Instance Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/LetterOpener.md Returns the global configuration instance. If one doesn't exist, creates and returns a new Configuration object. ```ruby LetterOpener.configuration ``` -------------------------------- ### h Example Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/Message.md Example demonstrating how to use h() for HTML escaping. ```ruby message = LetterOpener::Message.new(mail, location: '/tmp', message_template: :default) puts message.h("
Safe & Sound
") # => "<div>Safe & Sound</div>" ``` -------------------------------- ### auto_link Example Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/Message.md Example demonstrating how to use auto_link to convert URLs to HTML links. ```ruby text = "Check out http://example.com and https://test.com" message = LetterOpener::Message.new(mail, location: '/tmp', message_template: :default) linked = message.auto_link(text) # => "Check out http://example.com and https://test.com" ``` -------------------------------- ### Example Usage with Mail Gem, Pony Gem, and ActionMailer Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/DeliveryMethod.md Demonstrates how to integrate LetterOpener::DeliveryMethod with different email sending libraries. ```ruby require "letter_opener" # Using global configuration LetterOpener.configure do |config| config.location = '/tmp/letter_opener' end delivery = LetterOpener::DeliveryMethod.new # Using instance-specific options delivery = LetterOpener::DeliveryMethod.new( location: '/tmp/development_mails' ) # With Mail gem Mail.defaults do delivery_method LetterOpener::DeliveryMethod, location: '/tmp/mails' end # With Pony gem Pony.options = { via: LetterOpener::DeliveryMethod, via_options: { location: '/tmp/mails' } } # With ActionMailer (non-Rails) ActionMailer::Base.add_delivery_method :letter_opener, LetterOpener::DeliveryMethod ActionMailer::Base.delivery_method = :letter_opener ``` -------------------------------- ### Handling InvalidOption Exception Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/errors.md Example of how to handle the InvalidOption exception and set up the location option. ```ruby begin delivery = LetterOpener::DeliveryMethod.new rescue LetterOpener::DeliveryMethod::InvalidOption => e puts "Configuration error: #{e.message}" # Set up location and retry LetterOpener.configure do |config| config.location = Rails.root.join('tmp', 'letter_opener') end delivery = LetterOpener::DeliveryMethod.new end ``` -------------------------------- ### Fix for Missing Configuration (InvalidOption) Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/quick-reference.md Example of fixing the InvalidOption error by configuring the location. ```ruby # Cause: No location configured # Fix: Set location LetterOpener.configure { |c| c.location = '/tmp/mails' } ``` -------------------------------- ### Rails Configuration Example Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/README.md Example configuration for Letter Opener within a Rails application's development environment. ```ruby # config/environments/development.rb Rails.application.configure do # Enable email delivery (required) config.action_mailer.perform_deliveries = true # Use letter_opener (required) config.action_mailer.delivery_method = :letter_opener # Optional: configure letter_opener gem # If omitted, uses default: Rails.root.join('tmp', 'letter_opener') end # Or in config/initializers/letter_opener.rb LetterOpener.configure do |config| config.location = Rails.root.join('tmp', 'letter_opener') config.message_template = :default end ``` -------------------------------- ### Rails Integration Example Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/DeliveryMethod.md Configuration for using LetterOpener::DeliveryMethod in Rails applications. ```ruby # config/environments/development.rb config.action_mailer.delivery_method = :letter_opener config.action_mailer.perform_deliveries = true ``` -------------------------------- ### Per-instance Configuration Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/README.md Example of configuring Letter Opener on a per-instance basis, overriding global settings. ```ruby LetterOpener::DeliveryMethod.new(location: '/tmp/other') ``` -------------------------------- ### Send an email in Rails Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/quick-reference.md Example of sending an email that will be opened by Letter Opener. ```ruby UserMailer.welcome(user).deliver_now # Opens in browser ``` -------------------------------- ### rake tmp:letter_opener example output Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/Railtie.md Example output when running the `rake tmp:letter_opener` command. ```bash $ bundle exec rake tmp:letter_opener rm -rf tmp/letter_opener/1234567890.123456_abc123 rm -rf tmp/letter_opener/1234567891.234567_def456 ``` -------------------------------- ### Rails Quick Start Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/INDEX.md Commands and configuration for integrating Letter Opener into a Rails application. ```bash gem "letter_opener", group: :development bundle install ``` ```ruby # config/environments/development.rb config.action_mailer.delivery_method = :letter_opener config.action_mailer.perform_deliveries = true ``` ```ruby UserMailer.welcome(user).deliver_now # Opens in browser ``` -------------------------------- ### Non Rails Setup - Pony Gem Source: https://github.com/ryanb/letter_opener/blob/master/README.md Set up Letter Opener with the Pony gem. ```ruby require "letter_opener" Pony.options = { via: LetterOpener::DeliveryMethod, via_options: {location: File.expand_path('../tmp/letter_opener', __FILE__)} } ``` -------------------------------- ### Basic Sinatra Integration Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md Integrates Letter Opener with Sinatra by configuring Mail.defaults to use LetterOpener::DeliveryMethod. ```ruby require 'sinatra' require 'letter_opener' require 'mail' configure do LetterOpener.configure do |config| config.location = File.expand_path('../tmp/letter_opener', __FILE__) end Mail.defaults do delivery_method LetterOpener::DeliveryMethod end end post '/send-email' do Mail.deliver do from 'app@example.com' to params[:email] subject 'Your Email' body params[:message] end "Email sent! Check your browser." end ``` -------------------------------- ### Pony Gem Integration Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/configuration.md Example of integrating Letter Opener with the Pony gem for sending emails. ```ruby require "letter_opener" Pony.options = { via: LetterOpener::DeliveryMethod, via_options: { location: File.expand_path('../tmp/mails', __FILE__), message_template: :default } } Pony.mail( to: 'user@example.com', from: 'app@example.com', subject: 'Welcome', body: 'Welcome to our app!' ) ``` -------------------------------- ### Rails Setup - Add Gem Source: https://github.com/ryanb/letter_opener/blob/master/README.md Add the letter_opener gem to your development environment. ```ruby gem "letter_opener", group: :development ``` -------------------------------- ### Custom Template Example (Unsupported) Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/templates.md An example of creating and using a custom ERB template for Letter Opener messages. This is noted as unsupported. ```ruby # Create custom template at: app/views/letter_opener/custom.html.erb

<%= subject %>

From: <%= from %>

<%= body %> # Use it messages = LetterOpener::Message.rendered_messages( mail, message_template: 'custom' # relative path in views directory ) ``` -------------------------------- ### Rails With ActionMailer Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md Demonstrates sending an email using Action Mailer in a Rails application, which will be opened by Letter Opener in the browser. ```ruby # app/mailers/user_mailer.rb class UserMailer < ApplicationMailer def welcome_email(user) @user = user mail(to: user.email, subject: 'Welcome!') end end # In controller or console: UserMailer.welcome_email(user).deliver_now # Email opens in default browser automatically ``` -------------------------------- ### Configuration in Docker Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md Configuration for Letter Opener within a Docker environment, including setting the mail storage location and delivery method. ```ruby # config/initializers/letter_opener.rb LetterOpener.configure do |config| # Store emails in a volume if available mail_dir = ENV['MAIL_STORAGE'] || '/app/tmp/letter_opener' config.location = mail_dir # Optional: custom URI scheme for accessing files # config.file_uri_scheme = 'file://///wsl$/Ubuntu-18.04' if ENV['WSL'] end # config/environments/development.rb config.action_mailer.delivery_method = :letter_opener config.action_mailer.perform_deliveries = true ``` -------------------------------- ### Rails Quick Start Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/README.md Configuration for using Letter Opener in a Rails application. ```ruby # Gemfile gem 'letter_opener', group: :development # config/environments/development.rb config.action_mailer.delivery_method = :letter_opener config.action_mailer.perform_deliveries = true # That's it! Emails now open in the browser UserMailer.welcome(user).deliver_now ``` -------------------------------- ### Conditional Delivery Method Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md Configures Pony to use Letter Opener for development environments and SMTP for production. ```ruby require 'letter_opener' require 'pony' if ENV['ENVIRONMENT'] == 'development' Pony.options = { via: LetterOpener::DeliveryMethod, via_options: { location: File.expand_path('../tmp/letter_opener', __FILE__) } } else # Use real SMTP in production Pony.options = { via: :smtp, via_options: { address: 'smtp.example.com', port: 587, user_name: ENV['SMTP_USER'], password: ENV['SMTP_PASSWORD'] } } end Pony.mail(to: 'user@example.com', subject: 'Test', body: 'Content') ``` -------------------------------- ### Non Rails Setup - Mail Gem Source: https://github.com/ryanb/letter_opener/blob/master/README.md Set up Letter Opener with the Mail gem. ```ruby require "letter_opener" Mail.defaults do delivery_method LetterOpener::DeliveryMethod, location: File.expand_path('../tmp/letter_opener', __FILE__) end ``` -------------------------------- ### Configuration with ActionMailer Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md Configures Letter Opener using the LetterOpener.configure block and sets it as the default delivery method for Action Mailer. ```ruby require 'letter_opener' require 'action_mailer' LetterOpener.configure do |config| config.location = '/tmp/mailers' config.message_template = :default end ActionMailer::Base.add_delivery_method( :letter_opener, LetterOpener::DeliveryMethod ) ActionMailer::Base.delivery_method = :letter_opener class UserMailer < ActionMailer::Base def confirmation(email) @email = email mail( to: email, from: 'confirm@example.com', subject: 'Confirm your email' ) do |format| format.text { render text: "Confirm at: example.com/confirm" } format.html { render html: "

Confirm at: link

" } end end end UserMailer.confirmation('user@example.com').deliver_now ``` -------------------------------- ### Rails Development Setup Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/configuration.md Configures Letter Opener for a Rails development environment, enabling email delivery and setting the delivery method and location. ```ruby # config/environments/development.rb Rails.application.configure do # ... other settings ... # Enable email delivery config.action_mailer.perform_deliveries = true # Use letter_opener for email preview config.action_mailer.delivery_method = :letter_opener # Configure letter_opener config.letter_opener_location = Rails.root.join('tmp', 'letter_opener') end # Alternatively, in an initializer (config/initializers/letter_opener.rb): LetterOpener.configure do |config| config.location = Rails.root.join('tmp', 'letter_opener') config.message_template = :default end ``` -------------------------------- ### Rails Setup - Delivery Method Source: https://github.com/ryanb/letter_opener/blob/master/README.md Set the delivery method in config/environments/development.rb to use Letter Opener. ```ruby config.action_mailer.delivery_method = :letter_opener config.action_mailer.perform_deliveries = true ``` -------------------------------- ### ActionMailer without Rails Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/configuration.md Example of using Letter Opener with Action Mailer in a non-Rails application. ```ruby require "letter_opener" require "action_mailer" ActionMailer::Base.add_delivery_method( :letter_opener, LetterOpener::DeliveryMethod, location: File.expand_path('../tmp/letter_opener', __FILE__) ) ActionMailer::Base.delivery_method = :letter_opener class UserMailer < ActionMailer::Base def welcome(user) mail(to: user.email, subject: 'Welcome!') end end UserMailer.welcome(user).deliver_later ``` -------------------------------- ### Fix for Missing Email Address (ArgumentError) Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/quick-reference.md Example of fixing ArgumentError by setting the 'from' address. ```ruby # Cause: Email has no from address # Fix: Set from address mail = Mail.new(from: 'sender@example.com', to: 'user@example.com') ``` -------------------------------- ### Non-Rails with Mail Gem - Basic Setup Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/configuration.md Configures the Mail gem to use Letter Opener as the delivery method with a specified location. ```ruby require "letter_opener" require "mail" Mail.defaults do delivery_method LetterOpener::DeliveryMethod, location: File.expand_path('../tmp/letter_opener', __FILE__) end ``` -------------------------------- ### Rails With Configuration Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md Customizes Letter Opener's behavior in Rails by setting a custom storage location for emails and using a minimal template. ```ruby # config/initializers/letter_opener.rb LetterOpener.configure do |config| # Store emails in custom directory config.location = Rails.root.join('tmp', 'development_emails') # Use minimal template config.message_template = :light # For WSL users # config.file_uri_scheme = 'file://///wsl$/Ubuntu-18.04' end ``` -------------------------------- ### File Organization Example Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/README.md Illustrates the directory structure created by Letter Opener for storing emails and attachments. ```text location/ ├── 1234567890.123456_abc123def456/ │ ├── rich.html (HTML version, if multipart) │ ├── plain.html (Plain text version) │ └── attachments/ │ ├── document.pdf │ └── image.png ├── 1234567891.234567_xyz789/ │ └── plain.html └── ... ``` -------------------------------- ### Alternative Gem Installation (Not Recommended) Source: https://github.com/ryanb/letter_opener/wiki/Known-Issues This snippet shows an alternative, but not recommended, way to include the letter_opener gem. ```ruby group :development do gem "letter_opener" end ``` -------------------------------- ### Fix for Missing Email Address (ArgumentError) Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/quick-reference.md Example of fixing ArgumentError by setting the 'to' address. ```ruby # Cause: Email has no to address # Fix: Set to address mail = Mail.new(to: 'user@example.com', from: 'sender@example.com') ``` -------------------------------- ### Handling Errno::ENOENT (Template file not found) Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/errors.md Example of rescuing Errno::ENOENT when a template file is not found and falling back to a default template. ```ruby begin message = LetterOpener::Message.new( mail, location: '/tmp/mails', message_template: :nonexistent ) message.render rescue Errno::ENOENT => e puts "Template not found: #{e.message}" # Fall back to default template message = LetterOpener::Message.new( mail, location: '/tmp/mails', message_template: :default ) message.render end ``` -------------------------------- ### Finding Gem Templates Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/templates.md Bash commands to locate the Letter Opener gem installation and the directory where default and light templates are stored. ```bash # Find gem location gem which letter_opener # => /path/to/gems/letter_opener-1.10.0/lib/letter_opener.rb # Templates are at: # /path/to/gems/letter_opener-1.10.0/lib/letter_opener/templates/default.html.erb # /path/to/gems/letter_opener-1.10.0/lib/letter_opener/templates/light.html.erb ``` -------------------------------- ### Mail Gem With Configuration Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md Configures Letter Opener globally using LetterOpener.configure and then applies it to the Mail gem's delivery method. ```ruby require 'letter_opener' require 'mail' # Configure globally LetterOpener.configure do |config| config.location = '/tmp/letter_opener' config.message_template = :default end # Use with Mail Mail.defaults do delivery_method LetterOpener::DeliveryMethod end Mail.deliver do from 'sender@example.com' to 'recipient@example.com' subject 'Test Message' body 'This email opens in the browser' end ``` -------------------------------- ### Non-Rails Quick Start Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/INDEX.md Commands and configuration for using Letter Opener outside of a Rails application. ```bash gem "letter_opener" bundle install ``` ```ruby require "letter_opener" require "mail" Mail.defaults do delivery_method LetterOpener::DeliveryMethod, location: "/tmp/mails" end Mail.deliver do from "sender@example.com" to "user@example.com" body "Test" end ``` -------------------------------- ### Handling ArgumentError (location or default configuration must be given) Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/errors.md Example of how to rescue an ArgumentError when Letter Opener is not configured and then configure it. ```ruby begin message = LetterOpener::Message.new(mail) rescue ArgumentError => e if e.message.include?('or default configuration must be given') puts "Configuration missing: #{e.message}" # Set up configuration LetterOpener.configure do |config| config.location = '/tmp/letter_opener' config.message_template = :default end # Retry message = LetterOpener::Message.new(mail) end end ``` -------------------------------- ### Custom Rake Task for Clearing Emails Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/quick-reference.md Example of a custom Rake task to clear emails. ```ruby # lib/tasks/letter_opener.rake namespace :emails do task clear: :environment do location = LetterOpener.configuration.location FileUtils.rm_rf Dir["#{location}/*"] end end ``` -------------------------------- ### Constructor Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/Configuration.md Creates a new configuration instance with default values. ```ruby LetterOpener::Configuration.new ``` -------------------------------- ### Recommended Gem Installation Source: https://github.com/ryanb/letter_opener/wiki/Known-Issues This snippet provides the recommended way to include the letter_opener gem in the development group of a Gemfile. ```ruby group :development do gem "letter_opener", github: 'ryanb/letter_opener' end ``` -------------------------------- ### Performance Tip: Reduce Template Size Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/quick-reference.md Using the light template to reduce file size. ```ruby # Use light template for smaller files LetterOpener.configure { |c| c.message_template = :light } ``` -------------------------------- ### Fix for Invalid Template (Errno::ENOENT) Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/quick-reference.md Example of fixing Errno::ENOENT by using a valid template name. ```ruby # Cause: Invalid template name # Fix: Use :default or :light LetterOpener.configure { |c| c.message_template = :default } ``` -------------------------------- ### location Possible Values Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/types.md Examples of possible values for the location attribute, including absolute and relative paths, and usage with Rails.root.join() and File.expand_path(). ```ruby # Rails config.location = Rails.root.join('tmp', 'my_mails') # Non-Rails with File.expand_path config.location = File.expand_path('../tmp/letter_opener', __FILE__) # Direct path config.location = '/var/tmp/letter_opener' # Pathname object config.location = Pathname.new('/tmp/mails') ``` -------------------------------- ### Non Rails Setup - Action Mailer Directly Source: https://github.com/ryanb/letter_opener/blob/master/README.md Add the delivery method if using Action Mailer directly without Rails. ```ruby require "letter_opener" ActionMailer::Base.add_delivery_method :letter_opener, LetterOpener::DeliveryMethod, :location => File.expand_path('../tmp/letter_opener', __FILE__) ActionMailer::Base.delivery_method = :letter_opener ``` -------------------------------- ### Template Context Binding Example Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/templates.md ERB code illustrating how instance methods and local variables of the Message object are accessible within the template via the binding. ```erb <%= from %> <%= h(body) %> <%= auto_link(text) %> <%= mail.subject %> <% if mail.multipart? %> ... <% end %> ``` -------------------------------- ### Mail Gem Error Handling Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md Demonstrates how to handle potential errors when sending emails with the Mail gem and Letter Opener, such as invalid configuration or invalid email addresses. ```ruby begin Mail.deliver do from 'sender@example.com' to 'recipient@example.com' subject 'Test' body 'Content' end rescue LetterOpener::DeliveryMethod::InvalidOption => e puts "Letter Opener not configured: #{e.message}" puts "Configure with:" puts " LetterOpener.configure { |c| c.location = '/tmp/mails' }" rescue ArgumentError => e puts "Invalid email: #{e.message}" puts "Make sure from and to addresses are set" end ``` -------------------------------- ### Performance Tip: Monitor File Size Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/quick-reference.md Checking the size of an email file. ```ruby # Check email size mail = UserMailer.welcome(user).deliver_now path = mail['location_plain'].value size_mb = File.size(path) / 1024 / 1024 puts "Email size: #{size_mb}MB" ``` -------------------------------- ### Mailer Example Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/README.md Example of using Letter Opener within a Rails mailer to preview emails. ```ruby class UserMailer < ApplicationMailer def welcome(user) @user = user mail(to: user.email, subject: 'Welcome!') # With delivery_method = :letter_opener, this opens in the browser end end # In controller or console: UserMailer.welcome(user).deliver_now # Email preview opens automatically ``` -------------------------------- ### Create Delivery Method with Custom Options Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/quick-reference.md Instantiate LetterOpener::DeliveryMethod with custom options. ```ruby delivery = LetterOpener::DeliveryMethod.new( location: '/custom/path', message_template: :light ) # For Mail gem Mail.defaults do delivery_method LetterOpener::DeliveryMethod, location: '/tmp' end # For ActionMailer ActionMailer::Base.add_delivery_method :letter_opener, LetterOpener::DeliveryMethod, location: '/tmp' ``` -------------------------------- ### Configure location with File.expand_path Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/configuration.md Sets the file system path for storing rendered email HTML files using File.expand_path. ```ruby LetterOpener.configure do |config| config.location = File.expand_path('../tmp/letter_opener', __FILE__) end ``` -------------------------------- ### Preventing InvalidOption Exception Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/errors.md Examples of how to prevent the InvalidOption exception by configuring the location option. ```ruby # Always configure location globally LetterOpener.configure do |config| config.location = Rails.root.join('tmp', 'letter_opener') end # Or pass it to the constructor Mail.defaults do delivery_method LetterOpener::DeliveryMethod, location: '/tmp/mails' end ``` -------------------------------- ### Configure message template with a string Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/configuration.md Sets the message template to 'default' using a string. ```ruby LetterOpener.configure do |config| config.message_template = 'default' end ``` -------------------------------- ### Configure file_uri_scheme with file:// Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/configuration.md Sets the file URI scheme to 'file://'. ```ruby LetterOpener.configure do |config| config.file_uri_scheme = 'file://' end ``` -------------------------------- ### Launchy Control Environment Variables Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/quick-reference.md Environment variables for controlling Launchy behavior, useful in environments like WSL or Docker. ```bash # Launchy Control (for WSL, Docker, etc.): export LAUNCHY_DRY_RUN=true # Don't actually open browser export BROWSER=/dev/null # Disable browser opening export LAUNCHY_DEBUG=true # Enable debug output export LAUNCHY_APPLICATION=firefox # Specify browser ``` -------------------------------- ### Handling ArgumentError (SMTP To address blank) Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/errors.md Example of how to handle the ArgumentError when the SMTP To address is blank. ```ruby begin delivery_method.deliver!(mail) rescue ArgumentError => e if e.message.include?('SMTP To') puts "Missing recipient address: #{e.message}" # Handle missing recipient end end ``` -------------------------------- ### Handling ArgumentError (SMTP From address blank) Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/errors.md Example of how to handle the ArgumentError when the SMTP From address is blank. ```ruby begin delivery_method.deliver!(mail) rescue ArgumentError => e if e.message.include?('SMTP From') puts "Missing sender address: #{e.message}" # Re-create mail with valid sender end end ``` -------------------------------- ### Send an email with Mail Gem Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/configuration.md Demonstrates sending a test email using the Mail gem, which will be handled by Letter Opener. ```ruby # Send an email Mail.deliver do from 'sender@example.com' to 'recipient@example.com' subject 'Test Email' body 'This will open in the browser' end ``` -------------------------------- ### DeliveryMethod Options Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/quick-reference.md Configuration options for the DeliveryMethod. ```ruby { location: '/tmp/mails', # Required: directory path message_template: :default, # Optional: :default or :light file_uri_scheme: 'file://' # Optional: URI scheme prefix } ``` -------------------------------- ### Configuration Attributes Cheat Sheet Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/quick-reference.md Accessing configuration attributes of Letter Opener. ```ruby config = LetterOpener.configuration config.location # => String or Pathname config.message_template # => "default" or "light" config.file_uri_scheme # => String or nil ``` -------------------------------- ### Preventing ArgumentError (SMTP To address blank) Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/errors.md Examples of how to prevent the ArgumentError by ensuring a valid To address is set. ```ruby # Always set recipient(s) Mail.deliver do from 'noreply@example.com' to 'user@example.com' # Required subject 'Test' body 'Content' end # Note: CC or BCC without To will raise this error # This is valid: Mail.deliver do from 'noreply@example.com' cc 'manager@example.com' to 'user@example.com' # At least one To is required body 'Content' end ``` -------------------------------- ### Configure Letter Opener Globally Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/quick-reference.md Set global configuration options for Letter Opener. ```ruby LetterOpener.configure do |config| config.location = '/tmp/letter_opener' # Directory for emails config.message_template = :default # :default or :light config.file_uri_scheme = nil # Custom URI scheme end ``` -------------------------------- ### Preventing ArgumentError (SMTP From address blank) Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/errors.md Examples of how to prevent the ArgumentError by ensuring a valid From address is set. ```ruby # Always set a from address Mail.deliver do from 'noreply@example.com' # Required to 'user@example.com' subject 'Test' body 'Content' end # Or verify before delivery raise ArgumentError, "From address required" if mail.from.blank? delivery_method.deliver!(mail) ``` -------------------------------- ### Send an email Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/api-reference/Railtie.md Example of sending an email in a Rails controller or mailer, which will trigger the Letter Opener preview. ```ruby # In a Rails controller or mailer UserMailer.welcome_email(user).deliver_now # Email preview opens in the default browser ``` -------------------------------- ### Development Only Configuration Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/quick-reference.md Configure Letter Opener only for the development environment. ```ruby # config/environments/development.rb config.action_mailer.delivery_method = :letter_opener config.action_mailer.perform_deliveries = true # config/environments/production.rb config.action_mailer.delivery_method = :smtp # ... SMTP settings ... ``` -------------------------------- ### Configure message template to :light Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/configuration.md Sets the message template to ':light' for a minimal preview of the email body. ```ruby LetterOpener.configure do |config| config.message_template = :light end ``` -------------------------------- ### LetterOpener configuration in development environment Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/errors.md Recommended configuration for Letter Opener in the development environment. ```ruby # config/initializers/letter_opener.rb begin LetterOpener.configure do |config| config.location = Rails.root.join('tmp', 'letter_opener') config.message_template = :default end rescue => e Rails.logger.error("LetterOpener configuration error: #{e.message}") end ``` -------------------------------- ### RSpec Configuration for Mailers Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md Configures Letter Opener to use a specific directory for test emails and cleans up the directory after each test. ```ruby RSpec.configure do |config| config.before(:each, type: :mailer) do LetterOpener.configure do |c| c.location = Rails.root.join('tmp/test_mails') end end config.after(:each, type: :mailer) do FileUtils.rm_rf Rails.root.join('tmp/test_mails') end end ``` -------------------------------- ### Error logging for email delivery Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/errors.md Example of how to log various errors that might occur during email delivery using Letter Opener. ```ruby begin Mail.deliver do from 'app@example.com' to recipient subject 'Email' body 'Content' end rescue LetterOpener::DeliveryMethod::InvalidOption => e Rails.logger.error("Letter Opener not configured: #{e.message}") rescue ArgumentError => e Rails.logger.error("Invalid email content: #{e.message}") rescue => e Rails.logger.error("Unexpected error delivering email: #{e.class} - #{e.message}") end ``` -------------------------------- ### Use Light Template (Global) Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/README.md Configures the 'light' template globally for Letter Opener. ```ruby # Global: LetterOpener.configure { |c| c.message_template = :light } ``` -------------------------------- ### DeliveryMethod Settings Cheat Sheet Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/quick-reference.md Accessing settings for the DeliveryMethod. ```ruby delivery.settings[:location] # => String or Pathname delivery.settings[:message_template] # => String delivery.settings[:file_uri_scheme] # => String or nil ``` -------------------------------- ### Custom Email Directory Configuration Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/quick-reference.md Configure a custom directory for storing emails in a Rails application. ```ruby LetterOpener.configure do |config| config.location = Rails.root.join('var', 'emails') end ``` -------------------------------- ### Configure location with Rails root Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/configuration.md Sets the file system path for storing rendered email HTML files using Rails.root.join. ```ruby LetterOpener.configure do |config| config.location = Rails.root.join('tmp', 'my_mails') end ``` -------------------------------- ### file_uri_scheme How It Works Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/types.md Illustrates how the file_uri_scheme is used to construct the final URI for opening files with Launchy. ```ruby uri = "#{settings[:file_uri_scheme]}#{filepath}" Launchy.open(uri) ``` -------------------------------- ### Docker Environment Configuration Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/configuration.md Configuration for Letter Opener within a Docker environment. ```ruby # config/environments/development.rb (or initializer) LetterOpener.configure do |config| config.location = '/app/tmp/letter_opener' # May need custom scheme depending on how you access files # If mounting volumes, might need: file:///host/path/to/files end ``` -------------------------------- ### Pony Gem With HTML Content Source: https://github.com/ryanb/letter_opener/blob/master/_autodocs/integration-guide.md Sends an HTML email using the Pony gem with Letter Opener, displaying rich content in the browser preview. ```ruby Pony.mail( to: 'user@example.com', from: 'noreply@example.com', subject: 'Welcome', html_body: '

Welcome!

Click to get started.

' ) ```