Merhaba, bu yazımızda sizinle beraber e-posta gönderimini öğreneceğiz, bunun için `nodemailer` paketini kullanacağız.
Diğer yazılara nispeten çerez niyetine okuyabileceğiniz ama toplu e-posta gönderimi yapacağınız zaman yararlı olabilecek bir yazı olacak. Bu yazıdan sonra npm yerine yarn kullanarak devam edeceğiz. Hızlıca kuruluma geçelim.
Terminalden,
yarn add nodemailer
Paketimizi dahil edelim,
const nodemailer = require('nodemailer') Hangi servis sağlayıcıyı kullanacaksak onu ekliyoruz,
const transporter = nodemailer.createTransport({
direct:true,
host: 'smtp.yandex.com',
port: 465,
auth: {
user: '[email protected]',
pass: 'xx' },
secure: true
})Mandrill
var transporter = nodemailer.createTransport({
direct:true,
host: 'smtp.mandrillapp.com',
port: 587,
auth: {
user: 'xx',
pass: 'xx' },
secure: false
})
E-posta opsiyonlarını girelim,
const mailOptions = {// Hangi sağlayıcıyı kullanıyorsanız onun bilgilerini girmelisiniz.
from: `"Çağatay Çalı" <[email protected]>`,
to: '[email protected]',
subject: `Burası başlık kısmı`,
text: `Buraya text girebilirsiniz..`,
html: `<h1>Buraya html girebilirsiniz.</h1>`
}E-postamızı gönderelim,
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error)
}
console.log('Message %s sent: %s', info.messageId, info.response)
})Kodumuzun son hali şu şekilde oldu,
const nodemailer = require('nodemailer')
const transporter = nodemailer.createTransport({
direct:true,
host: 'smtp.yandex.com',
port: 465,
auth: {
user: '[email protected]',
pass: 'xx' },
secure: true
})const mailOptions = {
// Ben domain'imi yandex'e bağladığım için cagatay.me olarak belirttim.
from: `"Çağatay Çalı" <[email protected]>`,
to: '[email protected]',
subject: `Burası başlık kısmı`,
text: `Buraya text girebilirsiniz..`,
html: `<h1>Buraya html girebilirsiniz.</h1>`
}transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error)
}
console.log('Message %s sent: %s', info.messageId, info.response)
})