Mail

Mail class allowes you to easily send an e-mail using any adapter you define in configs/mail.php.

Currently available mail drivers are:

Mail: \Koldy\Mail\Driver\Mail
PHPMailer: \Koldy\Mail\Driver\PHPMailer
Simulation: \Koldy\Mail\Driver\Simulate

Creating instance

Initializing Mail class should be always made by using create method. Create method can accept only one parameter, the configuration name. If you don't pass anything or you pass null, first defined connection will be used as default.


Mail::create(); // returns instance with default config
Mail::create('other-key'); // returns instance with other config set

Basic example


$mail Mail::create()
    ->
from('your@email.com')
    ->
to('destination@email.com')
    ->
subject('The e-mail subject')
    ->
body('This is some mail text...');

if (
$mail->send()) {
    echo 
'Mail is sent!';
} else {
    echo 
"Mail wasn't sent because: {$mail->getError()}";
}

Another example; sending HTML e-mail and different error handling:


Mail::create()
    ->
from('your@email.com''Your Name')
    ->
to('destination@email.com''Recipient Name')
    ->
subject('The e-mail subject')
    ->
body('<html><body>This is some HTML e-mail text...</body></html>'true'Alternative plain text...')
    ->
send();

if (
$mail->hasFailed()) {
    if (
$mail->hasException()) {
        echo 
$mail->getException()->getTraceAsString();
    } else {
        echo 
"Mail wasn't sent because: {$mail->getError()}";
    }
}

E-mail headers and files


$mail Mail::create()
    ->
from('your@email.com')
    ->
to('destination@email.com')
    ->
subject('The e-mail subject')
    ->
body('This is some mail text...')
    ->
header('header-name1''header-value1')
    ->
header('header-name2''header-value2')
    ->
attachFile('/path/to/file1.txt')
    ->
attachFile('/path/to/file2.txt''ship-file-with-another-name.txt');

if (
$mail->send()) {
    echo 
'Mail is sent!';
} else {
    echo 
"Mail wasn't sent because: {$mail->getError()}";
}
LogUrl