im using this to send a email via yii2... problem is // Strip HTML and PHP tags from a…
- User
im using this to send a email via yii2... problem is
// Strip HTML and PHP tags from a string @url https://www.php.net/manual/en/function.strip-tags.php $stripReply = strip_tags($this->reply, '<p><a><i><b><u>'); $sanitizedReply = preg_replace('/\bhttps?:\/\/\S+/i', '', $stripReply);// No links. $isSent = \Yii::$app->mailer->compose( ['html' => 'emailReply-html', 'text' => 'emailReply-text'], ['contact' => $contact], ['sanitizedReply' => $sanitizedReply], ) ->setFrom($this->_emailSupport) ->setTo($contact->email) ->setSubject('Re: ' . $contact->subject) //->setTextBody($sanitizedReply) ->send();the sanitizedReply isn't working.. i get a error when trying to use it.
- Assistant
Based on the provided code snippet, it seems that you need to pass the
sanitizedReplyvariable correctly to your email template. The issue likely lies in how the data is being passed to thecomposemethod.Correcting the Issue
- User
it doesnt even make sense.. error im getting is from yii2 base view.
- Assistant
The error you're encountering from
yii\base\Viewusually indicates an issue with how data is being accessed or managed within your view files (email templates, in this case). Given the situation, there are a few steps you can take to diagnose and potentially resolve the issue: - User
whats the best practices for writing code for yii2 mail
- Assistant
- User
i meant for the template files
- Assistant
- User
write me a html and text sample
- Assistant
Certainly! Here is a basic example of an HTML and corresponding plain text email template that you can use in Yii2:
HTML Template (email-html.php)
<?php /** * @var $this \yii\web\View * @var $username string * @var $content string */ ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Your Email Subject</title> <style> /* Add any styles here, but remember they'll need to be inline if you want full compatibility */ </style> </head> <body style="margin: 0; padding: 0; font-family: Arial, sans-serif; font-size: 16px; color: #333;"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td style="padding: 20px;"> <h1 style="color: #555;">Hello, <?= htmlspecialchars($username) ?>!</h1> <p> <?= nl2br(htmlspecialchars($content)) ?> </p> <p> <a href="https://example.com" style="color: #1a73e8; text-decoration: none;">Visit our site</a> </p> <p> Best Regards,<br> Your Company Name </p> </td> </tr> </table> </body> </html> - User
for a yii2 app
- Assistant