ForgeTotal.js 5
es

Total.js 5

Envíate un mensaje con LOGMAIL()

Total.js 5 · total5@0.0.19 mailsmtplogging Ejecutada con Total.js 5

LOGMAIL(email, [subject], body, [callback]) envía un mensaje pensado para el desarrollador, no para un cliente: sin vista ni layout. El cuerpo es un texto. El texto va dentro de un pre, en una pequeña página HTML, de modo que se conservan los saltos de línea y los espacios; se pone tal cual, así que no envíes HTML que no sea de confianza. Sin subject, el asunto es el nombre de la aplicación (name en el config).

Pasa el cuerpo como texto. Para un objeto usa JSON.stringify(object, null, 2), como hace la segunda ruta de abajo.

Usa el mismo servidor SMTP, remitente y cola que MAIL() (mail_smtp, mail_smtp_options, mail_from en el config). Está pensado para cosas como «falló la importación» con los datos que necesitas ver.

config
name : Log mail example
mail_smtp : 127.0.0.1
mail_smtp_options : { port: 2527 }
mail_from : hello@example.com
mail_from_name : Example
controllers/log.js
exports.install = function() {
	ROUTE('POST /alert/ @json', alert);
	ROUTE('POST /alert-object/ @json', alert_object);
	ROUTE('POST /alert-raw/ @json', alert_raw);
};

function alert($) {
	LOGMAIL('ops@example.com', 'Import failed', 'Line 1\nLine 2', function(err) {
		$.json({ sent: !err, error: err ? err.toString() : null });
	});
}

function alert_object($) {
	// without a subject: the subject is the name of the application; the object is written by us
	LOGMAIL('ops@example.com', JSON.stringify({ rows: 12, failed: ['a', 'b'] }, null, 2), function(err) {
		$.json({ sent: !err, error: err ? err.toString() : null });
	});
}

function alert_raw($) {
	// the object itself: it throws in total5 0.0.19
	try {
		LOGMAIL('ops@example.com', { rows: 12 }, NOOP);
		$.json({ thrown: false });
	} catch (e) {
		$.json({ thrown: true, error: e.message });
	}
}