Skip to main content

Ruby : Send email with excel attachment using net/smpt

require 'net/smtp'
require 'open-uri'


def send_email_with_attachment(subject, body, to_email)
 user = 'mailsender@gmail.com'
 pwd = 'Mail123Pa55word'
 to = to_email
 from = 'noreply@baliw.com'
 subject = "#{subject}"

filename = "Excel_Attachment.xlsx"
filename = "xlsTemplate.xls"
filename = "xlsxTemplate.xlsx"

# Read a file and encode it into base64 format
filecontent = File.binread(filename)
encodedcontent = [filecontent].pack("m")   # base64

marker = "AUNIQUEMARKER"

body = <<EOF
This is a test email to send an attachement using net/smpt.
EOF

# Define the main headers.
part1 = <<EOF
From: No Reply <#{from}>
To: A Test User <#{to}>
Subject: Sending Excel Attachement test123
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: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;  name = \"#{filename}\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="#{File.basename filename}"

#{encodedcontent}
--#{marker}--
EOF

message = part1 + part2 + part3
puts message
 
 smtp = Net::SMTP.new 'smtp.gmail.com', 587
 smtp.enable_starttls
 smtp.start('gmail.com', user, pwd, :login)
 smtp.send_message(message, from, to)
 smtp.finish
end

#send_email_with_attachment("test 123 subject w/ excel attachment", "test 123 body with excel", "baliw@gmail.com")

Comments

Popular posts from this blog

Alidropship plugins - Da Vince Theme Subscription Fixed using MailChimp

From MailChimp Code: Wrecked the design on the UI <!-- Begin MailChimp Signup Form --> <link href="// cdn-images.mailchimp.com/embedcode/slim-10_7.css " rel="stylesheet" type="text/css"> <style type="text/css"> #mc_embed_signup{background:#fff; clear:left; font:14px Helvetica,Arial,sans-serif;  width:450px;} /* Add your own MailChimp form style overrides in your site stylesheet or in this style block.    We recommend moving this block and the preceding CSS link to the HEAD of your HTML file. */ </style> <div id="mc_embed_signup"> <form action=" https://tester.us19.list-manage.com/subscribe/post?u=5b6e4f12343b19be8f09a2fef&amp;id=aeeef4383c " method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>     <div id="mc_embed_signup_scroll"> <label for...

Ubuntu 20.04 LTS | Installing OpenJDK 11 (LTS) JDK, JRE, HotSpot

https://adoptopenjdk.net/installation.html#linux-pkg ctrl + f => Linux RPM and DEB installer packages bboy@bboy-LE7450:~$ cat /etc/os-release | grep UBUNTU_CODENAME UBUNTU_CODENAME=focal bboy@bboy-LE7450:~$ sudo apt-get install wget apt-transport-https gnupg [sudo] password for bboy: Reading package lists... Done Building dependency tree       Reading state information... Done wget is already the newest version (1.20.3-1ubuntu1). wget set to manually installed. gnupg is already the newest version (2.2.19-3ubuntu2.1). gnupg set to manually installed. The following packages were automatically installed and are no longer required:   linux-headers-5.8.0-45-generic linux-hwe-5.8-headers-5.8.0-45   linux-image-5.8.0-45-generic linux-modules-5.8.0-45-generic   linux-modules-extra-5.8.0-45-generic Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed:   apt-transport-https 0 upgraded, 1 newly installed, 0 to remove and 21 not upgraded. Need to get 1,...