Last active
November 5, 2015 09:11
-
-
Save awongCM/615b65a50c9448e56f17 to your computer and use it in GitHub Desktop.
SMTP Mail snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/smtp' | |
#Snippet 1: writing simple using your stmp gmail | |
message = <<MESSAGE_END | |
From: Private Person <someone@fromsomemail.com> | |
To: A Test User <someoneelse@tosomemail.com> | |
Subject: SMTP e-mail test | |
This is a test e-mail message. | |
MESSAGE_END | |
Net::SMTP.start('localhost') do |smtp| | |
smtp.send_message message, 'someone@fromsomemail.com', | |
'someoneelse@tosomemail.com' | |
end | |
#Snippet 2: Start SMTP server using server | |
Net::SMTP.start('stmp.gmail.com') | |
#Snippet 3: Start STMTP server using credentials | |
Net::SMTP.start('smtp.gmail.com', 25, 'localhost', 'username', 'password', :plain) | |
#OR | |
smtp_config_array2params = ['smtp.gmail.com', 25, 'localhost', 'username', 'password', :plain] | |
Net::SMTP.start(*smtp_config_params) | |
#OR | |
smtp_config_hash2params = {:smtp_domain=>'smtp.gmail.com', :port_no=>25, :host_name => 'localhost', :username =>'username', :password=>'password', :password_type=>:plain} | |
Net::SMTP.start(*smtp_config_hash2params.values) | |
#OR | |
@params = smtp_config_hash2params.collect{|k, v| v} | |
Net::SMTP.start(*@params) | |
#Snippet 4: Start sending email using HTML tags | |
require 'net/smtp' | |
message = <<MESSAGE_END | |
From: Private Person <me@fromdomain.com> | |
To: A Test User <test@todomain.com> | |
MIME-Version: 1.0 | |
Content-type: text/html | |
Subject: SMTP e-mail test | |
This is an e-mail message to be sent in HTML format | |
<b>This is HTML message.</b> | |
<h1>This is headline.</h1> | |
MESSAGE_END | |
Net::SMTP.start('localhost') do |smtp| | |
smtp.send_message message, 'me@fromdomain.com','test@todomain.com' | |
#Snippet 5: sending email with email attachments | |
require 'net/smtp' | |
filename = "/tmp/test.txt" | |
# Read a file and encode it into base64 format | |
filecontent = File.read(filename) | |
encodedcontent = [filecontent].pack("m") # base64 | |
marker = "AUNIQUEMARKER" | |
body =<<EOF | |
This is a test email to send an attachement. | |
EOF | |
# Define the main headers. | |
part1 =<<EOF | |
From: Private Person <me@fromdomain.net> | |
To: A Test User <test@todmain.com> | |
Subject: Sending Attachement | |
MIME-Version: 1.0 | |
Content-Type: multipart/mixed; boundary=#{marker} | |
--#{marker} | |
EOF | |
# Define the message action | |
part2 =<<EOF | |
Content-Type: text/plain | |
Content-Transfer-Encoding:8bit | |
#{body} | |
--#{marker} | |
EOF | |
# Define the attachment section | |
part3 =<<EOF | |
Content-Type: multipart/mixed; name=\"#{filename}\" | |
Content-Transfer-Encoding:base64 | |
Content-Disposition: attachment; filename="#{filename}" | |
#{encodedcontent} | |
--#{marker}-- | |
EOF | |
mailtext = part1 + part2 + part3 | |
# Let's put our code in safe area | |
begin | |
Net::SMTP.start('localhost') do |smtp| | |
smtp.sendmail(mailtext, 'awong.cm@gmail.com', | |
['awong.chmg@yahoo.com']) | |
end | |
rescue Exception => e | |
print "Exception occured: " + e | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment