从网站上的表单发送的电子邮件中未显示日期

时间:2013-12-12 13:17:40

标签: javascript php forms email date

我的网站上有一个表格完美无缺,但我发现日期没有显示在我从表格收到的电子邮件中。当检查我的MailHandler.php时,我发现$ header中没有Date的部分,所以我添加了一个,但是我显然做错了,因为日期仍然没有显示。拜托,请问有人应该如何写这些文章来帮助我吗?

提前谢谢!

<?php
$owner_email = $_POST["owner_email"];
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= 'From:' . $_POST["email"] . "\r\n";
$headers .= 'Datum:' . $_POST["date"] . "\r\n";
$subject = 'From your website  -   ' . $_POST["name"];
$messageBody = "";

if($_POST['name']!='nope'){
    $messageBody .= '<p>Name: ' . $_POST["name"] . '</p>' . "\n";
    $messageBody .= '<br>' . "\n";
}
if($_POST['email']!='nope'){
    $messageBody .= '<p>Email: ' . $_POST['email'] . '</p>' . "\n";
    $messageBody .= '<br>' . "\n";
}else{
    $headers = '';
}
if($_POST['state']!='nope'){        
    $messageBody .= '<p>Address: ' . $_POST['state'] . '</p>' . "\n";
    $messageBody .= '<br>' . "\n";
}
if($_POST['phone']!='nope'){        
    $messageBody .= '<p>Phone: ' . $_POST['phone'] . '</p>' . "\n";
    $messageBody .= '<br>' . "\n";
}   
if($_POST['fax']!='nope'){      
    $messageBody .= '<p>Date: ' . $_POST['date'] . '</p>' . "\n";
    $messageBody .= '<br>' . "\n";
}
if($_POST['fax']!='nope'){      
    $messageBody .= '<p>Fax: ' . $_POST['no'] . '</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";
}
?>

这是MailHandler.ashx:

<?php
$owner_email = $_POST["owner_email"];
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= 'From:' . $_POST["email"] . "\r\n";
$headers .= 'Date:' . $_POST["date"] . "\r\n";
$subject = 'Via Marietorps hemsida  -   ' . $_POST["name"];
$messageBody = "";

if($_POST['name']!='nope'){
    $messageBody .= '<p>Namn: ' . $_POST["name"] . '</p>' . "\n";
    $messageBody .= '<br>' . "\n";
}
if($_POST['email']!='nope'){
    $messageBody .= '<p>Email: ' . $_POST['email'] . '</p>' . "\n";
    $messageBody .= '<br>' . "\n";
}else{
    $headers = '';
}
if($_POST['state']!='nope'){        
    $messageBody .= '<p>Leveransadress: ' . $_POST['state'] . '</p>' . "\n";
    $messageBody .= '<br>' . "\n";
}
if($_POST['phone']!='nope'){        
    $messageBody .= '<p>Telefonnummer: ' . $_POST['phone'] . '</p>' . "\n";
    $messageBody .= '<br>' . "\n";
}   
if($_POST['date']!='nope'){     
    $messageBody .= '<p>Datum: ' . $_POST['date'] . '</p>' . "\n";
    $messageBody .= '<br>' . "\n";
}
if($_POST['message']!='nope'){
    $messageBody .= '<p>Bestallning: ' . $_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";
}

&GT;

这是form.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',
                    notRequiredCl:'notRequired',
                    successCl:'success',
                    successShow:'4000',
                    mailHandlerURL:'bin/MailHandler.php',
                    ownerEmail:'info@mydomain.se',
                    stripHTML:true,
                    smtpMailServer:'localhost',
                    targets:'input,textarea',
                    controls:'a[data-type=reset],a[data-type=submit]',
                    validate:true,
                    rx:{
                        ".name":{rx:/^[a-öA-Ö'][a-öA-Ö-' ]+[a-öA-Ö']?$/,target:'input'},
                        ".state":{rx:/.\+?[0-9a-öA-Ö'][0-9a-öA-Ö-' ]+[0-9a-öA-Ö']?$/,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'},
                        ".fax":{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(){
                                    _.validateFu(label)
                                    if(_.isEmpty(label))
                                        inp.val(defVal)
                                        ,_.hideErrorFu(label.removeClass(_.invalidCl))                                          
                                })
                                .bind('keyup',function(){
                                    label.hasClass(_.invalidCl)
                                        &&_.validateFu(label)
                                })
                            label.find('.'+_.errorCl+',.'+_.emptyCl).css({display:'block'}).hide()
                        })
                        _.success=$('.'+_.successCl,_.form).hide()
                    },
                    isRequired:function(el){                            
                        return !el.hasClass(_.notRequiredCl)
                    },
                    isValid:function(el){                           
                        var ret=true
                        $.each(_.rx,function(k,d){
                            if(el.is(k))
                                ret=d.rx.test(el.find(d.target).val())                                      
                        })
                        return ret                          
                    },
                    isEmpty:function(el){
                        var tmp
                        return (tmp=el.find(_.targets).val())==''||tmp==el.data('defVal')
                    },
                    validateFu:function(el){                            
                        el.each(function(){
                            var th=$(this)
                                ,req=_.isRequired(th)
                                ,empty=_.isEmpty(th)
                                ,valid=_.isValid(th)                                

                            if(empty&&req)
                                _.showEmptyFu(th.addClass(_.invalidCl))
                            else
                                _.hideEmptyFu(th.removeClass(_.invalidCl))

                            if(!empty)
                                if(valid)
                                    _.hideErrorFu(th.removeClass(_.invalidCl))
                                else
                                    _.showErrorFu(th.addClass(_.invalidCl))                             
                        })
                    },
                    getValFromLabel:function(label){
                        var val=$('input,textarea',label).val()
                            ,defVal=label.data('defVal')                                
                        return label.length?val==defVal?'nope':val:'nope'
                    }
                    ,submitFu:function(){
                        _.validateFu(_.labels)                          
                        if(!_.form.has('.'+_.invalidCl).length)
                            $.ajax({
                                type: "POST",
                                url:_.mailHandlerURL,
                                data:{
                                    name:_.getValFromLabel($('.name',_.form)),
                                    email:_.getValFromLabel($('.email',_.form)),
                                    phone:_.getValFromLabel($('.phone',_.form)),
                                    fax:_.getValFromLabel($('.fax',_.form)),
                                    state:_.getValFromLabel($('.state',_.form)),
                                    message:_.getValFromLabel($('.message',_.form)),
                                    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的)

2 个答案:

答案 0 :(得分:2)

你的第一个:

if($_POST['fax']!='nope')

应该是

if($_POST['date']!='nope')

答案 1 :(得分:0)

您不需要使用post来发送日期,只需使用php date()函数指定日期并将其打印到标题

php日期功能依赖于时区,因此您需要设置时区然后调用它。 You can find timezones here。它接受两个参数,第一个是格式,第二个是时间,如果省略则默认为当前时间。下面函数中的“r”将时间格式设置为“RFC 2822格式化日期”。查看日期()here

的PHP手册
    $yourTimezone = new DateTimeZone('your_timezone');
    $date = date('r'),

然后在你的标题数组中

   $headers .= 'Date:' . $date . "\r\n";