|
How to send mail with Net::SMTP
require 'net/smtp'
myaccount = “myaccount@domain.tld”
smtp = Net::SMTP.new(mail.domain.tld)
smtp.start()
recipient = “name@domain.tld”
smtp.ready(myaccount, recipient) do |email|
email.write “Subject: saying hi\r\n”
email.write “\r\n”
email.write “Hi Name,\r\n”
email.write “\r\n”
email.write “I had a great time at the Sunday's picnic.\r\n”
email.write “\r\n”
email.write “Hope we can do this again soon.\r\n”
email.write “\r\n”
email.write “Cheers,\r\n”
email.write “\r\n”
email.write “Your new best friend\r\n”
end
The code is pretty self-explanatory, always remember to insert a blank line separating the header info, in our case the subject.
Read email with Net::POP3
require 'net/pop'
pop = Net::POP3.new(“mail.domain.tld”)
pop.start(“username”, “password”) do |pop|
content = pop.mails[0]
puts content.header.split(“\r\n”).grep(/^From: /)
puts “\nFull message:\n”
content.all($stdout)
end
|