oroinc/laminas-mail
oroinc/laminas-mail is a small bridge package for using Laminas Mail components within Oro applications. Provides the Laminas mail classes and configuration needed to send emails, manage transports, and integrate with Oro’s mailing features.
To start using oroinc/laminas-mail in Laravel, install the package via Composer:
composer require oroinc/laminas-mail
use Laminas\Mail\Message;
use Laminas\Mail\Transport\Sendmail;
// Create a message
$message = new Message();
$message->setFrom('sender@example.com')
->addTo('recipient@example.com')
->setSubject('Hello World')
->setBody('This is a test email.');
// Send using Sendmail transport (default in Laravel)
$transport = new Sendmail();
$transport->send($message);
Laminas\Mail\Message for constructing emails.Sendmail, Smtp, or File transports.SmtpOptions or FileOptions for transport-specific settings.// Create a message
$message = new Message();
// Set basic headers
$message->setFrom('noreply@example.com')
->addTo('user@example.com')
->setSubject('Your Order Confirmation')
->setEncoding('UTF-8');
// Add HTML and plain-text parts
$htmlPart = new \Laminas\Mime\Part('HTML content');
$htmlPart->setType('text/html');
$textPart = new \Laminas\Mime\Part('Plain text content');
$textPart->setType('text/plain');
// Attach parts to the message
$body = new \Laminas\Mime\Message();
$body->setParts([$textPart, $htmlPart]);
$message->setBody($body);
// Send the message
$transport->send($message);
// Configure SMTP transport
$transport = new \Laminas\Mail\Transport\Smtp();
$options = new \Laminas\Mail\Transport\SmtpOptions([
'name' => 'example.com',
'host' => env('MAIL_HOST'),
'port' => env('MAIL_PORT'),
'connection_class' => 'login',
'connection_config' => [
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'ssl' => env('MAIL_ENCRYPTION') === 'ssl' ? 'ssl' : null,
],
]);
$transport->setOptions($options);
// Save emails to a directory for debugging
$transport = new \Laminas\Mail\Transport\File();
$options = new \Laminas\Mail\Transport\FileOptions([
'path' => storage_path('app/emails'),
'mode' => 0777,
]);
$transport->setOptions($options);
Register the transport in AppServiceProvider:
public function register()
{
$this->app->singleton(\Laminas\Mail\Transport\TransportInterface::class, function ($app) {
$transport = new \Laminas\Mail\Transport\Smtp();
$options = new \Laminas\Mail\Transport\SmtpOptions([
'name' => 'example.com',
'host' => env('MAIL_HOST'),
// ... other config
]);
$transport->setOptions($options);
return $transport;
});
}
Create a helper class for common email templates:
class EmailHelper
{
public static function createPasswordResetEmail(string $token, string $email)
{
$message = new Message();
$message->setFrom('noreply@example.com')
->addTo($email)
->setSubject('Reset Your Password');
$html = '<p>Click <a href="'.url('reset-password?token='.$token).'">here</a> to reset your password.</p>';
$text = 'Click the link below to reset your password: '.url('reset-password?token='.$token);
$htmlPart = new \Laminas\Mime\Part($html);
$htmlPart->setType('text/html');
$textPart = new \Laminas\Mime\Part($text);
$textPart->setType('text/plain');
$body = new \Laminas\Mime\Message();
$body->setParts([$textPart, $htmlPart]);
$message->setBody($body);
return $message;
}
}
string|false returns).connection_time_limit for long-running scripts to avoid "Broken pipe" errors:
$options->setConnectionConfig([
'use_complete_quit' => false,
]);
$options->setConnectionTimeLimit(300); // 5 minutes
openssl is enabled in your PHP installation. For TLS, use port 587 and set 'ssl' => 'tls'.laminas/laminas-crypt:
composer require laminas/laminas-crypt
.env:
'connection_config' => [
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
]
$transport = new \Laminas\Mail\Transport\File([
'path' => storage_path('app/emails'),
]);
$logger = new \Monolog\Logger('laminas-mail');
$transport->setLogger($logger);
$transport = new \Laminas\Mail\Transport\Smtp($options);
foreach ($emails as $email) {
$message = $EmailHelper::createPasswordResetEmail($token, $email);
$transport->send($message);
}
QUIT: For servers with reuse limits, disable QUIT:
$options->setConnectionConfig([
'use_complete_quit' => false,
]);
Laminas\Mail\Transport\TransportInterface:
class CustomTransport implements TransportInterface
{
public function send(Message $message)
{
// Custom logic (e.g., API call)
}
}
Laminas\Mail\Protocol\Smtp\Auth\AbstractAuth:
class CustomAuth extends AbstractAuth
{
public function authenticate()
{
// Custom auth logic
}
}
Register it in a plugin manager:
$pluginManager = new \Laminas\Mail\Protocol\SmtpPluginManager();
$pluginManager->setService('custom', new CustomAuth());
| Error | Solution |
|---|---|
Could not read from [host] |
Check SMTP server is running, firewall rules, and credentials. |
Failed to authenticate |
Verify username/password and auth method (PLAIN, LOGIN, CRAM-MD5). |
Invalid parameter number for hash() |
Ensure laminas/laminas-crypt is installed for CRAM-MD5. |
Broken pipe during long scripts |
Set connection_time_limit and disable use_complete_quit. |
Message must contain a body |
Always set a body or parts using setBody() or setParts(). |
Mailable is preferred, you can integrate Laminas Mail by overriding the build() method:
public function build()
{
$message = new \Laminas\Mail\Message();
// Custom Laminas Mail logic
return $message;
}
Mail::to('user@example.com')->send(new CustomMailable());
// Inside CustomMailable, use Lamin
How can I help you explore Laravel packages today?