PHP code example of ddrv / mailer

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

    

ddrv / mailer example snippets


     



$transport = new Ddrv\Mailer\Transport\SendmailTransport(
    '-f' // sendmail options
);



$transport = new Ddrv\Mailer\Transport\SmtpTransport(
    'smtp.fight.club',  // host (REQUIRED)
    25,                 // port (REQUIRED)
    'info',             // login (REQUIRED)
    'super-secret',     // password (REQUIRED)
    '[email protected]',   // sender email (REQUIRED)
    null,               // encryption: 'tls', 'ssl' or null
    'http://fight.club' // domain
);



$transport = new Ddrv\Mailer\Transport\FakeTransport();



/** @var Ddrv\Mailer\Contract\Transport $transport */



/** @var Ddrv\Mailer\Contract\Transport $transport */
$mailer = new Ddrv\Mailer\Mailer($transport);



$message = new Ddrv\Mailer\Message(
    'Subject',     // Subject
    '<p>HTML</p>', // HTML body
    'Simple text.' // Text plain body
);



/**
 * @var Ddrv\Mailer\Contract\Message $message
 * @var Ddrv\Mailer\Mailer $mailer
 */
$mailer->send($message);



/** @var Ddrv\Mailer\Message $message */
$message->setSubject('Welcome to Fight Club!');
$message->setText('Welcome to Fight Club!' . PHP_EOL . 'Please, read our rules in attachments.');
$html = '<h1>Welcome to Fight Club!</h1><p>Please, read our rules in attachments.</p>';
$html .= '<img src="cid:poster" alt="Poster"/><img src="cid:ticket" alt="Your ticket"/>';
$message->setHtml($html);

$message->setSender('[email protected]', 'Support od Fight Club'); // The SMTP transport will set its value.

$message->addRecipient('[email protected]', 'Tyler Durden', Ddrv\Mailer\Message::RECIPIENT_TO);
$message->addRecipient('[email protected]', 'Robert Paulson', Ddrv\Mailer\Message::RECIPIENT_CC);
$message->addRecipient('[email protected]', 'Angel Face', Ddrv\Mailer\Message::RECIPIENT_BCC);
$message->addRecipient('[email protected]', 'Richard Chesler', Ddrv\Mailer\Message::RECIPIENT_TO);

$message->getRecipientName('[email protected]'); // Returns 'Robert Paulson'.
$message->getRecipientName('[email protected]'); // Returns null.

$message->removeRecipient('[email protected]'); // If you change your mind.

// You may remove recipients by type
$message->removeRecipients(Ddrv\Mailer\Message::RECIPIENT_TO);
$message->removeRecipients(Ddrv\Mailer\Message::RECIPIENT_CC);
$message->removeRecipients(Ddrv\Mailer\Message::RECIPIENT_BCC);
// Or all
$message->removeRecipients();

$message->addRecipient('[email protected]', 'Tyler Durden', Ddrv\Mailer\Message::RECIPIENT_TO);
$message->addRecipient('[email protected]', 'Robert Paulson', Ddrv\Mailer\Message::RECIPIENT_CC);
$message->addRecipient('[email protected]', 'Angel Face', Ddrv\Mailer\Message::RECIPIENT_BCC);



$rules = <<<TEXT
1. You don't talk about fight club.
2. You don't talk about fight club.
3. When someone says stop, or goes limp, the fight is over.
4. Only two guys to a fight.
5. One fight at a time.
6. They fight without shirts or shoes.
7. The fights go on as long as they have to.
8. If this is your first night at fight club, you have to fight.
TEXT;
$message->attachFromString(
    'rules.txt',                // Attachment name (REQUIRED)
    $rules,                     // Contents (REQUIRED)
    'text/plain; charset=UTF-8' // Mime Type
);


$path = '/home/tyler/docs/projects/mayhem/rules.txt';

$message->attachFromFile(
    'project-mayhem.txt',       // Attachment name (REQUIRED)
     $path,                     // Path to attached file (REQUIRED)
    'text/plain; charset=UTF-8' // Mime Type
);

$message->detach('project-mayhem.txt'); // If you change your mind.

$message->setHtmlContentFromString(
    'ticket',                                            // HTML Content ID
    file_get_contents('/home/tyler/tickets/038994.jpg'), // Contents (REQUIRED)
    'image/jpeg'                                         // Mime Type
);

$message->setHtmlContentFromString(
    'script',                // HTML Content ID
    'alert("ok");',          // Contents (REQUIRED)
    'application/javascript' // Mime Type
);

$message->setHtmlContentFromFile(
    'poster',                            // HTML Content ID
    '/home/tyler/images/fight-club.jpg', // Path to file (REQUIRED)
    'image/jpeg'                         // Mime Type
);

$message->unsetBodyHtmlContent('script'); // If you change your mind.

$message->hasHeader('X-Some-Header');          // Returns false.
$message->setHeader('X-Some-Header', 'Value'); // Header set.
$message->hasHeader('X-Some-Header');          // Returns true.
$message->getHeader('X-Some-Header');          // Returns 'Value'.
$message->removeHeader('X-Some-Header');       // Header removed.
$message->hasHeader('X-Some-Header');          // Returns false.

$message->getRecipients(); // Returns array if recipients emails.
$message->getSubject(); // Returns mail subject.
$message->getHeadersRaw(); // Returns string of mail headers.
$message->getBodyRaw(); // Returns string of mail body.




/**
 * @var Ddrv\Mailer\Contract\Transport $transport
 * @var Ddrv\Mailer\Contract\Message $message1
 * @var Ddrv\Mailer\Contract\Message $message2
 */
$spool = new Ddrv\Mailer\Spool\MemorySpool();
// or
$spool = new Ddrv\Mailer\Spool\FileSpool('/path/to/emails');
// Or any implementation of Ddrv\Mailer\Contract\Spool

$wrapper = new Ddrv\Mailer\Transport\SpoolTransport($transport, $spool);
$mailer = new Ddrv\Mailer\Mailer($wrapper);

$mailer->send($message1);
$mailer->send($message2);

// Now the letters will only be added to the queue.
// To send them, you need to execute:

$wrapper->flush(
    100, // Number of emails sent.
    5    // Number of attempts to send emails.
);



/**
 * @var Ddrv\Mailer\Mailer $mailer
 * @var Ddrv\Mailer\Contract\Message $message
 */
$mailer->personal($message);



use Ddrv\Mailer\TransportFactory;

// smtp
$transport = TransportFactory::make('smtp://user:[email protected]:465/?encryption=tls&domain=example.com&sender=user%40exapmle.com&name=Informer');

// sendmail
$transport = TransportFactory::make('sendmail://localhost/?options=-i+-r+user%40example.com');

// file
$transport = TransportFactory::make('file:////path/to/mail/files');

// fake
$transport = TransportFactory::make('fake://localhost');