PHPMailer (SMTP Email Sending)
The artiframe add phpmailer extension adds the phpmailer/phpmailer package, the world's most widely used email sending library, to your project.
Instead of dealing with PHP's native mail() function, SMTP errors, and server blocks, you can securely send HTML emails with attachments using the src/Service/Mailer.php wrapper.
To install the extension, enter the following command in your terminal:
$ artiframe add phpmailer
After the command executes, SMTP configuration variables will be added to your .env file. You should fill these out with information provided by your hosting or email provider (e.g., Yandex, Gmail, AWS SES).
SMTP_HOST=smtp.gmail.com
SMTP_PORT=465
[email protected]
SMTP_PASS=eposta_sifreniz_veya_uygulama_sifresi
SMTP_SECURE=ssl
[email protected]
SMTP_FROM_NAME="ArtiFrame App"
ArtiFrame's Mailer class features a fluent interface (method chaining). You do not need to repeatedly set connection configurations; it automatically pulls them from the .env file.
<?php
use Src\Service\Mailer;
// Start the Mailer
$mailer = Mailer::create();
try {
$isSent = $mailer
->to('[email protected]', 'John Doe') // Recipient (Name is optional)
->subject('Your Order Has Been Received') // Email Subject
->html('<h1>Hello John!</h1><p>Your order is preparing...</p>') // HTML Content
->send(); // Send!
echo "Email successfully sent!";
} catch (Exception $e) {
echo "Email sending failed: " . $e->getMessage();
}
Use CC to make the recipient visible to everyone, and BCC for hidden recipients.
<?php
Mailer::create()
->to('[email protected]')
->cc('[email protected]') // Visible Carbon Copy
->bcc('[email protected]') // Blind Carbon Copy
->subject('Weekly Report')
->html('The report is attached.')
->send();
You can effortlessly attach files located on your server to the email. You can optionally give the attached file a different name.
<?php
Mailer::create()
->to('[email protected]')
->subject('Your Monthly Invoice')
->html('Please review your invoice attached.')
// 1st Parameter: Physical path of the file, 2nd Parameter: The name it will have in the email
->attach(__DIR__ . '/../../storage/invoices/INV-1001.pdf', 'Invoice_INV1001.pdf')
->attach(__DIR__ . '/../../storage/docs/contract.docx') // If no name is provided, it uses the file's name
->send();
When you display your logo or banner in the HTML content of the email as <img src="...">, mail clients sometimes block the image for security reasons (Download Pictures warning appears). To prevent this, you "embed" the image into the email and call it from the HTML with a unique identity (CID).
<?php
$html = '
<h2>Congratulations!</h2>
<p>Your account has been verified.</p>
<img src="cid:company_logo" alt="Our Logo" style="width: 200px;">
';
Mailer::create()
->to('[email protected]')
->subject('Account Verification')
->html($html)
// 1st Parameter: File path, 2nd Parameter: CID (Identity in content), 3rd Parameter: File name
->embedImage(__DIR__ . '/../../public/assets/img/logo.png', 'company_logo', 'logo.png')
->send();