Hyperlink URL in email sent from site

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';

$headers  = 

'MIME-Version: 1.0'."\r\n".

'Content-type: text/html; charset=utf-8'."\r\n".

'From: "WAP Service" <wap-service@example.com>'."\r\n".

'Reply-To: wap-service@example.com'."\r\n" .

'X-Mailer: PHP/'.phpversion();


$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.

Here’s one:


<?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.

@jacmoe,

gr8, +1 for ans.