php - Yii2 Swiftmailer can't send image and font awesome icons
Get the solution ↓↓↓Mu Controller Code:
if ($model->load(Yii::$app->request->post())) {
$data = $this->renderPartial('receivers/contact_form_output_quick_query', [
'model' => $model,
'formData' => $_REQUEST], true);
$receiver_message = $this->renderPartial('receivers/receiver_message_quick_query',['data' => $data]);
$message = Yii::$app->mailer->compose();
$message->setFrom('[email protected]')
->setTo('[email protected]')
->setSubject('Subject Of email')
->setHtmlBody($data)
->send();
$message = Yii::$app->mailer->compose();
$message->setFrom('[email protected]')
->setTo($model->email)
->setSubject('Reply: Online Query')
->setHtmlBody($receiver_message)
->send();
Yii::$app->session->setFlash('success', 'Congratulations! Your message has been successfully Sent.');
}
Will some one tells me what is the main reason that image and Font Awesome icons are not sending properly?
My receiver message partial view goes here:
<div>
<table style="width: 600px;">
<tr>
<td>Thanks for your interest in our services. We have received your online query.</td>
</tr>
<tr>
<td>Thanks again for contacting us.</td>
</tr>
<tr>
<td style="height: 5px;"></td>
</tr>
<tr>
<td>(It may take some time depending upon the number of queries in the pipeline. We appreciate your cooperation in this regard.)</td>
</tr>
<tr>
<td>Regards!</td>
</tr>
</table>
<?= $this->render('signature'); ?>
<table>
<tr>
<td><strong style="font-size:16px;">Query Details that we received:</strong></td>
</tr>
<tr>
<td><?= $data; ?></td>
</tr>
</table>
</div>
The above is the render partial view for the receiver message:
My signature partial view is:
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<table>
<tr>
<td colspan="3">
<img src="<?= $this->theme->baseUrl?>/images/email_signature.png">
</td>
</tr>
<tr>
<td style="height: 30px;"></td>
</tr>
<tr>
<td>+123456778</td>
<td colspan="2"> | +123456778</td>
</tr>
<tr>
<td>http://www.example.com</td>
<td>| [email protected] </td>
<td>| <b>Skype:</b> skypeid</td>
</tr>
<tr>
<td><a target="_blank" href="https://www.linkedin.com"><i class="fa fa-linkedin"></i> Linked In | </a></td>
<td><a target="_blank" href="https://www.facebook.com"><i class="fa fa-facebook"></i> Facebook | </a></td>
<td><a target="_blank" href="https://twitter.com"><i class="fa fa-twitter"></i> https://www.twitter.com</a></td>
</tr>
The above is the render partial view for signature containing image and font awesome icons main problem is in this file.
Answer
Solution:
Images
$this->theme->baseUrl
will return path to theme, but without domain and scheme part (for example it will return/themes/my-theme
). When you're generating URLs for mailing, you need to full URL with scheme and domain - you should generate URLs like this:
<img src="<?= Yii::$app->request->getHostInfo() . $this->theme->getBaseUrl() ?>/images/email_signature.png">
Font Awesome Icons
I have a bad news for you - many clients (especially webmails) does not support webfonts, so you will not be able to use Font Awesome icons in this way. Support for SVG is even worse, so this is not a solution either. You should probably stick to good old PNGs as icons.
Unfortunately working with mailing is like a time travel - you should build your emails in the same way you created your websites 10-20 years ago. Forget about fancy CSS3 features - the most bulletproof templates are still built using tables with inline CSS everywhere...
Answer
Solution:
Not every mail client supports webfonts. So probably best for you is to download it as images, save it in your public assets folder and invoke it withimg
element tag.
Answer
Solution:
if I am not wrong you need to useembed()
orattach()
for showing inline images in the email.
$Imageid = $message->attach(Swift_Attachment::fromPath('path/to/image')
->setDisposition('inline'));
and then use the$cid
for showing the image inside the email text where ever you are assigning the text for the email
<img src="' . $Imageid . '" alt="Image" />'
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: php undefined array key
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.