Skip to main content

Email

CmAdmin.send_email sends an email using a partial or HTML body, with optional CC/BCC, a link button and attachments. Deliveries can be logged to cm_email_logs.

Quick example​

CmAdmin.send_email(
to: ["anbu@commutatus.com"],
subject: "[DMSv2] New Prospect Created!",
partial_file_path: "/cm_admin/mailers/prospect_admin_notification",
partial_locals: { user: User.last }
)

Configuration​

Email logging is disabled by default. To enable it:

config/initializers/cm_admin.rb
CmAdmin.config.enable_email_logging = true
rails g cm_admin:add_email_logs
rails db:migrate

Every email sent through CmAdmin.send_email is then logged in cm_email_logs with delivery status, failure reasons and recipient tracking.

Options​

OptionRequiredDefaultDescription
fromNoproject settingSender address.
toYesβ€”Recipients. Array or single address.
ccNoβ€”CC recipients. Array or single address.
bccNoβ€”BCC recipients. Array or single address.
reply_toNoβ€”Reply-to address.
subjectYesβ€”Email subject.
templateNo:standardLayout: :standard or :blank.
partial_file_pathNoβ€”Path to the partial used as the email body.
partial_localsNoβ€”Locals passed to the partial.
bodyNoβ€”HTML or plain-text body.
button_textNoβ€”Label for the link button (with button_link).
button_linkNoβ€”URL for the link button.
attachmentsNoβ€”Array of attachment hashes. See Notes.
recordNoβ€”Record to link the email log to. Sets module_name, record_type, record_id and record_url.
notify_on_failureNotrueSend an admin notification email when delivery fails. Set to false for failure-notification emails themselves, to avoid loops.

Recipes​

Email with a partial template​

CmAdmin.send_email(
to: ['anbu@commutatus.com'],
subject: "[DMSv2] New Prospect Created!",
partial_file_path: '/cm_admin/mailers/prospect_admin_notification',
partial_locals: { user: User.last }
)

CC, BCC and reply-to​

CmAdmin.send_email(
to: 'user@example.com',
cc: 'manager@example.com',
bcc: ['admin@example.com', 'audit@example.com'],
reply_to: 'noreply@example.com',
subject: "Project Update",
body: '<p>Here is the latest project update.</p>'
)

Custom from address​

CmAdmin.send_email(
from: 'info@commutatus.com',
to: ['anbu@commutatus.com'],
subject: "[DMSv2] New Prospect Created!",
partial_file_path: '/cm_admin/mailers/prospect_admin_notification',
partial_locals: { user: User.last }
)
CmAdmin.send_email(
to: ['info@commutatus.com'],
subject: "New User Created!",
partial_file_path: '/cm_admin/mailers/user_admin_notification',
partial_locals: { user: User.last },
button_text: 'View user',
button_link: 'http://domain.com/user/1'
)

Plain HTML body​

CmAdmin.send_email(
to: ['anbu@commutatus.com'],
subject: "[DMSv2] New Prospect Created!",
body: '<p>Simple HTML content to be added here.</p>'
)

Blank template​

CmAdmin.send_email(
to: ['anbu@commutatus.com'],
subject: "[DMSv2] New Prospect Created!",
template: :blank,
body: '<p>Simple HTML content to be added here.</p>'
)

Attachments​

CmAdmin.send_email(
to: ['anbu@commutatus.com'],
subject: "[DMSv2] New Prospect Created!",
body: '<p>Simple HTML content to be added here.</p>',
attachments: [
{ filename: 'sample.pdf', file_content: '...' },
{ filename: 'sample.pdf', url_path: '...' }
]
)

Notes​

  • Each attachment is a hash with filename plus file_content (base64) or url_path.
  • file_content is the base64-encoded file:
    • From a path: Base64.encode64(File.open(file_path).read)
    • From a URL: Base64.encode64(URI.open(file_url).read)