Рассмотрим пример отправки почты через AJAX — запрос посредством PHP из сервера Node.js. Для начала нам потребуется инструмент генерации AJAX — запросов и для этого будем использовать модуль request. Установить ее очень просто:
npm install request
Отправка из Node.js:
var request = require('request'); request. post( { url:'http://xxx.xxx.xxx.xxx/mail/send.php', form:{ key: data.key, title: "Ваш пароль в сервисе", userEmail: data.userEmail, message:{ "Логин":result[0].email, "Пароль":result[0].password } } }, function(err, httpResponse, body){ console.log(err); console.log(httpResponse); console.log(body); });
Прием в PHP — файле send.php:
<?php $message = ""; foreach ($_REQUEST["message"] as $key => $value) { $message .= "<br/><b>".htmlspecialchars($key)."</b>: ".htmlspecialchars($value)."<br/>"; } // Для отправки HTML-письма должен быть установлен заголовок Content-type $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; // Дополнительные заголовки $headers[] = 'To: Mary <mary@example.com>, Kelly <kelly@example.com>'; $headers[] = 'From: Birthday Reminder <birthday@example.com>'; $headers[] = 'Cc: birthdayarchive@example.com'; $headers[] = 'Bcc: birthdaycheck@example.com'; // Отправляем mail($_REQUEST["userEmail"], $_REQUEST["title"], $message, $headers); echo json_encode($_REQUEST);