Skip to content
← All PrestaShop 9 errors

PrestaShop 9 errors

Class "Swift_Message" not found in PrestaShop 9

PrestaShop 9 replaced Swift Mailer with Symfony Mailer and no longer ships the Swift classes. Modules that built emails with Swift_Message or Swift_SmtpTransport stop sending.

The error
Class "Swift_Message" not found
Removed in
PrestaShop 9.0
Usually found in
Newsletter, notification and SMTP modules

What happened

Up to version 8, PrestaShop sent email through the Swift Mailer library, and its classes were available to every module. Some modules used them directly: Swift_Message to build a message, Swift_SmtpTransport to connect to a mail server, Swift_Attachment to attach a file.

Swift Mailer is no longer maintained, and PrestaShop 9.0 switched to Symfony Mailer. The Swift classes are no longer included, so the first line that uses one fails with Class "Swift_Message" not found (or Swift_SmtpTransport, Swift_Mailer, Swift_Attachment).

The risk is that it often happens out of sight: in a cron task, after an order, when a form is sent. The store keeps working and the emails just stop.

The fix

Usually the best fix is to stop building emails by hand and use PrestaShop's own Mail::send(). It uses the store's mail settings, templates and languages, and it works in 1.7, 8 and 9:

Mail::send(
    (int) $idLang,
    'my_template',               // mails/{iso}/my_template.html and .txt in the module
    $this->trans('Your subject', [], 'Modules.Mymodule.Admin'),
    ['{firstname}' => $firstname],
    $toEmail,
    $toName,
    null, null,
    $fileAttachment,             // ['content' => ..., 'name' => ..., 'mime' => ...] or null
    null,
    _PS_MODULE_DIR_ . 'mymodule/mails/'
);

If the module really needs its own transport (for example a separate SMTP server), it has to bring its own mail library through Composer instead of relying on the one PrestaShop used to ship. Symfony Mailer is the natural choice, since PrestaShop 9 uses it too.

Where else it hides

grep -rn "Swift_" modules/ override/

Also check overrides of the Mail class: they copy parts of the old Swift-based code and break in the same way. And send a real test email after the upgrade (order confirmation, contact form, password reset), because a broken mail module rarely shows an error to anyone.

Find every place like this before you upgrade

Where one removed call is, there are usually more. Our free PrestaShop 9 Readiness Check reads the code of every module, the theme and the overrides and lists each problem with its file and line. It only reads, nothing is changed.

Get the free check

Prefer not to fix it yourself? We upgrade stores to PrestaShop 9 for a fixed price. See the upgrade