最近搞房源举报功能,截图如下:
CI框架有发送邮件类:具体的可以看手册,关于邮件的配置文件,我放在单独的文件里,方便以后维护
文件名为:email.php,这个文件保存到application/config/email.php,然后在控制器里加载$this->load->library('email'),这样就不需要使用$this->email->initialize()函数来初始化参数了
<?php
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.163.com';
$config['smtp_user'] = 'email地址';
$config['smtp_pass'] = '*******';
$config['mailtype'] = 'html';
$config['validate'] = true;
$config['priority'] = 1;
$config['crlf'] = "\r\n";
$config['smtp_port'] = 25;
$config['charset'] = 'gb2312';
$config['wordwrap'] = TRUE;
视图关键代码:
<form action="<?php echo base_url();?>content/sendMail.html" method="post">
<input type="hidden" name="number" value="<?php echo $this->uri->segment(3);?>" />
<input type="hidden" name="url" value="<?php echo $_SERVER['PHP_SELF'];?>" />
<table>
<tr>
<td colspan="2">您要投诉的帖子,编号<span style="color:red;"><?php printf('ZB%07s',$this->uri->segment(3));?></span></td>
</tr>
<tr>
<td><input name="warn" type="radio" value="1" checked="checked"/> 中介冒充个人</td>
<td><input name="warn" type="radio" value="2" /> 联系电话虚假</td>
</tr>
<tr>
<td><input name="warn" type="radio" value="3" /> 虚假、违法信息</td>
<td> </td>
</tr>
<tr>
<td colspan="2">
<textarea name="cont" cols="" rows="" style="width:280px;height:70px;"></textarea>
</td>
</tr>
<tr>
<td><input name="complaint" type="submit" value="投诉" /></td>
<td> </td>
</tr>
</table>
</form>
控制器代码如下:
function sendMail(){
$url='http://zufang.0551fangchan.com'.$this->input->post('url',true);
$num=$this->input->post('number',true);
$this->load->library('email');
$warn=$this->input->post('warn',true);
$content=$this->input->post('cont',true);
if($warn===1){
$type='中介冒充个人';
}elseif($warn===2){
$type='联系电话虚假';
}elseif($warn===3){
$type='虚假、违法信息';
}
$title="HouseID:{$num} have a problem!";
$message="网友举报编号为:<span style='color:red'>{$num}</span>的房源:<br />原因是:<span style='color:red'>{$type}</span><br />举报内容:{$content}<br />链接如下:<a href=\"{$url}\" target='_blank'><span style='color:red'>点击查看举报房源</span></a> ";
$this->email->from('services0551@163.com', '网友');
$this->email->to('442700091@qq.com');
$this->email->subject($title);
$this->email->message($message);
if (!$this->email->send()){
echo '发送邮件失败!';//这个地方可以加些跳转效果
redirect($url);
}else{
echo '发送邮件成功';//这个地方可以加些跳转效果
redirect($url);
}
}