### Create a Basic Email with MailFactory Source: https://context7.com/tmm1/mailfactory/llms.txt Initialize a MailFactory instance and assign headers and body content dynamically. The to_s method generates the final RFC-compliant email string. ```ruby require 'mailfactory' # Create a new email message mail = MailFactory.new # Set basic headers mail.to = "recipient@example.com" mail.from = "sender@example.com" mail.subject = "Hello from MailFactory" # Set plain text body mail.text = "This is the plain text version of the email.\n\nIt supports multiple lines." # Convert to string for sending email_string = mail.to_s # => Returns complete RFC-compliant email with headers, message-id, and body ``` -------------------------------- ### Manage Custom Headers in MailFactory Source: https://context7.com/tmm1/mailfactory/llms.txt Demonstrates setting, adding, retrieving, and removing email headers using dynamic methods and explicit header control methods. ```ruby require 'mailfactory' mail = MailFactory.new mail.to = "recipient@example.com" mail.from = "sender@example.com" mail.subject = "Test" mail.text = "Testing custom headers." # Set Reply-To header mail.replyto = "replies@example.com" # Use dynamic methods (underscores become hyphens) mail.x_mailer = "MyApp/1.0" # Sets "X-Mailer" header mail.x_custom_header = "custom value" # Sets "X-Custom-Header" header # Use camelCase (split on capitals) mail.XPriority = "1" # Sets "X-Priority" header # Direct header manipulation mail.set_header("X-Custom", "value") # Set or replace header mail.add_header("X-Tag", "important") # Add header (allows duplicates) mail.add_header("X-Tag", "urgent") # Add another X-Tag header # Retrieve headers (returns array) tags = mail.get_header("X-Tag") # => ["important", "urgent"] # Remove a header mail.remove_header("X-Priority") puts mail.to_s ``` -------------------------------- ### Attach Files with Custom Names and IO Streams Source: https://context7.com/tmm1/mailfactory/llms.txt Use attach_as to rename files during attachment or to attach content directly from IO streams. ```ruby require 'mailfactory' mail = MailFactory.new mail.to = "recipient@example.com" mail.from = "sender@example.com" mail.subject = "Report" mail.text = "Attached is the quarterly report." # Attach file with a different name in the email mail.attach_as("/tmp/report_2024_q1_final_v3.xlsx", "Q1_Report.xlsx") # Attach from an IO stream File.open("/path/to/data.csv", "rb") do |file| mail.attach_as(file, "export.csv", "text/csv") end # Attach with custom headers for inline display mail.add_attachment_as("/path/to/chart.png", "chart.png", "image/png", "Content-ID: ") puts mail.to_s ``` -------------------------------- ### Create Multipart HTML Emails Source: https://context7.com/tmm1/mailfactory/llms.txt Set both text and html properties to create a multipart/alternative message. Use rawhtml for full control over the HTML document structure. ```ruby require 'mailfactory' mail = MailFactory.new mail.to = "recipient@example.com" mail.from = "sender@example.com" mail.subject = "Newsletter" # Plain text fallback mail.text = "Welcome to our newsletter!\n\nVisit our website for more information." # HTML version (only provide the body content) mail.html = "

Welcome to our newsletter!

Visit our website for more information.

" # For complete HTML control, use rawhtml= instead mail.rawhtml = "Newsletter

Custom HTML

" puts mail.to_s # => Returns multipart/alternative email with both text and HTML parts ``` -------------------------------- ### Add File Attachments Source: https://context7.com/tmm1/mailfactory/llms.txt Use the attach method to include files. MIME types are detected automatically if the mime-types gem is present, or can be specified manually. ```ruby require 'mailfactory' mail = MailFactory.new mail.to = "recipient@example.com" mail.from = "sender@example.com" mail.subject = "Documents Attached" mail.text = "Please find the requested documents attached." # Attach files (MIME type auto-detected) mail.attach("/path/to/document.pdf") mail.attach("/path/to/image.png") # Attach with explicit MIME type mail.attach("/path/to/data.bin", "application/octet-stream") # Attach with custom headers (e.g., for Content-ID in inline images) mail.attach("/path/to/logo.png", "image/png", "Content-ID: ") puts mail.to_s # => Returns multipart/mixed email with attachments base64 encoded ``` -------------------------------- ### Send Email via SMTP Source: https://context7.com/tmm1/mailfactory/llms.txt Use Ruby's Net::SMTP library to transmit the email string generated by MailFactory. ```ruby require 'net/smtp' require 'mailfactory' mail = MailFactory.new mail.to = "recipient@example.com" mail.from = "sender@example.com" mail.subject = "Important Update" mail.text = "Please review the attached documents." # Send via SMTP Net::SMTP.start('smtp.example.com', 25, 'mail.example.com', 'username', 'password', :cram_md5) do |smtp| smtp.send_message(mail.to_s, 'sender@example.com', 'recipient@example.com') end ``` -------------------------------- ### Construct Email Without Automatic Headers Source: https://context7.com/tmm1/mailfactory/llms.txt Uses the construct method to generate email output while optionally disabling automatic Message-ID and Date header generation. ```ruby require 'mailfactory' mail = MailFactory.new mail.to = "recipient@example.com" mail.from = "sender@example.com" mail.subject = "Test" mail.text = "Hello World" # Build without auto-generated message-id (to_s adds message-id automatically) email_without_msgid = mail.construct(messageid: false) # Build without auto-generated date header email_no_date = mail.construct(date: false) # Build with both (equivalent to to_s) email_full = mail.construct(messageid: true, date: true) # Check if email will be multipart if mail.multipart? puts "Email has HTML content or attachments" end ``` -------------------------------- ### Handle International Characters Source: https://context7.com/tmm1/mailfactory/llms.txt Shows how MailFactory automatically applies quoted-printable encoding to subjects and sender names containing non-ASCII characters. ```ruby require 'mailfactory' mail = MailFactory.new # International characters in subject (auto-encoded) mail.subject = "Réunion à 14h - Änderungen" # Stored as: =?utf-8?Q?R=C3=A9union_=C3=A0_14h_-_=C3=84nderungen?= # International characters in sender name mail.from = "François Müller " # Properly quoted with name preserved mail.to = "recipient@example.com" mail.text = "Meeting details enclosed." puts mail.subject # Returns encoded subject puts mail.to_s # Full email with proper encoding ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.