Skip to content

Mail Extension

Dmitriy Zayceff edited this page Apr 24, 2015 · 7 revisions

A simple extension for sending mail via an smtp server.

How to use?

Sending email via Gmail account.

use php\mail\Email;
use php\mail\EmailBackend;

// Create email server backend configuration.
$backend = new EmailBackend();

// set username and password of gmail account
$backend->setAuthentication('[email protected]', 'password');

// host name of gmail smtp server
$backend->hostName     = 'smtp.googlemail.com';

$backend->smtpPort     = 465;
$backend->sslOnConnect = true;

$email = new Email();
$email->setSubject('Hello!');
$email->setFrom('[email protected]', 'JPHP Compiler');
$email->setTo(['[email protected]', '[email protected]']);

// Set html text
$email->setHtmlMessage('<b>Foobar</b>');

// Set simple text
$email->setTextMessage('Foobar');

// Add attachment using file, also you can use php\io\Stream objects.
$email->attach('path/to/file', 'image/jpeg', 'My Picture');

try {
   $messageId = $email->send($backend);
} catch (Exception $e) {
   // error ...
}
Clone this wiki locally