JavaScript
Exemple de code pour l'envoi d'un e-mail avec des pièces jointes
Pour envoyer un e-mail avec une pièce jointe, utilisez la Attachmentspropriété, comme suit:
JavaScript
CourrielJS
EmailJS.com figurait déjà dans notre article de blog, Envoyer des e-mails avec ReactJS . Ce service vous permet de connecter votre service de messagerie, de créer un modèle de courrier électronique et de l'envoyer à partir de JavaScript sans aucun code serveur. Voyons la portée.
Créez un compte et choisissez un service de messagerie avec lequel vous connecter. Il existe des options de services transactionnels populaires, telles qu'Amazon SES ou Mailgun, ainsi que des services personnels tels que Gmail ou Outlook. Vous pouvez également ajouter un serveur SMTP personnalisé. C'est ce que nous allons faire puisque nous utilisons Mailtrap.
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 Guide on How to Build HTML Email. Once this is done, click Save.
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.
Now you need to install EmailJS SDK. This can be done with npm:
npm install emailjs-com --save
JavaScript
or bower
bower install emailjs-com --save
JavaScript
If you need to use EmailJS on your website, paste the following code before closing tag:
JavaScript
The actual email sending can be carried out via two methods: emailjs.send or emailjs.sendForm. Here are the code examples for both of them:
emailjs.send
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);
});
JavaScript
emailjs.sendForm
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);
});
JavaScript
Code sample for sending emails
Let’s check whether EmailJS.com works by sending an email straight from the browser. We’ve set up an email service, Mailtrap, and built a simple template. Now, we need to create an HTML file with the following code:
JavaScript
Run it in the browser and check out the Mailtrap Demo Inbox. It works!
Pricing
EmailJS offers a free subscription plan that allows you to send up to 200 emails per month using only two templates. In addition, you’ll have a limited list of contacts and email size (up to 50Kb). Higher quotas are available for paid subscriptions: Personal ($5/month), Professional ($15/month), and Business ($50/month).
To wrap up
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. The mailto: method is different but it can also be useful in some cases. If you, however, have changed your mind and are willing to set up back-end for your app, check out one of our dedicated blog posts:
How to send emails with Python
How to send emails with Ruby
How to send emails with PHP
How to send emails with ASP.NET C#
Add comment
Name*
E-mail*
Subscribe
Your email workflows deserve more love
Try Mailtrap for free
CRAFTED BY
PRODUCTS
Jira Smart Checklist
Notemate.co
Pivotal Booster
EXPERIENCE
Open Source
Railsware Blog
MAILTRAP
Pricing
Changelog
Status
Terms of Service
Privacy Policy
Navigational Information
DPA
CONTACT
email: support[at]mailtrap.io
© Railsware Products, Inc.
What is Mailtrap?
Mailtrap is a test mail server solution that allows testing email notifications without sending them to the real users of your application. Not only does Mailtrap work as a powerful email test tool, it also lets you view your dummy emails online, forward them to your regular mailbox, share with the team and more! Mailtrap is a mail server test tool built by Railsware Products, Inc., a premium software development consulting company.