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β
| Option | Required | Default | Description |
|---|---|---|---|
from | No | project setting | Sender address. |
to | Yes | β | Recipients. Array or single address. |
cc | No | β | CC recipients. Array or single address. |
bcc | No | β | BCC recipients. Array or single address. |
reply_to | No | β | Reply-to address. |
subject | Yes | β | Email subject. |
template | No | :standard | Layout: :standard or :blank. |
partial_file_path | No | β | Path to the partial used as the email body. |
partial_locals | No | β | Locals passed to the partial. |
body | No | β | HTML or plain-text body. |
button_text | No | β | Label for the link button (with button_link). |
button_link | No | β | URL for the link button. |
attachments | No | β | Array of attachment hashes. See Notes. |
record | No | β | Record to link the email log to. Sets module_name, record_type, record_id and record_url. |
notify_on_failure | No | true | Send 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 }
)
Link buttonβ
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
filenameplusfile_content(base64) orurl_path. file_contentis the base64-encoded file:- From a path:
Base64.encode64(File.open(file_path).read) - From a URL:
Base64.encode64(URI.open(file_url).read)
- From a path:
Relatedβ
- Webhooks: open and click tracking
- Generators
- Configuration