PHP通过响应在同一页面上发送电子邮件

时间:2014-11-17 22:32:11

标签: php html email post

代码执行但我没有收到任何错误消息,也没有成功消息,为什么这不起作用?

<?php 

if(isset($_POST['action'])=='submitfunc') {
    submitfunc(); 
}
else {} //show form $fromeml = "From:".$_POST['demo-email'].' \r\n';

if(isset($_POST['demo-copy'])==true){
    $ccemail = "Cc:".$_POST['demo-email'].' \r\n'; 
}
else {
    $ccemail= "Cc: \r\n";
} 

function submitfunc() {

    $to = "me@mywebsite.net";    
    $subject = "This is subject";    
    $message = $_POST['demo-name']."<br><br>";    
    $message .= $_POST['demo-message'];    
    $header = $fromeml;    
    $header = $ccemail; 
    $header .= "MIME-Version: 1.0\r\n";    
    $header .= "Content-type:    text/html\r\n";
    $retval = mail ($to,$subject,$message,$header);   
    if( $retval == true ) {
        echo <strong>"Message sent successfully...";</strong>    
    } 
    else {
        echo "Message could not be sent...";    
    }  
}


?>

此外,这是上面正文中的表单HTML代码。我使用name作为在post变量中查找的函数。

                    <form method="post" action="?action=submitfunc">
                        <div class="row uniform 50%">
                            <div class="6u 12u$(3)">
                                <input type="text" name="demo-name" id="demo-name" value="" placeholder="Name" />
                            </div>
                            <div class="6u$ 12u$(3)">
                                <input type="email" name="demo-email" id="demo-email" value="" placeholder="Email" />
                            </div>
                            <div class="12u$">
                                <div class="select-wrapper">
                                    <select name="demo-category" id="demo-category">
                                        <option value="">- Category -</option>
                                        <option value="1">Interested in a Quote for a new website</option>
                                        <option value="2">HELP!!!  Technical Issue</option>
                                        <option value="3">Maitenance Quote for existing website</option>
                                        <option value="4">General Question</option>
                                    </select>
                                </div>
                            </div>
                            <div class="4u 12u$(2)">
                                <input type="radio" id="demo-priority-low" name="demo-priority" checked>
                                <label for="demo-priority-low">Low Priority</label>
                            </div>
                            <div class="4u 12u$(2)">
                                <input type="radio" id="demo-priority-normal" name="demo-priority">
                                <label for="demo-priority-normal">Normal Priority</label>
                            </div>
                            <div class="4u$ 12u(2)">
                                <input type="radio" id="demo-priority-high" name="demo-priority">
                                <label for="demo-priority-high">High Priority</label>
                            </div>
                            <div class="6u 12u$(2)">
                                <input type="checkbox" id="demo-copy" name="demo-copy">
                                <label for="demo-copy">Email me a copy of this message</label>
                            </div>
                            <div class="6u$ 12u$(2)">
                                <input type="checkbox" id="demo-human" name="demo-human" >
                                <label for="demo-human">I am a human and not a robot</label>
                            </div>
                            <div class="12u$">
                                <textarea name="demo-message" id="demo-message" placeholder="Enter your message" rows="6"></textarea>
                            </div>
                            <div class="12u$">
                                <ul class="actions">
                                    <li><input type="submit" value="Send Message" class="special" /></li>
                                    <li><input type="reset" value="Reset" /></li>

2 个答案:

答案 0 :(得分:1)

我最终使用不同的版本自行解决它,它实际上将表单操作设置为空白并触发isset($ _ POST [&#39; submit&#39;]){,如下所示:

<?php 



if(isset($_POST['submit'])) {
    $to = "my@emladdress.com"; // this is your Email address
    $from = $_POST['demo-email']; // this is the senders Email address
    $first_name = $_POST['demo-name'];
    $last_name = $_POST['demo-priority'].label;
    $subject = "Form submission for ".$_POST['demo-category'];
    $subject2 = "Copy of your form submission for ".$_POST['demo-category'];
    $message = "<h1>".$first_name . "</h1>" . $last_name . " wrote the following:" . "\n\n" . $_POST['demo-message'];
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['demo-message'];
    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers2 = "MIME-Version: 1.0" . "\r\n";
$headers2 .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    }


?>

这个版本与上面的表格一起工作,并将电子邮件设置为html,方便且全部来自一个页面!

答案 1 :(得分:0)

您的代码中有拼写错误,<strong>打开和关闭标记位于echo命令之外:

echo <strong>"Message sent successfully...";</strong>  

应该是:

echo "<strong>Message sent successfully...</strong>";

另外,我建议在调试时打开错误消息:

error_reporting(E_ALL);
ini_set('display_errors', '1');