• Latest
  • Trending
  • All
  • Business News
  • Startup Investments
  • Startup News
  • Programming
  • Software Architecture
  • Web Security
Sending Emails with Ruby – DZone Internet Dev

Sending Emails with Ruby – DZone Internet Dev

8 months ago
Lowering incident response time for OutSystems with AWS serverless know-how

Lowering incident response time for OutSystems with AWS serverless know-how

3 days ago
8 Knowledge Constructions That Energy Your Databases

8 Knowledge Constructions That Energy Your Databases

1 week ago
Let’s Architect! Architecting for governance and administration

Let’s Architect! Designing event-driven architectures

1 week ago
EP 42: Designing a chat utility

EP 42: Designing a chat utility

2 weeks ago
Textual content analytics on AWS: implementing an information lake structure with OpenSearch

Textual content analytics on AWS: implementing an information lake structure with OpenSearch

2 weeks ago
EP 41: What’s Kubernetes?

EP 41: What’s Kubernetes?

3 weeks ago
Streaming the AWS Wickr desktop consumer with Amazon AppStream 2.0

Streaming the AWS Wickr desktop consumer with Amazon AppStream 2.0

3 weeks ago
EP 40: Git workflow – by Alex Xu

EP 40: Git workflow – by Alex Xu

4 weeks ago
Genomics workflows, Half 4: processing archival information

Genomics workflows, Half 4: processing archival information

1 month ago
EP 39: Accounting 101 in Fee Techniques

EP 39: Accounting 101 in Fee Techniques

1 month ago
Prime 10 AWS Structure Weblog posts of 2022

Prime 10 AWS Structure Weblog posts of 2022

1 month ago
Deploying Oracle RAC in AWS Outposts by way of FlashGrid Cluster

Deploying Oracle RAC in AWS Outposts by way of FlashGrid Cluster

1 month ago
  • Home
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms & Conditions
Sunday, February 5, 2023
  • Login
Startup News
  • Home
  • Startups
    • All
    • Business News
    • Startup Investments
    • Startup News
    Market analysis startup Bolt Perception receives funding from 212 — Retail Know-how Innovation Hub

    Market analysis startup Bolt Perception receives funding from 212 — Retail Know-how Innovation Hub

    [Funding alert] Fintech startup FinBox raises $15M in Sequence A spherical led by A91 Companions

    [Funding alert] Fintech startup FinBox raises $15M in Sequence A spherical led by A91 Companions

    NRMA backs VC’s $50 million agritech fund

    NRMA backs VC’s $50 million agritech fund

    Fanclash funding: Esports fantasy startup FanClash raises $40 million Collection B spherical

    Fanclash funding: Esports fantasy startup FanClash raises $40 million Collection B spherical

    Turkish enterprise capital fund ‘hunts’ for seed-stage startups

    Turkish enterprise capital fund ‘hunts’ for seed-stage startups

    The rise of API-first corporations, in fintech and past – TechCrunch

    The rise of API-first corporations, in fintech and past – TechCrunch

    QSTP-funded startup brings digital actuality to life

    QSTP-funded startup brings digital actuality to life

    Payglocal Funding: Cross-border funds startup PayGlocal raises $12 million from Tiger International, Sequoia

    Payglocal Funding: Cross-border funds startup PayGlocal raises $12 million from Tiger International, Sequoia

    [Funding alert] Fintech startup PayGlocal raises $12M from Tiger World, Sequoia, BEENEXT

    [Funding alert] Fintech startup PayGlocal raises $12M from Tiger World, Sequoia, BEENEXT

    With $110M in new funds, Aidoc is branching out of radiology

    With $110M in new funds, Aidoc is branching out of radiology

    Trending Tags

    • startup advice
    • startup funding
    • startup
    • funding
    • fund
    • Tips
  • Software & Development
    • All
    • Programming
    • Software Architecture
    • Web Security
    Lowering incident response time for OutSystems with AWS serverless know-how

    Lowering incident response time for OutSystems with AWS serverless know-how

    8 Knowledge Constructions That Energy Your Databases

    8 Knowledge Constructions That Energy Your Databases

    Let’s Architect! Architecting for governance and administration

    Let’s Architect! Designing event-driven architectures

    EP 42: Designing a chat utility

    EP 42: Designing a chat utility

    Textual content analytics on AWS: implementing an information lake structure with OpenSearch

    Textual content analytics on AWS: implementing an information lake structure with OpenSearch

    EP 41: What’s Kubernetes?

    EP 41: What’s Kubernetes?

    Streaming the AWS Wickr desktop consumer with Amazon AppStream 2.0

    Streaming the AWS Wickr desktop consumer with Amazon AppStream 2.0

    EP 40: Git workflow – by Alex Xu

    EP 40: Git workflow – by Alex Xu

    Genomics workflows, Half 4: processing archival information

    Genomics workflows, Half 4: processing archival information

    EP 39: Accounting 101 in Fee Techniques

    EP 39: Accounting 101 in Fee Techniques

    Trending Tags

    • Java
    • Microsoft
    • employee wellness programs
    • Project
    • Dev
    • Hackers
    • Security
  • Contact Us
No Result
View All Result
Startup News
Home Software & Development Programming

Sending Emails with Ruby – DZone Internet Dev

by Startupnews Writer
June 14, 2022
in Programming
0
Sending Emails with Ruby – DZone Internet Dev
491
SHARES
1.4k
VIEWS
Share on FacebookShare on Twitter


Let’s say you’ve a working Ruby app and wish so as to add an e mail supply performance to it. This could possibly be associated to person authentication, or every other sort of transactional e mail, it makes no distinction. This tutorial is tailor-made is geared toward serving to you implement sending emails with Ruby.

Choices For Sending an Electronic mail in Ruby

Principally, you’ll be able to decide one of many three choices. 

The best one is utilizing Internet::SMTP class. It gives the performance to ship e mail by way of SMTP. The downside of this feature is that Internet::SMTP lacks features to compose emails. You’ll be able to all the time create them your self, however this takes time.

The second choice is to make use of a devoted Ruby gem like Mail, Pony, or others. These options allow you to deal with e mail actions in a easy and efficient manner. Motion Mailer is an ideal e mail resolution by the prism of Rails. And, almost certainly, this shall be your selection. 

The third choice is class Socket. Principally, this class permits you to set communication between processes or inside a course of. So, e mail sending could be applied with it as effectively. Nonetheless, the reality is that Socket doesn’t give you intensive functionalities, and also you’re unlikely to need to go together with it. 

Now, let’s attempt to ship an e mail utilizing every of the described options. 

The right way to Ship Emails in Ruby by way of Internet::SMTP

From my expertise, using that choice in a daily net app is rare. Nonetheless, sending emails by way of Internet::SMTP could possibly be a match when you use mruby (a light-weight implementation of the Ruby language) on some IoT gadgets. Additionally, it’ll do if utilized in serverless computing, for instance, AWS Lambda. Take a look at this script instance first after which we’ll undergo it intimately.

require 'internet/smtp'
message = <<END_OF_MESSAGE
From: YourRubyApp <data@yourrubyapp.com>
To: BestUserEver <your@bestuserever.com>

Topic: Any e mail topic you need

Date: Tue, 02 Jul 2019 15:00:34 +0800

Lorem Ipsum

END_OF_MESSAGE

Internet::SMTP.begin('your.smtp.server', 25) do |smtp|

  smtp.send_message message,

  'data@yourrubyapp.com',

  'your@bestuserever.com'

finish

It is a easy instance of sending a textual e mail by way of SMTP (official documentation could be discovered right here. You’ll be able to see 4 headers: From, To, Topic, and Date. Understand that you must separate them with a clean line from the e-mail physique textual content. Equally vital is to hook up with the SMTP server. 

Internet::SMTP.begin('your.smtp.server', 25) do |smtp| 

Naturally, right here will seem your information as an alternative of ‘your.smtp.server‘, and 25 is a default port quantity. If wanted, you’ll be able to specify different particulars like username, password, or authentication scheme (:plain, :login, and :cram_md5). It could look as follows:

`Internet::SMTP.begin('your.smtp.server', 25, ‘localhost’, ‘username’, ‘password’ :plain) do |smtp|`

Right here, you’ll connect with the SMTP server utilizing a username and password in plain textual content format, and the shopper’s hostname shall be recognized as localhost.

After that, you need to use the send_message technique and specify the addresses of the sender and the recipient as parameters. The block type of SMTP.begin (`Internet::SMTP.begin('your.smtp.server', 25) do |smtp|`) closes the SMTP session routinely.

Within the Ruby Cookbook, sending emails with the Internet::SMTP library is known as minimalism since you must construct the e-mail string manually. Nonetheless, it’s not as hopeless as you could assume. Let’s see how one can improve your e mail with HTML content material and even add an attachment.

Sending an HTML Electronic mail in Internet::SMTP

Take a look at this script instance that refers back to the message part.

message = <<END_OF_MESSAGE
From: YourRubyApp <data@yourrubyapp.com>
To: BestUserEver <your@bestuserever.com>
MIME-Model: 1.0
Content material-type: textual content/html

Topic: Any e mail topic you need

Date: Tue, 02 Jul 2019 15:00:34 +0800

A little bit of plain textual content.

<robust>The start of your HTML content material.</robust>

<h1>And a few headline, as effectively.</h1>

END_OF_MESSAGE

Aside from HTML tags within the message physique, we’ve received two extra headers: MIME-Model and Content material-type.  MIME refers to Multipurpose Web Mail Extensions. It’s an extension of the Web e mail protocol that permits you to mix totally different content material sorts in a single message physique. The worth  MIME-Model is often 1.0. It signifies {that a} message is MIME-formatted. 

As for the Content material-type header, every little thing is obvious. In our case, we now have two sorts – HTML and plain textual content. Additionally, ensure to separate these content material sorts utilizing defining boundaries.

 Aside from MIME-Model and Content material-type, you need to use different MIME headers:

– Content material-Disposition – specifies the presentation model (inline or attachment)

– Content material-Switch-Encoding – signifies a binary-to-text encoding scheme (7bit, quoted-printable, base64, 8bit, or binary).

Sending an Electronic mail with an Attachment in Internet::SMTP

Let’s add an attachment, similar to a PDF file. On this case, we have to replace Content material-type to multipart/blended. Additionally, use the pack("m") operate to encode the hooked up file with base64 encoding.

require 'internet/smtp'
filename = "/tmp/Attachment.pdf"
file_content = File.learn(filename)
encoded_content = [file_content].pack("m")   # base64
marker = "AUNIQUEMARKER"

After that, you could outline three components of your e mail.

Half 1 – Essential Headers

part1 = <<END_OF_MESSAGE
From: YourRubyApp <data@yourrubyapp.com>
To: BestUserEver <your@bestuserever.com>
Topic: Including attachment to e mail
MIME-Model: 1.0
Content material-Kind: multipart/blended; boundary = #{marker}
--#{marker}
END_OF_MESSAGE

Half 2 – Message Motion

part2 = <<END_OF_MESSAGE
Content material-Kind: textual content/html
Content material-Switch-Encoding:8bit
A little bit of plain textual content.
<robust>The start of your HTML content material.</robust>
<h1>And a few headline, as effectively.</h1>
--#{marker}
END_OF_MESSAGE

Half 3 – Attachment

part3 = <<END_OF_MESSAGE
Content material-Kind: multipart/blended; title = "#{filename}"
Content material-Switch-Encoding:base64
Content material-Disposition: attachment; filename = "#{filename}"
#{encoded_content}
--#{marker}--
END_OF_MESSAGE

Now, we will put all of the components collectively and finalize the script. That’s the way it will look:

require 'internet/smtp'
filename = "/tmp/Attachment.pdf"
file_content = File.learn(filename)
encoded_content = [file_content].pack("m")   # base64
marker = "AUNIQUEMARKER"
part1 = <<END_OF_MESSAGE
From: YourRubyApp <data@yourrubyapp.com>
To: BestUserEver <your@bestuserever.com>
Topic: Including attachment to e mail
MIME-Model: 1.0
Content material-Kind: multipart/blended; boundary = #{marker}
--#{marker}
END_OF_MESSAGE

part2 = <<END_OF_MESSAGE
Content material-Kind: textual content/html
Content material-Switch-Encoding:8bit
A little bit of plain textual content.

<robust>The start of your HTML content material.</robust>
<h1>And a few headline, as effectively.</h1>
--#{marker}
END_OF_MESSAGE

part3 = <<END_OF_MESSAGE
Content material-Kind: multipart/blended; title = "#{filename}"
Content material-Switch-Encoding:base64
Content material-Disposition: attachment; filename = "#{filename}"
#{encoded_content}
--#{marker}--
END_OF_MESSAGE

message = part1 + part2 + part3
start
  Internet::SMTP.begin('your.smtp.server', 25) do |smtp|
    smtp.send_message message,
    'data@yourrubyapp.com',
    'your@bestuserever.com'
  finish

Can I Ship an Electronic mail to A number of Recipients in Internet::SMTP?

Undoubtedly, you’ll be able to. send_messageexpects second and subsequent arguments to include recipients’ emails. For instance, this:

Internet::SMTP.begin('your.smtp.server', 25) do |smtp|
  smtp.send_message message,
  'data@yourrubyapp.com',
  'your@bestuserever1.com',
  ‘your@bestuserever2.com’,
  ‘your@bestuserever3.com
finish

Greatest Ruby Gems for Sending Emails

Within the Ruby ecosystem, you could find particular e mail gems that may enhance your e mail sending expertise.

Ruby Mail

This library is geared toward giving a single level of entry to handle all email-related actions together with sending and receiving emails.

Pony

You might need heard a fairy story about sending an e mail in a single command. Maintain on to your hats, ‘trigger it’s actual and supplied by Pony gem. 

ActionMailer

That is the preferred gem for sending emails on Rails. In case your app is written on prime of it, ActionMailer will definitely come up. It helps you to ship emails utilizing mailer courses and views. 

Utilizing Mailtrap to Check Electronic mail Sending with Internet::SMTP

The setup may be very easy. When you’re in your demo inbox, copy the SMTP credentials on the SMTP Settings tab and insert them into your code. Or you will get a ready-to-use template of a easy message within the Integrations part. Simply select a programming language or framework your app is constructed with. 

require 'internet/smtp'
message = <<END_OF_MESSAGE
From: YourRubyApp <data@yourrubyapp.com>
To: BestUserEver <your@bestuserever.com>
Topic: Any e mail topic you need
Date: Tue, 02 Jul 2019 15:00:34 +0800
Lorem Ipsum
END_OF_MESSAGE

Internet::SMTP.begin('smtp.mailtrap.io', 587, '<username>', '<password>', :cram_md5) do |smtp|
  smtp.send_message message,
  'data@yourrubyapp.com',
  'your@bestuserever.com'
finish

If every little thing is alright, you’ll see your message within the Mailtrap Demo inbox. Additionally, you’ll be able to attempt to test your e mail with HTML content material and an attachment. 

You could have simply learn the complete tutorial on how one can check and ship emails in Ruby. Hope you loved it!



Source_link

Related

Tags: DevDZoneEmailsRubySendingWeb
Share196Tweet123
Startupnews Writer

Startupnews Writer

We write full-time and bring you the best news for startups and enterprises. We are passionate about tech entrepreneurship & innovation. Here you will find also web security news and software architecture standards for your next project.

  • Trending
  • Comments
  • Latest
Why is RESTful API so widespread?

Why is RESTful API so widespread?

August 25, 2022
What do WhatsApp, Discord, and Fb Messenger have in frequent? (Episode 10)

What do WhatsApp, Discord, and Fb Messenger have in frequent? (Episode 10)

June 6, 2022
These local weather startups are nonetheless elevating cash regardless of Putin, inflation, markets – 24/7 Wall St.

These local weather startups are nonetheless elevating cash regardless of Putin, inflation, markets – 24/7 Wall St.

June 5, 2022
Acquisitions and investments within the funds trade: challenges and alternatives

A Standardized, Specification-Pushed API Lifecycle

June 5, 2022

Telematics Options Market Measurement to Surpass US$ 142.93

0
Acquisitions and investments within the funds trade: challenges and alternatives

Acquisitions and investments within the funds trade: challenges and alternatives

0
With Market Measurement Valued at $1.4 Billion by 2026, it`s a Wholesome Outlook for the World MEMS Oscillators Market

With Market Measurement Valued at $1.4 Billion by 2026, it`s a Wholesome Outlook for the World MEMS Oscillators Market

0
How Ukrainian startups are surviving the battle with Russia

How Ukrainian startups are surviving the battle with Russia

0
Lowering incident response time for OutSystems with AWS serverless know-how

Lowering incident response time for OutSystems with AWS serverless know-how

February 2, 2023
8 Knowledge Constructions That Energy Your Databases

8 Knowledge Constructions That Energy Your Databases

January 28, 2023
Let’s Architect! Architecting for governance and administration

Let’s Architect! Designing event-driven architectures

January 26, 2023
EP 42: Designing a chat utility

EP 42: Designing a chat utility

January 21, 2023
  • Home
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms & Conditions

Copyright © 2022.

No Result
View All Result
  • Home
  • Startups
  • Software & Development
  • Contact Us

Copyright © 2022.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
What Are Cookies
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT
Translate »