PHP code example of initphp / mailer

1. Go to this page and download the library: Download initphp/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/ */

    

initphp / mailer example snippets


use InitPHP\Mailer\Mailer;
use InitPHP\Mailer\Exception\MailerException;

$mailer = Mailer::newInstance([
    'protocol'   => 'smtp',
    'SMTPHost'   => 'smtp.example.com',
    'SMTPUser'   => '[email protected]',
    'SMTPPass'   => 'your-password',
    'SMTPPort'   => 587,
    'SMTPCrypto' => 'tls',
]);

try {
    $mailer->setFrom('[email protected]', 'Your Name')
        ->setTo('[email protected]')
        ->setSubject('Hello from InitPHP Mailer')
        ->setMessage('This is a plain-text message.')
        ->send();
} catch (MailerException $e) {
    // $e->getMessage(); for SMTP failures $e->getCode() holds the reply code.
}

$mailer = Mailer::newInstance(); // protocol defaults to "mail"

$mailer->setFrom('[email protected]', 'Your Name')
    ->setTo('[email protected]')
    ->setSubject('Hello')
    ->setMessage('Plain-text body')
    ->send();

$mailer->setMailType('html')
    ->setFrom('[email protected]', 'Your Name')
    ->setTo('[email protected]')
    ->setSubject('Newsletter')
    ->setMessage('<h1>Hello</h1><p>This is an <strong>HTML</strong> message.</p>')
    ->setAltMessage('Hello — this is the plain-text fallback.')
    ->send();

$mailer->setMailType('html')
    ->setFrom('[email protected]')
    ->setTo('[email protected]')
    ->setSubject('Invoice')
    ->attach('/path/to/invoice.pdf');             // a file on disk

// In-memory / generated content (no temp file needed):
$pdf = $generator->render();
$mailer->attachContent($pdf, 'invoice.pdf', 'attachment', 'application/pdf');

// Inline image referenced from the HTML with cid:
$mailer->attach('/path/to/logo.png', 'inline');
$cid = $mailer->setAttachmentCID('/path/to/logo.png');
$mailer->setMessage('<img src="cid:' . $cid . '"> Welcome!')
    ->send();

use InitPHP\Mailer\Facade\Mailer;

Mailer::setFrom('[email protected]')
    ->setTo('[email protected]')
    ->setSubject('Hi')
    ->setMessage('Body')
    ->send();

use InitPHP\Mailer\Mailer as MailerInstance;
use InitPHP\Mailer\Facade\Mailer;

Mailer::setInstance(MailerInstance::newInstance(['protocol' => 'smtp', /* … */]));