PHP空白电子邮件 [英] PHP Blank Email

查看:161
本文介绍了PHP空白电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过PHP验证一个表单。问题在于,发送时一切都OK,但收到的电子邮件为空。这里是更多解释的代码:



形式:

 < div class =status alert alert-successstyle =display:none>< / div> 

< form id =main-contact-formclass =contact-formname =contact-formmethod =POSTaction =sendemail.phprole =form >
< div class =row>
< div class =col-sm-6>
< div class =form-group>
< input type =textname =nameclass =form-controlrequired =requiredplaceholder =Name>
< / div>
< / div>
< div class =col-sm-6>
< div class =form-group>
< input type =textclass =form-controlpattern =[^ @] * @ [^ @] *name =emailrequired =requiredplaceholder =Email address >
< / div>
< / div>
< / div>
< div class =row>
< div class =col-sm-12>
< div class =form-group>
< input id =form-emailclass =form-controltype =textname =subjectsize =64maxlength =64placeholder =Subjectrequired />
< / div>
< / div>
< div class =col-sm-12>
< div class =form-group>
< textarea name =bodyid =messagerequired =requiredclass =form-controlrows =8placeholder =Message>< / textarea>
< / div>
< div class =form-group>
< button type =submitname =sendclass =btn btn-danger btn-lg>发送消息< / button>
< / div>
< / div>
< / div>
< / form>

PHP文件(sendemail.php):

 <?php 
header('Content-type:application / json');
$ status = array(
'type'=>'success',
'message'=&''发送电子邮件'
);

$ recipient ='my_email@gmail.com';
$ email = $ _POST ['email'];
$ name = $ _POST ['name'];
$ subject = $ _POST ['subject'];
$ body = $ _POST ['body'];

$ body ='Name:'。 $名称。 \\\
\\\
。 '电邮:'。 $电子邮件。 \\\
\\\
。 '学科: ' 。 $主题。 \\\
\\\
。 '信息: ' 。 $身体;

$ success = mail($ recipient,$ subject,$ body,'From:<'。$ email。'>');

echo json_encode($ status);

?>

在Gmail帐户中收到的电子邮件:

 名称:

电子邮件:



主题:



消息:



空白电子邮件。



任何建议/解决方案将非常感谢!



添加了ajax内容(忘记添加):

  // Ajax contact 
var form = $('。contact-form );
form.submit(function(){
$ this = $(this);
$ .post($(this).attr('action'),function(data){
$ this.prev()。text(data.message).fadeIn()。delay(3000).fadeOut();
},'json');
return false;
});


解决方案

  header 'content-type:application / json');`
$ status = array();

$ recipient ='my_email@gmail.com';
$ email = $ _POST ['email'];
$ name = $ _POST ['name'];
$ subject = $ _POST ['subject'];
$ body = $ _POST ['body'];

$ body ='Name:'。 $名称。 \\\
\\\
。 '电邮:'。 $电子邮件。 \\\
\\\
。 '学科: ' 。 $主题。 \\\
\\\
。 '信息: ' 。 $身体;

$ success = mail($ recipient,$ subject,$ body);
if($ success)
{
$ status ['type'] ='success';
$ status ['message'] ='发送';
}
else
{
$ status ['type'] ='failed';
$ status = $ _ POST;
}

echo json_encode($ status);

`


I'm making a form that validates through PHP. The problem is that all goes ok when sending, but the email received is blank. Here it is the code for more explanation:

The form:

<div class="status alert alert-success" style="display: none"></div>

<form id="main-contact-form" class="contact-form" name="contact-form" method="POST" action="sendemail.php" role="form">
    <div class="row">
        <div class="col-sm-6">
            <div class="form-group">
                <input type="text" name="name" class="form-control" required="required" placeholder="Name">
            </div>
        </div>
        <div class="col-sm-6">
            <div class="form-group">
                <input type="text" class="form-control" pattern="[^ @]*@[^ @]*" name="email" required="required" placeholder="Email address">
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12">
            <div class="form-group">
            <input id="form-email" class="form-control" type="text" name="subject" size="64" maxlength="64" placeholder="Subject" required />
            </div>
        </div>
        <div class="col-sm-12">
            <div class="form-group">
                <textarea name="body" id="message" required="required" class="form-control" rows="8" placeholder="Message"></textarea>
            </div>
            <div class="form-group">
                <button type="submit" name="send" class="btn btn-danger btn-lg">Send Message</button>
            </div>
        </div>
    </div>
</form>

The PHP file (sendemail.php):

<?php
header('Content-type: application/json');
$status = array(
    'type'=>'success',
    'message'=>'Email sent!'
);

$recipient = 'my_email@gmail.com';
$email = $_POST['email']; 
$name = $_POST['name']; 
$subject = $_POST['subject']; 
$body = $_POST['body']; 

$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $body;

$success = mail($recipient, $subject, $body, 'From: <'.$email.'>');

echo json_encode($status);
die;
?>

The email recivied on gmail account:

Name:

Email:

Subject:

Message:

Blank Email.

Any suggestions/solutions would be much appreciated!!

Added ajax content (forgot to add it):

    //Ajax contact
var form = $('.contact-form');
form.submit(function () {
    $this = $(this);
    $.post($(this).attr('action'), function(data) {
        $this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
    },'json');
    return false;
});

解决方案

header('Content-type: application/json');`
$status = array();

$recipient = 'my_email@gmail.com';
$email = $_POST['email']; 
$name = $_POST['name']; 
$subject = $_POST['subject']; 
$body = $_POST['body']; 

$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $body;

$success = mail($recipient, $subject, $body);
if($success)
{
$status['type']='success';
$status['message']='Send';
}
else 
{
$status['type']='failed';
$status=$_POST;
}

echo json_encode($status);
die;

`

这篇关于PHP空白电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆