织梦dedecms 网站自定义表单设置好后会提交到后台,但是很多管理员不会经常注意到信息,所以我现在给大家分享一下将自定义表单信息提交到指定邮箱:
1、首先在织梦dedecms 网站后台,进入系统--系统基本参数--核心设置里面,如下图:
将红色标注的填写为自己的邮箱帐号(同一个账号哦)
2、打开网站根目录下的plus文件夹,里面有个diy.php文件(表单提交后处理文件),打开它,在85行上下有如下代码: $query = "INSERT INTO `{$diy->table}` (`id`, `ifcheck` $addvar) VALUES (NULL, 0 $addvalue); ";这个代码就是将表单数据写入数据库,那么我们就在他下方,加上发送邮件的功能(最下方有我改得diy.php代码 UTF8的! 直接复制放进去! 改下第88行的name):
$mailtitle = "用户订单信息";//邮件标题
$mailbody = "称呼:{$name}\r\n邮箱:{$email}\r\n所在城市:{$city}\r\n国家:{$guojia}\r\n电话:{$tel}\r\n类别:{$qudao}";//$name中name是该input元素的id号
$headers = $cfg_adminemail;//用我们刚才设置的管理员邮箱发送
$mailtype = 'TXT';//邮件类型为文本类型
require_once(DEDEINC.'/mail.class.php');//加载织梦的邮件库
$smtp = new smtp($cfg_smtp_server,$cfg_smtp_port,true,$cfg_smtp_usermail,$cfg_smtp_password);//创建smtp服务
$smtp->debug = false;//不起用调试
$smtp->sendmail($cfg_smtp_usermail,$cfg_webname ,$cfg_smtp_usermail, $mailtitle, $mailbody, $mailtype);//发送邮件
3、(一般都是国内服务器!不加没什么事!)加上第二步给的代码,就可以实现管理员邮箱给自己发信,内容通过id号获取,不过美国主机是限制自己给自己发邮件的,所以在第二步代码的最后一句,要将其改下:$smtp->sendmail($cfg_smtp_usermail,$cfg_webname ,$cfg_smtp_usermail, $mailtitle, $mailbody, $mailtype);//发送邮件,sendmail(收件人,网站名,发件人,邮件标题,邮件内容,邮件类型),将收件人重新定义一个,比如:
$cfg_smtp_usermail2="xxxx@qq.com";
$smtp->sendmail($cfg_smtp_usermail2,$cfg_webname ,$cfg_smtp_usermail, $mailtitle, $mailbody, $mailtype);
这样就可以收到啦。
下面是我个人的测试diy.php代码
第八十八行非常重要! 你要把你表单里面的name全部放进去! 内容才可以!
<?php
/**
*
* 自定义表单
*
* @version $Id: diy.php 1 15:38 2010年7月8日Z tianya $
* @package DedeCMS.Site
* @copyright Copyright (c) 2007 - 2010, DesDev, Inc.
* @license http://help.dedecms.com/usersguide/license.html
* @link http://www.dedecms.com
*/
require_once(dirname(__FILE__)."/../include/common.inc.php");
$diyid = isset($diyid) && is_numeric($diyid) ? $diyid : 0;
$action = isset($action) && in_array($action, array('post', 'list', 'view')) ? $action : 'post';
$id = isset($id) && is_numeric($id) ? $id : 0;
if(emptyempty($diyid))
{
showMsg('非法操作!', 'javascript:;');
exit();
}
require_once DEDEINC.'/diyform.cls.php';
$diy = new diyform($diyid);
/*----------------------------
function Post(){ }
---------------------------*/
if($action == 'post')
{
if(emptyempty($do))
{
$postform = $diy->getForm(true);
include DEDEROOT."/templets/plus/{$diy->postTemplate}";
exit();
}
elseif($do == 2)
{
$dede_fields = emptyempty($dede_fields) ? '' : trim($dede_fields);
$dede_fieldshash = emptyempty($dede_fieldshash) ? '' : trim($dede_fieldshash);
if(!emptyempty($dede_fields))
{
if($dede_fieldshash != md5($dede_fields.$cfg_cookie_encode))
{
showMsg('数据校验不对,程序返回', '-1');
exit();
}
}
$diyform = $dsql->getOne("select * from dede_diyforms where diyid='$diyid' ");
if(!is_array($diyform))
{
showmsg('自定义表单不存在', '-1');
exit();
}
$addvar = $addvalue = '';
if(!emptyempty($dede_fields))
{
$fieldarr = explode(';', $dede_fields);
if(is_array($fieldarr))
{
foreach($fieldarr as $field)
{
if($field == '') continue;
$fieldinfo = explode(',', $field);
if($fieldinfo[1] == 'textdata')
{
${$fieldinfo[0]} = FilterSearch(stripslashes(${$fieldinfo[0]}));
${$fieldinfo[0]} = addslashes(${$fieldinfo[0]});
}
else
{
${$fieldinfo[0]} = GetFieldValue(${$fieldinfo[0]}, $fieldinfo[1],0,'add','','diy', $fieldinfo[0]);
}
$addvar .= ', `'.$fieldinfo[0].'`';
$addvalue .= ", '".${$fieldinfo[0]}."'";
}
}
}
$query = "INSERT INTO `{$diy->table}` (`id`, `ifcheck` $addvar) VALUES (NULL, 0 $addvalue); ";
$mailtitle = "用户订单信息";//邮件标题
$mailbody = "称呼:{$name}\r\n邮箱:{$email}\r\n电话:{$tele}\r\n手机:{$sj}\r\n地址:{$dz}\r\n说明:{$sm}";//$name中name是该input元素的id号
$headers = $cfg_adminemail;//用我们刚才设置的管理员邮箱发送
$mailtype = 'TXT';//邮件类型为文本类型
require_once(DEDEINC.'/mail.class.php');//加载织梦的邮件库
$smtp = new smtp($cfg_smtp_server,$cfg_smtp_port,true,$cfg_smtp_usermail,$cfg_smtp_password);//创建smtp服务
$smtp->debug = false;//不起用调试
$smtp->sendmail($cfg_smtp_usermail,$cfg_webname ,$cfg_smtp_usermail, $mailtitle, $mailbody, $mailtype);//发送邮件
if($dsql->ExecuteNoneQuery($query))
{
$id = $dsql->GetLastID();
if($diy->public == 2)
{
//diy.php?action=view&diyid={$diy->diyid}&id=$id
$goto = "diy.php?action=list&diyid={$diy->diyid}";
$bkmsg = '发布成功,现在转向表单列表页...';
}
else
{
$goto = !emptyempty($cfg_cmspath) ? $cfg_cmspath : '/';
$bkmsg = '发布成功,请等待管理员处理...';
}
showmsg($bkmsg, $goto);
}
}
}
/*----------------------------
function list(){ }
---------------------------*/
else if($action == 'list')
{
if(emptyempty($diy->public))
{
showMsg('后台关闭前台浏览', 'javascript:;');
exit();
}
include_once DEDEINC.'/datalistcp.class.php';
if($diy->public == 2)
$query = "SELECT * FROM `{$diy->table}` ORDER BY id DESC";
else
$query = "SELECT * FROM `{$diy->table}` WHERE ifcheck=1 ORDER BY id DESC";
$datalist = new DataListCP();
$datalist->pageSize = 10;
$datalist->SetParameter('action', 'list');
$datalist->SetParameter('diyid', $diyid);
$datalist->SetTemplate(DEDEINC."/../templets/plus/{$diy->listTemplate}");
$datalist->SetSource($query);
$fieldlist = $diy->getFieldList();
$datalist->Display();
}
else if($action == 'view')
{
if(emptyempty($diy->public))
{
showMsg('后台关闭前台浏览' , 'javascript:;');
exit();
}
if(emptyempty($id))
{
showMsg('非法操作!未指定id', 'javascript:;');
exit();
}
if($diy->public == 2)
{
$query = "SELECT * FROM {$diy->table} WHERE id='$id' ";
}
else
{
$query = "SELECT * FROM {$diy->table} WHERE id='$id' AND ifcheck=1";
}
$row = $dsql->GetOne($query);
if(!is_array($row))
{
showmsg('你访问的记录不存在或未经审核', '-1');
exit();
}
$fieldlist = $diy->getFieldList();
include DEDEROOT."/templets/plus/{$diy->viewTemplate}";
}
(责任编辑:最模板) |