我需要通過Ruby發送帶有附件的電子郵件。在Ruby中發送帶附件的電子郵件
正在四處搜索,但還沒有找到任何簡單的腳本示例來執行此操作。
最近我發現是ActionMailer,但似乎需要一堆其他腳本才能運行。 (注意:我沒有使用Ruby on Rails)
我需要通過Ruby發送帶有附件的電子郵件。在Ruby中發送帶附件的電子郵件
正在四處搜索,但還沒有找到任何簡單的腳本示例來執行此操作。
最近我發現是ActionMailer,但似乎需要一堆其他腳本才能運行。 (注意:我沒有使用Ruby on Rails)
你檢出了Mail?這就是Rails 3中新的ActionMailer API的基礎。
「郵件是Ruby的互聯網庫,旨在處理電子郵件生成,解析和發送簡單,rubyesque方式。」
這裏來自文檔一個簡單的例子:
require 'mail'
@mail = Mail.new
file_data = File.read('path/to/myfile.pdf')
@mail.attachments['myfile.pdf'] = { :mime_type => 'application/x-pdf',
:content => file_data }
更新:或者更簡單地說:
@mail = Mail.new
@mail.add_file("/path/to/file.jpg")
完整的文檔:here
require 'mail'
Mail.deliver do
from "[email protected]"
to "[email protected]"
subject "Email with attachment"
body "Hello world"
add_file "/path/to/file"
end
發送電子郵件或電子郵件與一個通過「郵件」gem安裝,ny類型的附件變得更加簡單。
步驟:1安裝「郵件」寶石
步驟:2在紅寶石文件保持下面給出的語法:
require 'mail'
def mailsender
Mail.defaults do
delivery_method :smtp,{ address: "<smtp_address>",openssl_verify_mode: "none" }
end
Mail.deliver do
from 'from_mail_id'
to 'to_mail_id'
subject 'subject_to_be_sent'
# body File.read('body.txt')
body 'body.txt'
add_file '<file_location>/Word Doc.docx'
add_file '<file_location>/Word Doc.doc'
end
end
步驟:3現在只需撥打在步驟定義的方法。
這些傢伙明確表示不使用Rails – sergiovilar 2016-08-21 13:33:59
@sergiovilar:Mail是一個純粹的Ruby庫。它不依賴於Rails。因此這是一個有效的答案(即使在今天,答案發布6年後)。 – 2016-08-23 12:04:27
我得到這個錯誤:'未定義的方法'[]'爲零:NilClass' – mpora 2017-09-13 19:31:36