SAPNET
SAPNET

Reputation: 351

Laravel 4.2 does not send email with image tags in the email body

The following code is working fine:

Mail::send([], [], function ($message) {
    $message->to('[email protected]')
    ->subject('Test Subject')
    ->setBody('<h1>Hi, welcome user!</h1>', 'text/html');
});

// check for failures
echo Mail::failures() ? 'Failed to send' : 'Sent';

However, when I added the image tag to the email's body as follows:

Mail::send([], [], function ($message) {
    $message->to('[email protected]')
    ->subject('Test Subject')
    ->setBody('<h1>Hi, welcome user!</h1> <img src="http://absolute_path_to_image">', 'text/html'); 
});
// check for failures
echo Mail::failures() ? 'Failed to send' : 'Sent';

The snippet returns "Sent" without any error message. I checked the log files but found nothing. There was no information in the log file or whatsoever.

The application is under a sub-dommain with SSL key enabled such as https://secure.example.com.

Note that the app used to work on the same server but under a different subdomain like https://secure.olddomain.com. I'm not sure it is related to this.

If you have experienced this problem before, please help and share your solution.

Thanks, SAPNET

Upvotes: 1

Views: 381

Answers (2)

Hiren Gohel
Hiren Gohel

Reputation: 5042

This one worked for me: The image is declared in the email view

<img src="{{ $message->embed(public_path() . '/x/x.png') }}" alt="" />

from Laravel doc "A $message variable is always passed to e-mail views, and allows the inline embedding of attachments."

Hope this helps you!

Upvotes: 1

If you send image file in mail body you need to embed this like below

\Mail::send([], [], function ($message) {
        $message->to('[email protected]')
        ->subject('Test Subject')
        ->setBody('<h1>Hi, welcome user!</h1> <img src="'. $message->embed('absolute_path_to_image') .'"> ', 'text/html'); 
    });

More About

Upvotes: 1

Related Questions