I send an email through my controller and set the body content via a view file as follows:
$body=$this->renderPartial('//mail/newEnquiry', array('model'=>$model), true);
I have the following in my email view file:
<?php echo CHtml::link('View this Enquiry now', array('admin/enquiry/view', 'id'=>$model->id)); ?>
When the email is received on the client the hyperlink address is just ‘/admin/enquiry/view/id/32’
It needs to include the full URL (http://www.mysite.com/…). I thought it would do this automatically but that does not seem to be the case…
This is because e-mails sent by PHP mail() function are plain-text encoded by default.
You have to provide extra (fourth) parameter to mail() function with proper headers definitions. I do this like this:
$subject = 'Day Report for '.date('l, jS F Y');
$message = 'message';
'Content-type: text/html; charset=utf-8'."\r\n".
'From: "WAP Service" <email@example.com>'."\r\n".
'Reply-To: firstname.lastname@example.org'."\r\n" .
$result = mail($to, $subject, $message, $headers);
And it works fine for me. Not only links but all HTML tags are perfectly inserted into generated e-mail.
No, that isn’t the problem - I have already put in the HTML headers! The URL is generated but it’s ‘href’ attribute is just ‘/admin/enquiry/view/id/32’ !
You can do this in several ways.
<?php echo CHtml::link('View this Enquiry now',
Yii::app()->createAbsoluteUrl('admin/enquiry/view', 'id'=>$model->id)); ?>
Thanks, this has worked. By the way the GET parameters need to be an array.