1.10.8. fejezet, E-mail

Kapcsolódó hivatkozások

IMAP get headers

import Imap from "node-imap";
import inspect from 'util';
 
var imap = new Imap({
    user: "USERNAME",
    password: "PASSWORD",
    host: "imap.freemail.hu",
    port: 993,
    tls: true
})
 
imap.once('ready', function () {
    openInbox(function (err: any, box: any) {
        var f = imap.seq.fetch('1:3', {
            bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
            struct: true
        });
 
        f.on('message', function (msg: any, seqno: any) {
            var prefix = '(#' + seqno + ') ';
            msg.on('body', function (stream: any, info: any) {
                var buffer = '';
                stream.on('data', function (chunk: any) {
                    buffer += chunk.toString('utf8');
                });
                stream.once('end', function () {
                    console.log(`${prefix} Parsed header subject: %s`,
                        Imap.parseHeader(buffer).subject);
                });
            });
        })
 
        f.once('error', function (err: any) {
            console.log('Fetch error: ' + err);
        });
 
        f.once('end', function () {
            console.log('Done fetching all messages!');
            imap.end();
        });
    })
})
 
imap.once('error', function (err: any) {
    console.log(err);
});
 
imap.once('end', function () {
    console.log('Connection ended');
});
 
imap.connect()
 
function openInbox(cb: any) {
    imap.openBox('INBOX', true, cb);
}

SMTP transport

import nodemailer from "nodemailer";
 
let transporter = nodemailer.createTransport({
    pool: true,
    host: "smtp.freemail.hu",
    port: 587,
    secure: false, // upgrade later with STARTTLS
    auth: {
        user: "username@freemail.hu",
        pass: "PASSWORD",
    },
});
 
var mailOptions = {
    from: '"Me" <me@freemail.hu>', // sender address
    to: 'you@email.hu', // list of receivers
    subject: 'Hello', // Subject line
    text: 'Hello world ?', // plaintext body
    html: '<b>Hello world ?</b>' // html body
};
 
transporter.verify(function (error, success) {
    if (error) {
        console.log(error);
    } else {
        console.log("Server is ready to take our messages");
        transporter.sendMail(mailOptions).finally(() => {
            transporter.close();
        })
 
    }
});