The popphp/pop-mail component provides an API to manage sending mail from your application. Support is built-in for multi-mimetype emails and attachments, as well as multiple recipients and queuing. It has a full feature set that supports:
- Send to email via sendmail, SMTP or any custom-written mail transport adapters
- Send emails to a queue of recipients, with individual message customization
- Save emails to be sent later
- Retrieve and manage emails from email mailboxes.
Install it directly into your project:
composer require popphp/pop-mail
Or, include it in your composer.json file:
{
"require": {
"popphp/pop-mail": "3.0.*",
}
}
A Note about SMTP
The SMTP transport component within pop-mail is forked from and built on top of the SMTP features and functionality of the Swift Mailer Library and the great work the Swift Mailer team has accomplished over the past years.
Here's an example sending a basic email using sendmail
:
use Pop\Mail;
$transport = new Mail\Transport\Sendmail()
$mailer = new Mail\Mailer($transport);
$message = new Mail\Message('Hello World');
$message->setTo('you@domain.com');
$message->setFrom('me@domain.com');
$message->attachFile(__DIR__ . '/image.jpg');
$message->setBody('Hello World! This is a test!');
$mailer->send($message);
Here's an example sending a basic email using SMTP (MS Exchange):
use Pop\Mail;
$transport = new Mail\Transport\Smtp('mail.msdomain.com', 587);
$transport->setUsername('me')
->setPassword('password');
$mailer = new Mail\Mailer($transport);
$message = new Mail\Message('Hello World');
$message->setTo('you@domain.com');
$message->setFrom('me@domain.com');
$message->attachFile(__DIR__ . '/image.jpg');
$message->setBody('Hello World! This is a test!');
$mailer->send($message);
Here's an example sending a basic email using SMTP (Gmail Exchange):
use Pop\Mail;
$transport = new Mail\Transport\Smtp('smtp.gmail.com', 587, 'tls');
$transport->setUsername('me@mydomain.com')
->setPassword('password');
$mailer = new Mail\Mailer($transport);
$message = new Mail\Message('Hello World');
$message->setTo('you@domain.com');
$message->setFrom('me@domain.com');
$message->attachFile(__DIR__ . '/image.jpg');
$message->setBody('Hello World! This is a test!');
$mailer->send($message);
use Pop\Mail;
$mailer = new Mail\Mailer(new Mail\Transport\Sendmail());
$message = new Mail\Message('Hello World');
$message->setTo('you@domain.com');
$message->setFrom('me@domain.com');
$fileData = file_get_contents($fileData);
$message->attachFile($fileData, 'image/jpeg', 'myimage.jpg');
$message->setBody('Hello World! This is a test!');
$mailer->send($message);
use Pop\Mail;
$mailer = new Mail\Mailer(new Mail\Transport\Sendmail());
$message = new Mail\Message('Hello World');
$message->setTo('you@domain.com');
$message->setFrom('me@domain.com');
$message->addText('Hello World! This is a test!');
$message->addHtml('<html><body><h1>Hello World!</h1><p>This is a test!</p></body></html>');
$mailer->send($message);
use Pop\Mail;
$queue = new Queue();
$queue->addRecipient([
'email' => 'me@domain.com',
'name' => 'My Name',
'company' => 'My Company',
'url' => 'http://www.domain1.com/'
]);
$queue->addRecipient([
'email' => 'another@domain.com',
'name' => 'Another Name',
'company' => 'Another Company',
'url' => 'http://www.domain2.com/'
]);
$message = new Mail\Message('Hello [{name}]!');
$message->setFrom('noreply@domain.com');
$message->setBody(
<<<TEXT
How are you doing? Your [{company}] is great!
I checked it out at [{url}]
TEXT
);
$queue->addMessage($message);
$mailer = new Mail\Mailer(new Mail\Transport\Sendmail());
$mailer->sendFromQueue($queue);
use Pop\Mail;
$message = new Mail\Message('Hello World');
$message->setTo('you@domain.com');
$message->setFrom('me@domain.com');
$message->addText('Hello World! This is a test!');
$message->addHtml('<html><body><h1>Hello World!</h1><p>This is a test!</p></body></html>');
$message->save(__DIR__ . '/mailqueue/test.msg');
$mailer = new Mail\Mailer(new Mail\Transport\Sendmail());
$mailer->sendFromDir(__DIR__ . '/mailqueue');
use Pop\Mail\Client;
$imap = new Client\Imap('imap.gmail.com', 993);
$imap->setUsername('me@domain.com')
->setPassword('password');
$imap->setFolder('INBOX');
$imap->open('/ssl');
// Sorted by date, reverse order (newest first)
$ids = $imap->getMessageIdsBy(SORTDATE, true);
$headers = $imap->getMessageHeadersById($ids[0]);
$parts = $imap->getMessageParts($ids[0]);
// Assuming the first part is an image attachement, display image
header('Content-Type: image/jpeg');
header('Content-Length: ' . strlen($parts[0]->content));
echo $parts[0]->content;