PHP code example of onviser / mailer

1. Go to this page and download the library: Download onviser/mailer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

onviser / mailer example snippets


$mail = (new Mail())
            ->setSubject('This is a subject')
            ->setBody('This is a body')
            ->setFrom('[email protected]')
            ->to('[email protected]');
            
$socket = new Socket('localhost', 25, 'login', 'password');            
$mailer = new SendToSocketMailer($socket);
if ($mailer->send($mail)) {
    // mail was sent    
} else { // error
    echo "error: {$mailer->getError()}" . PHP_EOL;
    print_r($mailer->getLog());
}

$mail = (new Mail())
            ->setSubject('This is a subject')
            ->setBody('This is a body')
            ->setFrom('[email protected]')
            ->to('[email protected]')
            ->to('[email protected]')
            ->copy('[email protected]')
            ->copy('[email protected]')
            ->blindCopy('[email protected]');

$mail = (new Mail())
            ->setSubject('This is a subject')
            ->setBody('This is a body')
            ->setFrom('[email protected]')
            ->setReplayTo('[email protected]')
            ->to('[email protected]');

$attachmentText = (new Attachment())
                    ->setFileName('file.txt')
                    ->setFileType('text/plain')
                    ->setData('This is a text.');
$attachmentHTML = (new Attachment())
                    ->setFileName('file.html')
                    ->setFileType('text/html')
                    ->setData('<p>This is a <strong>HTML</strong>.</p>');
$mail = (new Mail())
            ->setType(Mail::TYPE_HTML)
            ->setSubject('This is a subject')
            ->setBody('<p>This is a <strong>HTML</strong> body</p>')
            ->setFrom('[email protected]')
            ->to('[email protected]')
            ->attachment($attachmentText)
            ->attachment($attachmentHTML);

$mail = (new Mail())
            ->setSubject('This is a subject')
            ->setBody('This is a body')
            ->setFrom('[email protected]')
            ->to('[email protected]');
            
$mailer = new SendToFileMailer('../var/mail/');
if ($mailer->send($mail)) {
    // mail was saved    
} else { // error
    echo "error: {$mailer->getError()}" . PHP_EOL;
}