# app/mailers/application_mailer.rbclassApplicationMailer<ActionMailer::Basedefaultfrom:"from@example.com"layout'mailer'#寄信給多個收件者#default to: Proc.new { Admin.pluck(:email) }, from: 'notification@example.com'end#app/mailer/test_mailer.rbclassTestMailer<ApplicationMailer# Subject can be set in your I18n file at config/locales/en.yml# with the following lookup:## en.test_mailer.notify_job_apply.subject#defnotify_job_apply@greeting="Hi"#附件檔案attachments['filename.jpg']=File.read('/path/to/filename.jpg')mailto:"to@example.org"mailto:["email"],bcc:["sub-email"],subject:"Title"endend
信件內容
因為不是每個人都可以顯示 html 檔案,因此要有兩份
123456
#app/views/test_mailer/notify_job_apply.html.erb<h1>TestMailer#notify_job_apply</h1><p><%= @greeting %>, find me in app/views/test_mailer/notify_job_apply.html.erb</p><%=image_tagattachments['image.jpg'].url,alt:'My Photo',class:'photos'%>
#config/environments#忽略任何寄信錯誤config.action_mailer.raise_delivery_errors=false#測試用,不會真的寄信config.action_mailer.delivery_method=:test#用什麼方式傳遞config.action_mailer.delivery_method=:smtp#網站網址config.action_mailer.default_url_options={host:"http://localhost:3000"}#一定要轉 symbol 不然會吃不到config.action_mailer.smtp_settings=config_for(:email).symbolize_keys#使用 sidekiq 做背景處理config.active_job.queue_adapter=:sidekiq