<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: AlexandraMT</title>
    <description>The latest articles on DEV Community by AlexandraMT (@alexandramt).</description>
    <link>https://dev.to/alexandramt</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F836623%2Fbbdae271-34d7-43ea-abce-9463ca0b5abd.png</url>
      <title>DEV Community: AlexandraMT</title>
      <link>https://dev.to/alexandramt</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alexandramt"/>
    <language>en</language>
    <item>
      <title>How to Send Emails in Flask</title>
      <dc:creator>AlexandraMT</dc:creator>
      <pubDate>Mon, 11 Apr 2022 14:09:47 +0000</pubDate>
      <link>https://dev.to/alexandramt/how-to-send-emails-in-flask-53k6</link>
      <guid>https://dev.to/alexandramt/how-to-send-emails-in-flask-53k6</guid>
      <description>&lt;p&gt;Flask is a popular Python web framework and the preferred choice for many web developers. It’s often referred to as a microframework because of its limited capabilities and the general minimalist approach to the development in Python. As such, it also doesn’t offer a native solution for sending emails, but more than makes up for it with an excellent Flask-Mail extension.&lt;/p&gt;

&lt;p&gt;In this article, we’ll explain how to configure and send emails with Flask-Mail.To get started, we’ll need to take care of a few brief installs, traditionally done with a pip. If you don’t have Flask installed yet - check out the full article &lt;a href="https://mailtrap.io/blog/flask-email-sending/"&gt;How to Send Emails in Flask&lt;/a&gt; at &lt;a href="http://mailtrap.io"&gt;Mailtrap&lt;/a&gt; blog.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sending emails in Flask
&lt;/h2&gt;

&lt;p&gt;Email sending in Flask-Mail is handled by an instance of a Mail class.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;from flask import Flask&lt;br&gt;
from flask_mail import Mail&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;app = Flask(app_name) # pick the name&lt;br&gt;
mail = Mail(app)&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
We’ll need to set up a Message object, mapped by the URL rule (‘/’), and insert the base details of our message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@app.route("/")
def index():
  msg = Message('Hello from the other side!', sender =   'alexandra@mailtrap.io', recipients = ['paul@mailtrap.io'])
  msg.body = "Hey Paul, sending you this email from my Flask app, lmk if it works"
  mail.send(msg)
  return "Message sent!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All in all, the entire code will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask
from flask_mail import Mail

app = Flask(app_name)

app.config['MAIL_SERVER']='smtp.mailtrap.io'
app.config['MAIL_PORT'] = 2525
app.config['MAIL_USERNAME'] = '97e041d5e367c7'
app.config['MAIL_PASSWORD'] = 'cfaf5b99f8bafb'
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USE_SSL'] = False
mail = Mail(app)

@app.route("/")
def index():
  msg = Message('Hello from the other side!', sender =   'alexandra@mailtrap.io', recipients = ['paul@mailtrap.io'])
  msg.body = "Hey Paul, sending you this email from my Flask app, lmk if it works"
  mail.send(msg)
  return "Message sent!"

if __name__ == '__main__':
   app.run(debug = True)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it in Python Shell, open &lt;a href="http://localhost:5000/"&gt;http://localhost:5000/&lt;/a&gt;, and check whether the email arrived in your inbox. It certainly arrived in ours!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Customizing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;MAIL_DEFAULT_SENDER from the configuration is, by default, set to none and we skipped it in our setup. However, if you want to keep sending from the same address, it makes sense to specify it there. &lt;br&gt;
A good idea is to also specify the display name for a sender:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;msg = Message('Hello from the other side!', sender =  ("Alexandra from Mailtrap", 'peter@mailtrap.io')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can also specify a HTML version of the message. It can be sent along with a body message or without it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;msg.body = "Hey Paul, sending you this email from my Flask app, lmk if it works"&lt;br&gt;
msg.html = "&amp;lt;b&amp;gt;Hey Paul&amp;lt;/b&amp;gt;, sending you this email from my &amp;lt;a href="https://google.com"&amp;gt;Flask app&amp;lt;/a&amp;gt;, lmk if it works"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You may also add someone in cc and/or bcc, set a reply-to address, add extra headers, and so on. Here’s the full list of parameters available:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;flask-mail.Message(subject, recipients, body, html, sender, cc, bcc, reply-to, date, charset, extra_headers, mail_options, rcpt_options)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adding an attachment&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Flask-Mail also provides an easy way to attach an attachment to our message. We need to load it with open_resource() method and then use a Python “with” statement to add a file to our email. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;with app.open_resource("invoice.pdf") as fp:  &lt;br&gt;
msg.attach("invoice.pdf", "application/pdf", fp.read())&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Make sure you pick a proper &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types"&gt;MIME Type&lt;/a&gt; for each file and that each is uploaded to the same directory as your script.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sending bulk messages&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Most often, the Flask-Mail example above will be sufficient, but there are situations when you need to send dozens or even hundreds of emails for each request. Think about various cron jobs, for example.&lt;/p&gt;

&lt;p&gt;For that, we can use a Python “with” statement. The connection to our email will be kept alive until all emails have been sent (at which point it will close automatically). If you wish to specify the maximum number of emails to be sent, use &lt;em&gt;MAIL_MAX_EMAILS&lt;/em&gt; from the configuration (by default, no limit is set).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;with mail.connect() as conn:
    for user in users:
        message = '...'
        subject = "Hello from the other side!"
        msg = Message(recipients=[user.email],
                      body=message,
                      subject=subject)

        conn.send(msg)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Other options&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Flask-Mail also provides many more options for you to quickly configure an email, add the headers, body, and a number of other parameters. It’s concise and very easy to grasp. &lt;/p&gt;

&lt;p&gt;Check the &lt;a href="https://pythonhosted.org/Flask-Mail/#api"&gt;official documentation&lt;/a&gt; here to see them all.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sending emails asynchronously
&lt;/h2&gt;

&lt;p&gt;An important aspect to consider when setting up emails is the possibility of sending them asynchronously. &lt;/p&gt;

&lt;p&gt;Let’s look at a typical situation. A user enters your site with the intention of sending you an email. They fill out a form, hit a ‘send’ button, and wait. &lt;/p&gt;

&lt;p&gt;In the background, a template is put together and an attempt to reach an ESP (email sending provider) is initiated. Most of the time, it responds within a few milliseconds and sends an email while the user is redirected to some “thank you” page.&lt;/p&gt;

&lt;p&gt;The problems pile up if the server isn’t very responsive at the moment and it takes seconds rather than milliseconds to get a response. At times, the connection may even time out. Add to this the fact that multiple users could be attempting to perform this or another request at the same time, effectively clogging up the server. If your app crashes because of that, no emails will be sent either.&lt;/p&gt;

&lt;p&gt;Now, this is often a hypothetical scenario. Reputable ESPs earned their reputations because of their reliability. Ping Postmark or Sendgrid and you’ll probably never have to wait more than 100-200ms for a response. Send them dozens of emails at once and they’ll handle them with ease.&lt;/p&gt;

&lt;p&gt;As a matter of fact, most, if not all, of the popular ESPs send emails async anyway. It’s because of the underlying verifications each of them runs in the background, in attempts to protect their &lt;a href="https://mailtrap.io/blog/email-sender-reputation/"&gt;sender reputation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;None of these things change the fact that your app still connects to an external service every time an email is sent. That’s why you may want to consider adding an async capability to your app.&lt;br&gt;
Learn how to send an email with Celery and Flask-Mail in the full article on &lt;a href="https://mailtrap.io/blog/flask-email-sending/"&gt;Email Sending with Flask&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Testing emails in Flask
&lt;/h2&gt;

&lt;p&gt;Before deploying any email functionality, you certainly need a way to test whether emails are actually sent without spamming users. &lt;/p&gt;

&lt;p&gt;You also may want to see some of those emails because chances are there will be a thing or two to improve.&lt;/p&gt;

&lt;p&gt;The first part can be easily handled with simple Flask settings that will block sending. It can be done in two ways, with the identical outcome:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set &lt;em&gt;MAIL_SUPPRESS_SEND&lt;/em&gt; from the earlier configuration to &lt;em&gt;False&lt;/em&gt;, or&lt;/li&gt;
&lt;li&gt;Add a new setting to the configuration file – &lt;em&gt;TESTING&lt;/em&gt; – and set it to &lt;em&gt;True&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then, you can use the record_messages method to see what’s being sent from your Flask app (make sure you have &lt;a href="https://pypi.org/project/blinker/"&gt;blinker package&lt;/a&gt; installed). You’ll see the list of Message instances under outbox.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;with mail.record_messages() as outbox:

    mail.send_message(subject='testing',
                      body='test',
                      recipients=emails)

    assert len(outbox) == 1
    assert outbox[0].subject == "testing"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If, however, you wish to see the emails that your app sends, you’ll need a different solution. Earlier in the text, we showed that Mailtrap can be easily plugged into a Flask app. It works as a fake SMTP, intercepting your outgoing emails and putting them into virtual inboxes.&lt;/p&gt;

&lt;p&gt;Each email is visible in your Mailtrap dashboard. You can preview how it will look like on different screens. You may validate the headers and check the support for its HTML/CSS. &lt;/p&gt;

&lt;p&gt;Among other features, there’s a spam score, blacklists, bcc tracking, email forwarding, and multiple inboxes. It’s certainly a more comfortable method of testing than the one above, and you get to test a lot more than just sending.&lt;/p&gt;

&lt;p&gt;Test it on your own! You can get started for free with &lt;a href="https://mailtrap.io/register/signup"&gt;Mailtrap&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Sending emails is a vital part of many Flask applications. When users create an account, a confirmation email should hit their inboxes right away. When they forget their password, a handy mechanism for resetting it built into an app will make their life a lot easier. &lt;/p&gt;

&lt;p&gt;There are tons of other situations when an auto-generated email is a must. Luckily, it’s really easy to set things up and send the first emails within minutes. Learn more about Flask-Mail and other options in our full article - &lt;a href="https://mailtrap.io/blog/flask-email-sending/"&gt;Sending Emails with Flask - Step-by-Step Flask-Mail Guide&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Sending Emails with Ruby</title>
      <dc:creator>AlexandraMT</dc:creator>
      <pubDate>Mon, 11 Apr 2022 13:50:23 +0000</pubDate>
      <link>https://dev.to/alexandramt/sending-emails-with-ruby-3co1</link>
      <guid>https://dev.to/alexandramt/sending-emails-with-ruby-3co1</guid>
      <description>&lt;p&gt;Let’s say you have a working Ruby app and need to add an email delivery functionality to it. This could be related to user authentication, or any other kind of transactional emails, it makes no difference. This tutorial is tailored is aimed at  helping you implement sending emails with Ruby.&lt;/p&gt;

&lt;p&gt;If you want to read the  full article, check it out the on the Mailtrap’s blog:&lt;a href="https://mailtrap.io/blog/ruby-send-email/#Best-Ruby-gems-for-sending-emails"&gt; Sending Emails with Ruby&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Options for  sending an email in Ruby
&lt;/h2&gt;

&lt;p&gt;Mostly, you can pick one of the three options. &lt;/p&gt;

&lt;p&gt;The simplest one is using &lt;strong&gt;Net::SMTP class&lt;/strong&gt;. It provides the functionality to send email via SMTP. The drawback of this option is that Net::SMTP lacks functions to compose emails. You can always create them yourself, but this takes time.&lt;/p&gt;

&lt;p&gt;The second option is to use a dedicated &lt;strong&gt;Ruby gem&lt;/strong&gt; like Mail, Pony, or others. These solutions let you handle email activities in a simple and effective way. Action Mailer is a perfect email solution through the prism of Rails. And, most likely, this will be your choice. &lt;/p&gt;

&lt;p&gt;The third option is class &lt;strong&gt;Socket&lt;/strong&gt;. Mostly, this class allows you to set communication between processes or within a process. So, email sending can be implemented with it as well. However, the truth is that Socket does not provide you with extensive functionalities, and you’re unlikely to want to go with it. &lt;/p&gt;

&lt;p&gt;Now, let’s try to send an email using each of the described solutions. &lt;/p&gt;

&lt;h2&gt;
  
  
  How to send emails in Ruby via Net::SMTP
&lt;/h2&gt;

&lt;p&gt;From our experience, the use of that option in a regular web app is uncommon. However, sending emails via Net::SMTP could be a fit if you use &lt;a href="https://github.com/mruby/mruby"&gt;mruby&lt;/a&gt; (a lightweight implementation of the Ruby language) on some IoT device. Also, it will do if used in serverless computing, for example, &lt;a href="https://aws.amazon.com/ru/lambda/"&gt;AWS Lambda&lt;/a&gt;. Check out this script example first and then we’ll go through it in detail.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require 'net/smtp'

message = &amp;lt;&amp;lt;END_OF_MESSAGE
From: YourRubyApp &amp;lt;info@yourrubyapp.com&amp;gt;
To: BestUserEver &amp;lt;your@bestuserever.com&amp;gt;
Subject: Any email subject you want
Date: Tue, 02 Jul 2019 15:00:34 +0800

Lorem Ipsum
END_OF_MESSAGE
Net::SMTP.start('your.smtp.server', 25) do |smtp|
  smtp.send_message message,
  'info@yourrubyapp.com',
  'your@bestuserever.com'
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a simple example of sending a textual email via SMTP (official documentation can be found &lt;a href="https://docs.ruby-lang.org/en/2.4.0/Net/SMTP.html"&gt;here&lt;/a&gt;). You can see four headers: From, To, Subject, and Date. Keep in mind that you have to separate them with a blank line from the email body text. Equally important is to connect to the SMTP server. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;Net::SMTP.start('your.smtp.server', 25) do |smtp|&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Naturally, here will appear your data instead of &lt;code&gt;‘your.smtp.server‘&lt;/code&gt;, and 25 is a default port number. If needed, you can specify other details like username, password, or authentication scheme (:plain, :login, and :cram_md5). It may look as follows:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Net::SMTP.start('your.smtp.server', 25, ‘localhost’, ‘username’, ‘password’ :plain) do |smtp|&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here, you will connect to the SMTP server using a username and password in plain text format, and the client’s hostname will be identified as localhost.&lt;/p&gt;

&lt;p&gt;After that, you can use the &lt;code&gt;send_message&lt;/code&gt; method and specify the addresses of the sender and the recipient as parameters. The block form of SMTP.start (&lt;code&gt;Net::SMTP.start('your.smtp.server', 25) do |smtp|&lt;/code&gt;) closes the SMTP session automatically.&lt;/p&gt;

&lt;p&gt;In the Ruby Cookbook, sending emails with the Net::SMTP library is referred to as minimalism since you have to build the email string manually. Nevertheless, it’s not as hopeless as you may think of. Let’s see how you can enhance your email with HTML content and even add an attachment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sending an HTML email in Net::SMTP&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Check out this script example that refers to the message section.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;message = &amp;lt;&amp;lt;END_OF_MESSAGE
From: YourRubyApp &amp;lt;info@yourrubyapp.com&amp;gt;
To: BestUserEver &amp;lt;your@bestuserever.com&amp;gt;
MIME-Version: 1.0
Content-type: text/html
Subject: Any email subject you want
Date: Tue, 02 Jul 2019 15:00:34 +0800

A bit of plain text.

&amp;lt;strong&amp;gt;The beginning of your HTML content.&amp;lt;/strong&amp;gt;
&amp;lt;h1&amp;gt;And some headline, as well.&amp;lt;/h1&amp;gt;
END_OF_MESSAGE

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Apart from HTML tags in the message body, we’ve got two additional headers: &lt;code&gt;MIME-Version&lt;/code&gt; and &lt;code&gt;Content-type&lt;/code&gt;.  MIME refers to Multipurpose Internet Mail Extensions. It is an extension to Internet email protocol that allows you to combine different content types in a single message body. The value of &lt;code&gt;MIME-Version&lt;/code&gt; is typically 1.0. It indicates that a message is MIME-formatted. &lt;/p&gt;

&lt;p&gt;As for the &lt;code&gt;Content-type&lt;/code&gt; header, everything is clear. In our case, we have two types – HTML and plain text. Also, make sure to separate these content types using defining boundaries.&lt;/p&gt;

&lt;p&gt;Except for MIME-Version and Content-type, you can use other MIME headers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Content-Disposition&lt;/code&gt; – specifies the presentation style (inline or attachment)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Content-Transfer-Encoding&lt;/code&gt; – indicates a binary-to-text encoding scheme (7bit, quoted-printable, base64, 8bit, or binary).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Sending an email with an attachment in Net::SMTP&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s add an attachment, such as a PDF file. In this case, we need to update &lt;code&gt;Content-type&lt;/code&gt; to multipart/mixed. Also, use the &lt;code&gt;pack("m")&lt;/code&gt; function to encode the attached file with base64 encoding.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require 'net/smtp'

filename = "/tmp/Attachment.pdf"
file_content = File.read(filename)
encoded_content = [file_content].pack("m")   # base64

marker = "AUNIQUEMARKER"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, you need to define three parts of your email.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Part 1 – Main headers&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;part1 = &amp;lt;&amp;lt;END_OF_MESSAGE
From: YourRubyApp &amp;lt;info@yourrubyapp.com&amp;gt;
To: BestUserEver &amp;lt;your@bestuserever.com&amp;gt;
Subject: Adding attachment to email
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary = #{marker}
--#{marker}
END_OF_MESSAGE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;strong&gt;Part 2 – Message action&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;part2 = &amp;lt;&amp;lt;END_OF_MESSAGE
Content-Type: text/html
Content-Transfer-Encoding:8bit

A bit of plain text.

&amp;lt;strong&amp;gt;The beginning of your HTML content.&amp;lt;/strong&amp;gt;
&amp;lt;h1&amp;gt;And some headline, as well.&amp;lt;/h1&amp;gt;
--#{marker}
END_OF_MESSAGE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;strong&gt;Part 3 – Attachment&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;part3 = &amp;lt;&amp;lt;END_OF_MESSAGE
Content-Type: multipart/mixed; name = "#{filename}"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename = "#{filename}"

#{encoded_content}
--#{marker}--
END_OF_MESSAGE
Now, we can put all the parts together and finalize the script. That’s how it will look:
require 'net/smtp'

filename = "/tmp/Attachment.pdf"
file_content = File.read(filename)
encoded_content = [file_content].pack("m")   # base64

marker = "AUNIQUEMARKER"

part1 = &amp;lt;&amp;lt;END_OF_MESSAGE
From: YourRubyApp &amp;lt;info@yourrubyapp.com&amp;gt;
To: BestUserEver &amp;lt;your@bestuserever.com&amp;gt;
Subject: Adding attachment to email
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary = #{marker}
--#{marker}
END_OF_MESSAGE

part2 = &amp;lt;&amp;lt;END_OF_MESSAGE
Content-Type: text/html
Content-Transfer-Encoding:8bit

A bit of plain text.

&amp;lt;strong&amp;gt;The beginning of your HTML content.&amp;lt;/strong&amp;gt;
&amp;lt;h1&amp;gt;And some headline, as well.&amp;lt;/h1&amp;gt;
--#{marker}
END_OF_MESSAGE

part3 = &amp;lt;&amp;lt;END_OF_MESSAGE
Content-Type: multipart/mixed; name = "#{filename}"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename = "#{filename}"

#{encoded_content}
--#{marker}--
END_OF_MESSAGE

message = part1 + part2 + part3

begin
  Net::SMTP.start('your.smtp.server', 25) do |smtp|
    smtp.send_message message,
    'info@yourrubyapp.com',
    'your@bestuserever.com'
  end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Can I send an email to multiple recipients in Net::SMTP?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Definitely, you can. &lt;code&gt;send_message&lt;/code&gt; expects second and subsequent arguments to contain recipients’ emails. For example, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Net::SMTP.start('your.smtp.server', 25) do |smtp|
  smtp.send_message message,
  'info@yourrubyapp.com',
  'your@bestuserever1.com',
  ‘your@bestuserever2.com’,
  ‘your@bestuserever3.com
end

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Best Ruby gems for sending emails
&lt;/h2&gt;

&lt;p&gt;In Ruby ecosystem, you can find specific email gems that can improve your email sending experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ruby Mail&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;This library is aimed at giving a single point of access to manage all email-related activities including sending and receiving email.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pony&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You might have heard a fairy tale about sending an email in one command. Hold on to your hats, cause it’s real and provided by &lt;a href="https://github.com/adamwiggins/pony/tree/master"&gt;Pony&lt;/a&gt; gem. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ActionMailer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the most popular gem for sending emails on Rails. In case your app is written on top of it, ActionMailer will certainly come up. It lets you send emails using mailer classes and views. &lt;/p&gt;

&lt;h2&gt;
  
  
  Using Mailtrap to test email sending with Net::SMTP
&lt;/h2&gt;

&lt;p&gt;Setup is very simple. Once you’re in your demo inbox, copy the SMTP credentials on the SMTP Settings tab and insert them in your code. Or you can get a ready-to-use template of a simple message in the Integrations section. Just choose a programming language or framework your app is built with.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require 'net/smtp'

message = &amp;lt;&amp;lt;END_OF_MESSAGE
From: YourRubyApp &amp;lt;info@yourrubyapp.com&amp;gt;
To: BestUserEver &amp;lt;your@bestuserever.com&amp;gt;
Subject: Any email subject you want
Date: Tue, 02 Jul 2019 15:00:34 +0800

Lorem Ipsum
END_OF_MESSAGE
Net::SMTP.start('smtp.mailtrap.io', 587, '&amp;lt;username&amp;gt;', '&amp;lt;password&amp;gt;', :cram_md5) do |smtp|
  smtp.send_message message,
  'info@yourrubyapp.com',
  'your@bestuserever.com'
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything is alright, you’ll see your message in the Mailtrap Demo inbox. Also, you can try to check your email with HTML content and an attachment. Mailtrap allows you to see how your email will look and check HTML if necessary.&lt;/p&gt;

&lt;p&gt;Create a &lt;a href="https://mailtrap.io/register/signup"&gt;Mailtrap account&lt;/a&gt; and try to test it on your own for free!&lt;/p&gt;

&lt;p&gt;You have just read the full tutorial on how to test and send emails in Ruby. There is  still a lot to check out when it comes to Ruby Mail, Pony and Action Mailer. Learn more about Ruby Gems &amp;amp; Socket Class in our full &lt;a href="https://mailtrap.io/blog/ruby-send-email/#Best-Ruby-gems-for-sending-emails"&gt;Sending Emails with Ruby article &lt;/a&gt;at Mailtrap.io.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to send Emails in PHP?</title>
      <dc:creator>AlexandraMT</dc:creator>
      <pubDate>Mon, 11 Apr 2022 13:29:20 +0000</pubDate>
      <link>https://dev.to/alexandramt/how-to-send-emails-in-php-4fgj</link>
      <guid>https://dev.to/alexandramt/how-to-send-emails-in-php-4fgj</guid>
      <description>&lt;p&gt;PHP is one of the most popular web-development languages and a popular way to create dynamic web apps. In this article we’re going to help you painlessly configure the mail function in your application. &lt;/p&gt;

&lt;p&gt;So let us start! There are two basic ways of sending emails with PHP: a built-in mail function and external mail packages.&lt;/p&gt;

&lt;p&gt;To read the full article, check out Mailtrap's blog:&lt;a href="https://mailtrap.io/blog/php-email-sending/"&gt; How to Send Emails in PHP&lt;/a&gt;?&lt;/p&gt;

&lt;h2&gt;
  
  
  PHP built-in mail function ()
&lt;/h2&gt;

&lt;p&gt;PHP’s built-in mail function () is very simple, and  it provides limited functionality for sending emails. You won’t be able to add attachments to your email, and building a beautiful HTML template with embedded images will be a tricky task to accomplish . &lt;/p&gt;

&lt;p&gt;The other side of the PHP mail function () coin is that the email is sent from your web server, which may cause issues with deliverability due to security concerns such as suspicion of spam and blacklisting. The best way to overcome this problem is sending messages via an SMTP server, however, this functionality is limited as well. PHP mail() does not usually allow you to use the external SMTP server and it does not support SMTP authentication.&lt;/p&gt;

&lt;p&gt;Here’s what you can do with PHP’s built-in mail function(): &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create simple HTML/text messages without attachments and images&lt;/li&gt;
&lt;li&gt;send emails via localhost and Xmapp &lt;/li&gt;
&lt;li&gt;include several recipients with “$to” parameter. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is suitable for simple, mostly text-based notifications in your local environment. If you need to communicate with your app’s users, it is better to install an external mailer package.&lt;/p&gt;

&lt;p&gt;If you are still committed to the PHP built-in mail function() and are ready to accept the challenge, let’s take a look at the basic code and its main parameters. &lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax and parameters
&lt;/h2&gt;

&lt;p&gt;The PHP mail syntax is pretty simple:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;?php&lt;br&gt;
mail($to_email_address,$subject,$message,[$headers],[$parameters]);&lt;br&gt;
?&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It uses the following parameters: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“$to” = your message recipient(s). The email address format may be &lt;a href="mailto:user@example.com"&gt;user@example.com&lt;/a&gt; or User &lt;a href="mailto:user@example.com"&gt;user@example.com&lt;/a&gt;. In general, it needs to comply with &lt;a href="http://www.faqs.org/rfcs/rfc2822.html"&gt;RFC 2822&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;“$subject” = your message’s subject&lt;/li&gt;
&lt;li&gt;“$message” = the body of your message. Lines should be separated with a CRLF (\r\n). Each line should not exceed 70 characters.&lt;/li&gt;
&lt;li&gt;“[$headers]” = additional recipients of your message, which can be included in CC or BCC. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that headers are optional, except for the “from” header: it must be specified, otherwise, you will receive an error message &lt;em&gt;like Warning: mail(): “sendmail_from” not set in php.ini or custom “From:” header missing.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You can use additional headers to change the mail “From” address and set the “Reply to” address.&lt;/p&gt;

&lt;p&gt;For more details and additional parameters, refer to the &lt;a href="http://php.net/manual/en/function.mail.php"&gt;PHP documentation&lt;/a&gt;.  &lt;/p&gt;
&lt;h2&gt;
  
  
  Sending HTML email using PHP mail() function
&lt;/h2&gt;

&lt;p&gt;The body of the message can be written in HTML. However, as we’ve mentioned above, it should be simple. In the PHP mail function(), the HTML part will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$message = '
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
  &amp;lt;title&amp;gt;Review Request Reminder&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
  &amp;lt;p&amp;gt;Here are the cases requiring your review in December:&amp;lt;/p&amp;gt;
  &amp;lt;table&amp;gt;
    &amp;lt;tr&amp;gt;
      &amp;lt;th&amp;gt;Case title&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;Category&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;Status&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;Due date&amp;lt;/th&amp;gt;
    &amp;lt;/tr&amp;gt;
    &amp;lt;tr&amp;gt;
      &amp;lt;td&amp;gt;Case 1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Development&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;pending&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Dec-20&amp;lt;/td&amp;gt;
    &amp;lt;/tr&amp;gt;
    &amp;lt;tr&amp;gt;
      &amp;lt;td&amp;gt;Case 1&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;DevOps&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;pending&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Dec-21&amp;lt;/td&amp;gt;
    &amp;lt;/tr&amp;gt;
  &amp;lt;/table&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s important to remember that to send HTML mail, you need to set the Content-type header:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$headers[] = 'MIME-Version: 1.0';&lt;br&gt;
$headers[] = 'Content-type: text/html; charset=iso-8859-1';&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Simple Transmission Protocol (SMTP)
&lt;/h2&gt;

&lt;p&gt;Where do I specify the SMTP settings? This is a fair question. Go to the PHP installation folder and configure them in the “php.ini” file. But this will only work for localhost or Xmapp like solutions because as we have already mentioned,  PHP mail function does not support SMTP authentication and doesn’t allow sending messages via external servers. &lt;/p&gt;

&lt;p&gt;There are some other, rather haphazard options but we won’t promote them here. Alternatively, we recommend using external PHP mail packages for sending emails via an external SMTP server.&lt;/p&gt;

&lt;h2&gt;
  
  
  PHP mailing packages
&lt;/h2&gt;

&lt;p&gt;As we have already mentioned, the native PHP mail() function has limited functionality when it comes to mass sending. For example, it is not designed for creating &lt;a href="https://www.appypie.com/email-marketing-templates"&gt;engaging email templates&lt;/a&gt; that may boost your next campaign or sending a large volume of emails.&lt;/p&gt;

&lt;p&gt;But since PHP is still one of the &lt;a href="https://apiumhub.com/tech-blog-barcelona/interesting-facts-software-development/"&gt;most popular programming languages&lt;/a&gt;, it also doesn’t lack resources for sending mass emails. We can highly recommend several plugins, such as Pear Mail and Swift Mailer&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pear Mail&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pear.php.net/package/Mail"&gt;Pear Mail&lt;/a&gt; is a class that provides multiple interfaces for sending emails (which is stated in their documentation). &lt;/p&gt;

&lt;p&gt;Here is what you can do with Pear Mail: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create complex HTML/text messages with attachments and inlined images (with &lt;a href="https://pear.php.net/manual/en/package.mail.mail-mime.php"&gt;Mail_Mime class&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;send emails via PHP’s built-in mail() function, a sendmail program, or SMTP server&lt;/li&gt;
&lt;li&gt;send multiple emails from a queue (with &lt;a href="https://pear.php.net/manual/en/package.mail.mail-queue.php"&gt;Mail_Queue&lt;/a&gt; class).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Swift Mailer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://swiftmailer.symfony.com/"&gt;Swift Mailer&lt;/a&gt; is another popular package for sending emails in PHP. It is feature-rich, well covered by documentation, and pretty straightforward in use.&lt;/p&gt;

&lt;p&gt;Here is what you can do with Swift Mailer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create complex HTML/multipart templates &lt;/li&gt;
&lt;li&gt;add attachments and embed images&lt;/li&gt;
&lt;li&gt;send emails via authenticated SMTP, sendmail, Postfix, or your own transport&lt;/li&gt;
&lt;li&gt;use additional plugins.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Besides that, Swift Mailer offers enhanced security and handles large attachments and images with low memory usage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PHPMailer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And finally, &lt;a href="https://github.com/PHPMailer/PHPMailer"&gt;PHPMailer&lt;/a&gt;, which is the classic and the most popular email sending library for PHP. It deserves a separate article and a tutorial. You will find it &lt;a href="https://mailtrap.io/blog/phpmailer/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here is what you can do with PHPMailer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create complex HTML/multipart templates&lt;/li&gt;
&lt;li&gt;add attachments and embedded images&lt;/li&gt;
&lt;li&gt;send emails via authenticated SMTP.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PHPMailer is protected against header injection attacks and automatically validates emails.&lt;/p&gt;

&lt;p&gt;In this article, we have described the basic PHP email sending principles, syntax, and parameters. Moreover, we have reviewed the main ways of sending emails with PHP: its built-in mail function and the most popular external mail packages. PHPMailer and Swift Mailer are standard libraries for PHP email sending today, and PEAR Mail is still widely used. &lt;/p&gt;

&lt;p&gt;Choose your option according to your current needs and preferences and test your emails beforehand. For email experiments - create an account at &lt;a href="https://mailtrap.io/"&gt;Mailtrap&lt;/a&gt;, a fake SMTP server. It imitates a real SMTP server and traps your test email in the virtual inboxes. Have a try!&lt;/p&gt;

&lt;p&gt;Check the full &lt;a href="https://mailtrap.io/blog/php-email-sending/"&gt;Sending emails with PHP&lt;/a&gt; article at Mailtrap.io to get more details on email packages and examples!&lt;/p&gt;

</description>
      <category>php</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to send Emails with JavaScript</title>
      <dc:creator>AlexandraMT</dc:creator>
      <pubDate>Mon, 11 Apr 2022 13:13:34 +0000</pubDate>
      <link>https://dev.to/alexandramt/how-to-send-emails-with-javascript-ie3</link>
      <guid>https://dev.to/alexandramt/how-to-send-emails-with-javascript-ie3</guid>
      <description>&lt;p&gt;JavaScript is a programming language that you can use for both front-end and back-end development.  You can’t send emails using JavaScript code alone due to lack of support for server sockets. For this, you need a server-side language that talks to the SMTP server. You can use JS in conjunction with a server script that will send emails from the browser based on your requests. This is the value we’re going to introduce below.&lt;/p&gt;

&lt;p&gt;So, let’s figure out how you can use JS to send emails from the app that has no back-end. &lt;/p&gt;

&lt;p&gt;If you want to read a full article, check it out on the Mailtrap’s blog:  &lt;a href="https://mailtrap.io/blog/javascript-send-email/"&gt;Sending Emails with JavaScript &lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  SmtpJS.com – true email sending from JavaScript
&lt;/h2&gt;

&lt;p&gt;SmtpJS is a free library you can use for sending emails from JavaScript. All you need is an SMTP server and a few manipulations to get things done. We’ll use Mailtrap as the server because it is an actionable solution for email testing. Below is the flow you should follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create an HTML file (for example, test.html) with the following script:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;script src="https://smtpjs.com/v3/smtp.js"&amp;gt;&lt;br&gt;
&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a button that will trigger the JavaScript function&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;input type="button" value="Send Email" onclick="sendEmail()"&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write the JS function to send emails via SmtpJS.com.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;function sendEmail() {&lt;br&gt;
Email.send({&lt;br&gt;
    Host : "smtp.mailtrap.io",&lt;br&gt;
    Username : "&amp;lt;Mailtrap username&amp;gt;",&lt;br&gt;
    Password : "&amp;lt;Mailtrap password&amp;gt;",&lt;br&gt;
    To : 'recipient@example.com',&lt;br&gt;
    From : "sender@example.com",&lt;br&gt;
    Subject : "Test email",&lt;br&gt;
    Body : "&amp;lt;html&amp;gt;&amp;lt;h2&amp;gt;Header&amp;lt;/h2&amp;gt;&amp;lt;strong&amp;gt;Bold text&amp;lt;/strong&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/br&amp;gt;&amp;lt;em&amp;gt;Italic&amp;lt;/em&amp;gt;&amp;lt;/html&amp;gt;"&lt;br&gt;
}).then(&lt;br&gt;
  message =&amp;gt; alert(message)&lt;br&gt;
);&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you have multiple recipients, you can specify an array of email addresses in the To: property.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run &lt;em&gt;test.html&lt;/em&gt; in the browser and send your email&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2xc8aIU6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/romjf4a78p6iabkujlg4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2xc8aIU6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/romjf4a78p6iabkujlg4.png" alt="Image description" width="880" height="175"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The drawback with the code sample above is that your username and password are visible in the client-side script. This can be fixed if you utilize the encryption option provided by SmtpJS. Click the &lt;strong&gt;Encrypt your SMTP credentials&lt;/strong&gt; button and fill in the required fields. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PuzIjWdU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ogzvqbag1ds2d3t0cown.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PuzIjWdU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ogzvqbag1ds2d3t0cown.png" alt="Image description" width="774" height="814"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that, press Generate security token and use it then in your JS function instead of SMTP server settings like the following:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SecureToken : "&amp;lt;your generated token&amp;gt;"&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  EmailJS
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.emailjs.com/"&gt;EmailJS.com&lt;/a&gt; allows you to connect your email service, build an email template and send it from JavaScript without any server code. Let’s check out the scope.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create an account and choose an email service to connect with. There are the popular transactional services options available, such as Amazon SES or Mailgun, as well as personal services like Gmail or Outlook. You can also add a custom SMTP server. That’s what we’re going to do since we use Mailtrap. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VcSLTE-8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iw12o1m99f1fhzzrlmqh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VcSLTE-8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iw12o1m99f1fhzzrlmqh.png" alt="Image description" width="880" height="478"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create an email template using the built-in editor. The editor provides plenty of options for content building and other useful features, such as auto-reply, reCAPTCHA verification, and more. It’s also necessary to understand the basics of coding your own HTML email template. For this, read our &lt;a href="https://mailtrap.io/blog/build-html-email/"&gt;Guide on How to Build HTML Email&lt;/a&gt;. Once this is done, click &lt;strong&gt;Save&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;One of the major benefits of EmailJS.com is that the typical email attributes are hidden. The template includes the recipient field and it cannot be overridden from JS, so you send the template you have configured previously.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now you need to install EmailJS SDK. This can be done with npm:
&lt;code&gt;npm install emailjs-com --save&lt;/code&gt;
or bower
&lt;code&gt;bower install emailjs-com --save&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you need to use EmailJS on your website, paste the following code before closing tag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script type="text/javascript"
   src="https://cdn.jsdelivr.net/npm/emailjs-com@2.4.0/dist/email.min.js"&amp;gt;
&amp;lt;/script&amp;gt;
&amp;lt;script type="text/javascript"&amp;gt;
   (function(){
      emailjs.init("YOUR_USER_ID"); //use your USER ID
   })();
&amp;lt;/script&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The actual email sending can be carried out via two methods: &lt;code&gt;emailjs.send&lt;/code&gt; or &lt;code&gt;emailjs.sendForm&lt;/code&gt;. Here are the code examples for both of them:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;emailjs.send&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var templateParams = {
    name: 'James',
    notes: 'Check this out!'
};

emailjs.send('YOUR_SERVICE_ID', 'YOUR_TEMPLATE_ID', templateParams) //use your Service ID and Template ID
    .then(function(response) {
       console.log('SUCCESS!', response.status, response.text);
    }, function(error) {
       console.log('FAILED...', error);
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;emailjs.sendForm&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var templateParams = {
    name: 'James',
    notes: 'Check this out!'
};

emailjs.sendForm('YOUR_SERVICE_ID', 'YOUR_TEMPLATE_ID', templateParams) //use your Service ID and Template ID
    .then(function(response) {
       console.log('SUCCESS!', response.status, response.text);
    }, function(error) {
       console.log('FAILED...', error);
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although sending emails is a server-side thing, you can refrain from dealing with server-side coding. SmtpJS and EmailJS are two actionable solutions to streamline your front-end and make it able to send emails. EmailJS is better because it hides typical email attributes and you are able to send whatever templates you have configured before. You can also use the mailto method, which is different but can still be useful in some cases. Try Mailtrap to test what better works for you!&lt;/p&gt;

&lt;p&gt;Learn more about mailto and read the full article: &lt;a href="https://mailtrap.io/blog/javascript-send-email/"&gt;Sending Emails with JavaScript &lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Consider to create an account at &lt;a href="http://mailtrap.io"&gt;Mailtrap.io&lt;/a&gt; and try the described flow by your own!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>testing</category>
      <category>email</category>
    </item>
    <item>
      <title>How to Send an Email with Nodemailer</title>
      <dc:creator>AlexandraMT</dc:creator>
      <pubDate>Mon, 11 Apr 2022 12:53:28 +0000</pubDate>
      <link>https://dev.to/alexandramt/how-to-send-an-email-with-nodemailer-3b7a</link>
      <guid>https://dev.to/alexandramt/how-to-send-an-email-with-nodemailer-3b7a</guid>
      <description>&lt;p&gt;Nodemailer creators say that it makes sending an email a piece of cake. Let’s see if they are talking about cooking or eating 🙂 The idea of this article is to explain how to use Nodemailer for email sending. We will focus mainly on SMTP and HTML aspects but will also do an overview of all Nodemailer capabilities. Besides, this tutorial will help you prepare and test email messages to send out with your Node.js application.&lt;/p&gt;

&lt;p&gt;If you want to read the full article, check it out on the Mailtrap’s blog - &lt;a href="https://mailtrap.io/blog/sending-emails-with-nodemailer/"&gt;How to Send an Email with Nodemailer&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  How to use Nodemailer
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Installation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The only thing required to start using Nodemailer is Node.js version 6.0 or above. You should also install Nodemailer itself but it’s really easy with the npm or Yarn package manager. Execute the following command in the Node.js command prompt:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install nodemailer&lt;/code&gt;&lt;br&gt;
or&lt;br&gt;
&lt;code&gt;yarn add nodemailer&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once completed, include it into your application like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var nodemailer = require('nodemailer');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;or this if you are using ES modules:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import nodemailer from ‘nodemailer’;&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Sending messages
&lt;/h2&gt;

&lt;p&gt;To send a message with Nodemailer, there are three main steps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1. Create Nodemailer transporter&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;SMTP is the most common transporter, and below we will describe it in more detail, as well as demonstrate some examples. But there is a list of other available options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;em&gt;Built-in transports&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;sendmail, a regular sendmail command for simple messages. It’s similar to the mail() function in PHP&lt;/li&gt;
&lt;li&gt;SES , to handle large traffic of emails by sending them using &lt;a href="https://aws.amazon.com/ses/"&gt;Amazon SES&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;stream, a buffer for testing purposes, to return messages.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;-_ External transport. To put it simply, you can create your own transportation method._&lt;/p&gt;

&lt;p&gt;For more details, refer to the &lt;a href="https://nodemailer.com/about/"&gt;Nodemailer documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With SMTP, everything is pretty straightforward. Set host, port, authentication details and method, and that’s it. It’s also useful to verify that SMTP connection is correct at this stage: *&lt;em&gt;add verify(callback) *&lt;/em&gt; call to test connection and authentication.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;transporter.verify(function(error, success) {
   if (error) {
        console.log(error);
   } else {
        console.log('Server is ready to take our messages');
   }
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to test emails in Nodemailer?
&lt;/h2&gt;

&lt;p&gt;To test emails sent with Nodemailer, we will use &lt;a href="https://mailtrap.io/"&gt;Mailtrap&lt;/a&gt;, an online tool for complex email testing in a pre-production environment. It will catch our messages, display how they  look in a real email client, and help analyze and debug them. Mailtrap also provides Bcc testing options and allows you to share your email testing results with other team members, as well as forward emails to the real verified addresses.&lt;/p&gt;

&lt;p&gt;Even If you don’t have an account yet, the whole set up process takes just a couple of minutes. Mailtrap integrates as a regular SMTP server. Quickly &lt;a href="https://mailtrap.io/register/signup"&gt;sign up&lt;/a&gt; (it’s free), go to the &lt;strong&gt;SMTP settings&lt;/strong&gt; tab in your Inbox, copy the necessary settings, and insert them to your application script.&lt;/p&gt;

&lt;p&gt;Mailtrap offers a ready-to-use integration with Nodemailer: select it from the &lt;strong&gt;Integrations&lt;/strong&gt; section and insert it into your application code. It already contains transporter and syntaxis attributes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var transport = nodemailer.createTransport({
  host: "smtp.mailtrap.io",
  port: 2525,
  auth: {
    user: "1a2b3c4d5e6f7g", //generated by Mailtrap
    pass: "1a2b3c4d5e6f7g" //generated by Mailtrap
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Otherwise, you can use auto-generated email test accounts on Ethereal, which is also a fake SMTP service, mostly aimed at Nodemailer users.&lt;/p&gt;

&lt;p&gt;Recently, Nodemailer has introduced &lt;a href="https://nodemailer.com/app/"&gt;NodemailerApp&lt;/a&gt;. It provides Sendmail replacement, but first of all, is designed to debug emails. NodemailerApp has SMTP and POP3 local servers, a catchall email domain service, along with email preview capabilities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2. Set Nodemailer message options&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At this point, we should specify the sender, message recipients, and the content of our message.&lt;/p&gt;

&lt;p&gt;Remember, Unicode is supported, so you can include emojis as well!&lt;/p&gt;

&lt;p&gt;To send a text formatted as HTML, no extra attributes are required, just put your HTML body into the message with an html attribute. For advanced templates, you can add attachments and embed images. Let’s take a look at this example of simple message options first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var mailOptions = {
    from: '"Example Team" &amp;lt;from@example.com&amp;gt;',
    to: 'user1@example.com, user2@example.com',
    subject: 'Nice Nodemailer test',
    text: 'Hey there, it’s our first message sent with Nodemailer ;) ',
    html: '&amp;lt;b&amp;gt;Hey there! &amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; This is our first message sent with Nodemailer'
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Attachments in Nodemailer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can add different types of data to your message in Nodemailer using the following main properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;filename: the name of the attached file. Here you can use Unicode as well.&lt;/li&gt;
&lt;li&gt;content:  the body of your attachment. It can be a string, a buffer, or a stream.
&lt;/li&gt;
&lt;li&gt;path: path to the file, to stream it instead of including it in the message. It is a good option for big attachments.&lt;/li&gt;
&lt;li&gt;href: attachment URL. Data URIs are also supported.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; list: {
            // List-Help: &amp;lt;mailto:admin@example.com?subject=help&amp;gt;
            help: 'admin@example.com?subject=help',

            // List-Unsubscribe: &amp;lt;http://example.com&amp;gt; (Comment)
            unsubscribe: [
                {
                    url: 'http://example.com/unsubscribe',
                    comment: 'A short note about this url'
                },
                'unsubscribe@example.com'
            ],

            // List-ID: "comment" &amp;lt;example.com&amp;gt;
            id: {
                url: 'mylist.example.com',
                comment: 'my new list'
            }
        }
    };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Optional properties let you add specific content types or inline images.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;contentType&lt;/strong&gt;: if you don’t set it, it will be inferred from the filename property&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;` // An array of attachments
    attachments: [
        // String attachment
        {
            filename: 'notes.txt',
            content: 'new important notes',
            contentType: 'text/plain' // optional, would be detected from the filename
        },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;`&lt;br&gt;
&lt;strong&gt;CID&lt;/strong&gt;: inline images in the HTML message. For more details on attaching images to HTML emails, read this post. Note that the CID value should be unique.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cid: 'note@example.com' // should be as unique as possible
            },

            // File Stream attachment
            {
                filename: 'matrix neo.gif',
                path: __dirname + '/assets/neo.gif',
                cid: 'neo@example.com' // should be as unique as possible
            }
        ],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Encoding:&lt;/strong&gt; can be added to the string type of content. It will encode the content to a buffer type according to the encoding value you set (base64, binary, etc.)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     `   // Binary Buffer attachment
        {
            filename: 'image.png',
            content: Buffer.from(
                'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD/' +
                    '//+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4U' +
                    'g9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC',
                'base64'
            )`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 3. Deliver a message with sendMail()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once we created a transporter and configured a message, we can send it using the &lt;em&gt;sendMail()&lt;/em&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;transport.sendMail(mailOptions, (error, info) =&amp;gt; {
        if (error) {
            return console.log(error);
        }
        console.log('Message sent: %s', info.messageId);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Nodemailer capabilities
&lt;/h2&gt;

&lt;p&gt;We have covered the info on how to create and send an email in Nodemailer via SMTP, experimenting with different types of content: HTML, tables, lists, attachments, and embedded images.What is good about Nodemailer is that it offers a bunch of various options and settings, so you can customize every piece of your email. &lt;/p&gt;

&lt;p&gt;To get Nodemailer examples and learn more about debugging options in Nodemailer - read our full &lt;a href="https://mailtrap.io/blog/sending-emails-with-nodemailer/"&gt;How to Send an Email with Nodemailer &lt;/a&gt; article.&lt;/p&gt;

</description>
      <category>nodemailer</category>
      <category>node</category>
      <category>email</category>
    </item>
  </channel>
</rss>
