Search code examples
phpswiftimageemailswiftmailer

Image doesn't show when I send an email / swiftmailer


I am currently using Swiftmailer to send my emails. Everything works fine it is sending the emails however the emails are not showing the Logo I have put in. Here is the code:

require_once 'vendor/autoload.php';
require_once 'config/constants.php';

// Create the Transport
    $transport = (new Swift_SmtpTransport('smtp.gmail.com', 465, 'ssl'))
        ->setUsername(EMAIL)
        ->setPassword(PASSWORD);

// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);


function sendVerificationEmail($userEmail, $token){
    global $mailer;
    $body = '<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Verify Email</title>
</head>
<body>
<div class="not-verified-container">
    <img src="logo.svg" class="mazaw-logo-not-verified">
    <div class="container-without-logo">
        <div class="not-verified-texts">
            <h2>Account Verification</h2>
        </div>
    </div>
</div>
</body>
</html>';
    // Create a message
    $message = (new Swift_Message('Account Verification'))
        ->setFrom(EMAIL)
        ->setTo($userEmail)
        ->setBody($body, 'text/html');
// Send the message
    $result = $mailer->send($message);
}

I would be very very thankful if someone could take a look and see what I have done wrong. Thank you!


Solution

  • Embedding should work fine

    <?php    
    $message = new Swift_Message('Your subject');
    $message->setBody(
    '<html>' .
    ' <body>' .
    '  <img src="' . $message->embed(Swift_Image::fromPath('image.png')) . '" />' .
    ' </body>' .
    '</html>',
    'text/html'
    );    
    ?>