javascript 如何使 MailHandler.php 在 Wordpress 中工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12417734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make MailHandler.php work in Wordpress?
提问by Niccolo
I'm trying to make MailHandler.phpto work with Wordpress. I have tried a dozen of solutions but non of them worked. Here are the workarounds that I did with the "forms.js" file I've tried:
我正在尝试使MailHandler.php与 Wordpress 一起使用。我尝试了十几种解决方案,但没有一个奏效。以下是我尝试过的“ forms.js”文件的解决方法:
1.) Change "bin/MailHandler.php" to "mail/mailhandler.php"
1.) 将“ bin/MailHandler.php”改为“mail/mailhandler.php”
- ) Here are some original codes that I have changed too:
- ) 以下是我也更改过的一些原始代码:
mailHandlerURL:'../mail/mailhandler.php'was changed to
mailHandlerURL:'../mail/mailhandler.php'改为
mailHandlerURL:'http://www.mysite.net/wp-content/themes/mysite/mail/mailhandler.php',
mailHandlerURL:'http://www.mysite.net/wp-content/themes/mysite/mail/mailhandler.php',
THEN ALSO TRIED:
然后还尝试:
mailHandlerURL:'<?php echo get_template_directory_uri(); ?>
/mail/mailhandler.php',
mailHandlerURL:' <?php echo get_template_directory_uri(); ?>
/mail/mailhandler.php',
In the contact_template.phpis where my form is located. Here are some things I tried for the form action and none of them worked:
在contact_template.php 中是我的表单所在的位置。以下是我为表单操作尝试的一些方法,但都没有奏效:
**<form action="../mail/mailhandler.php" id="contact-form">** Changed to
**<form action="http://www.mysite.net/wp-content/themes/mysite/mail/mailhandler.php" id="contact-form">**
AND
和
<form action="<?php echo get_template_directory_uri(); ?>/mail/mailhandler.php" id="contact-form">
<form action="<?php echo get_template_directory_uri(); ?>/mail/mailhandler.php" id="contact-form">
Please help.
请帮忙。
By the way my smtpMailServer is set to localhostinside my forms.js.
顺便说一下,我的smtpMailServer在我的 forms.js 中设置为 localhost。
Thanks in advance.
提前致谢。
ADDITIONAL INFO
附加信息
Below are the codes of my mailhandler.php
下面是我的mailhandler.php的代码
<?php
$owner_email = $_POST["[email protected]"]; /*/originally owner_email/*/
$headers = 'From:' . $_POST["email"];
$subject = 'A message from your site visitor ' . $_POST["name"];
$messageBody = "";
if($_POST['name']!='nope'){
$messageBody .= '<p>Visitor: ' . $_POST["name"] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['email']!='nope'){
$messageBody .= '<p>Email Address: ' . $_POST['email'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['phone']!='nope'){
$messageBody .= '<p>Phone Number: ' . $_POST['phone'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['message']!='nope'){
$messageBody .= '<p>Message: ' . $_POST['message'] . '</p>' . "\n";
}
if($_POST["stripHTML"] == 'true'){
$messageBody = strip_tags($messageBody);
}
try{
if(!mail($owner_email, $subject, $messageBody, $headers)){
throw new Exception('mail failed');
}else{
echo 'mail sent';
}
}catch(Exception $e){
echo $e->getMessage() ."\n";
}
?>
?>
Here is what inside forms.js:
这是forms.js里面的内容:
(function($){
$.fn.extend({
forms:function(opt){
if(opt===undefined)
opt={}
this.each(function(){
var th=$(this),
data=th.data('forms'),
_={
errorCl:'error',
emptyCl:'empty',
invalidCl:'invalid',
successCl:'success',
successShow:'4000',
mailHandlerURL:'http://www.mysite.com/wp-content/themes/mysite/mail/mailhandler.php',
ownerEmail:'[email protected]',
stripHTML:true,
smtpMailServer:'localhost',
targets:'input,textarea',
controls:'a[data-type=reset],a[data-type=submit]',
validate:true,
rx:{
".name":{rx:/^[a-zA-Z'][a-zA-Z-' ]+[a-zA-Z']?$/,target:'input'},
".email":{rx:/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i,target:'input'},
".phone":{rx:/^\+?(\d[\d\-\+\(\) ]{5,}\d$)/,target:'input'},
".message":{rx:/.{20}/,target:'textarea'}
},
preFu:function(){
_.labels.each(function(){
var label=$(this),
inp=$(_.targets,this),
defVal=inp.val(),
trueVal=(function(){
var tmp=inp.is('input')?(tmp=label.html().match(/value=['"](.+?)['"].+/),!!tmp&&!!tmp[1]&&tmp[1]):inp.html()
return defVal==''?defVal:tmp
})()
trueVal!=defVal
&&inp.val(defVal=trueVal||defVal)
label.data({defVal:defVal})
inp
.bind('focus',function(){
inp.val()==defVal
&&(inp.val(''),_.hideEmptyFu(label),label.removeClass(_.invalidCl))
})
.bind('blur',function(){
!inp.val()
?inp.val(defVal)
:(_.isValid(label)
?_.showErrorFu(label)
:_.hideErrorFu(label)),
(_.isEmpty(label)
?_.showEmptyFu(label)
:_.hideEmptyFu(label))
})
.bind('keyup',function(){
label.hasClass(_.invalidCl)
&&_.isValid(label)
?_.showErrorFu(label)
:_.hideErrorFu(label)
})
label.find('.'+_.errorCl+',.'+_.emptyCl).css({display:'block'}).hide()
})
_.success=$('.'+_.successCl,_.form).hide()
},
isValid:function(el){
var ret=true,
empt=_.isEmpty(el)
if(empt)
ret=false,
el.addClass(_.invalidCl)
else
$.each(_.rx,function(k,d){
if(el.is(k))
d.rx.test(el.find(d.target).val())
?(el.removeClass(_.invalidCl),ret=false)
:el.addClass(_.invalidCl)
})
return ret
},
isEmpty:function(el){
var tmp
return (tmp=el.find(_.targets).val())==''||tmp==el.data('defVal')
},
validateFu:function(){
_.labels.each(function(){
var th=$(this)
_.isEmpty(th)
?_.showEmptyFu(th)
:_.hideEmptyFu(th)
_.isValid(th)
?_.showErrorFu(th)
:_.hideErrorFu(th)
})
},
submitFu:function(){
_.validateFu()
if(!_.form.has('.'+_.invalidCl).length)
$.ajax({
type: "POST",
url:_.mailHandlerURL,
data:{
name:$('.name input',_.form).val()||'nope',
email:$('.email input',_.form).val()||'nope',
phone:$('.phone input',_.form).val()||'nope',
fax:$('.fax input',_.form).val()||'nope',
state:$('.state input',_.form).val()||'nope',
message:$('.message textarea',_.form).val()||'nope',
owner_email:_.ownerEmail,
stripHTML:_.stripHTML
},
success: function(){
_.showFu()
}
})
},
showFu:function(){
_.success.slideDown(function(){
setTimeout(function(){
_.success.slideUp()
_.form.trigger('reset')
},_.successShow)
})
},
controlsFu:function(){
$(_.controls,_.form).each(function(){
var th=$(this)
th
.bind('click',function(){
_.form.trigger(th.data('type'))
return false
})
})
},
showErrorFu:function(label){
label.find('.'+_.errorCl).slideDown()
},
hideErrorFu:function(label){
label.find('.'+_.errorCl).slideUp()
},
showEmptyFu:function(label){
label.find('.'+_.emptyCl).slideDown()
_.hideErrorFu(label)
},
hideEmptyFu:function(label){
label.find('.'+_.emptyCl).slideUp()
},
init:function(){
_.form=this
_.labels=$('label',_.form)
_.preFu()
_.controlsFu()
_.form
.bind('submit',function(){
if(_.validate)
_.submitFu()
else
_.form[0].submit()
return false
})
.bind('reset',function(){
_.labels.removeClass(_.invalidCl)
_.labels.each(function(){
var th=$(this)
_.hideErrorFu(th)
_.hideEmptyFu(th)
})
})
_.form.trigger('reset')
}
}
if(!data)
(typeof opt=='object'?$.extend(_,opt):_).init.call(th),
th.data({cScroll:_}),
data=_
else
_=typeof opt=='object'?$.extend(data,opt):data
})
return this
}
})
})(jQuery)
$(document).ready(function(){
$('#contact-form').forms({
ownerEmail:'[email protected]'
})
});
采纳答案by lc.
This line $owner_email = $_POST["[email protected]"]; /*/originally owner_email/*/
is funny. I'm pretty sure that POST variable won't exist.
这条线$owner_email = $_POST["[email protected]"]; /*/originally owner_email/*/
很有趣。我很确定 POST 变量不会存在。
Try $owner_email = $_POST["owner_email"];
.
试试$owner_email = $_POST["owner_email"];
。
Or did you actually mean to statically/permanently change the value of $owner_email
to "[email protected]"? If so you want to set it to the string, $owner_email = "[email protected]";
, and NOT the POST variable called "[email protected]"...
或者您实际上是想静态/永久地将 的值更改$owner_email
为“[email protected]”?如果是这样,您想将其设置为字符串$owner_email = "[email protected]";
,而不是名为“[email protected]”的 POST 变量...
回答by Gima3000
Gima says: you most do this to get it work:
Gima 说:你最常这样做是为了让它工作:
Forget what someone said before, don't change anything in the code.
忘记之前有人说过的话,不要更改代码中的任何内容。
1) Insert the folder mail with MailHandler.php & MailHandler.ashx in your themes/name-theme folder.
1) 在您的主题/名称-主题文件夹中插入带有 MailHandler.php 和 MailHandler.ashx 的文件夹邮件。
2) Insert the forms.js in to your themes//name-theme folder inside the js folder.
2) 将 forms.js 插入到 js 文件夹内的主题//name-theme 文件夹中。
After that do the following:
之后执行以下操作:
1) in your HTML form page you must write this: <form id="newsletter-form" action="<?php echo get_template_directory_uri(); ?>/mail/MailHandler.php" >
1) 在您的 HTML 表单页面中,您必须这样写: <form id="newsletter-form" action="<?php echo get_template_directory_uri(); ?>/mail/MailHandler.php" >
2) in your forms.js you must write this on mailHandlerURL line 16:
2) 在你的 forms.js 中,你必须在 mailHandlerURL 第 16 行写下这个:
mailHandlerURL:'http://your-site.com/your-blog/wp-content/themes/theme-name/mail/MailHandler.php',
NOTE: "the http address abobe is without www"
注意:“abobe 的 http 地址没有 www”
3) don't forget to call your script from your header.php like this:
3)不要忘记像这样从 header.php 调用你的脚本:
<script src="<?php echo get_template_directory_uri(); ?>/js/forms.js"></script>
4)Enjoy the results......
4)享受结果......