【问题标题】:unable to see status message after sending an email from contact page in html using php使用 php 从 html 中的联系页面发送电子邮件后无法看到状态消息
【发布时间】:2021-10-19 17:44:01
【问题描述】:

我正在尝试从联系页面发送电子邮件。该功能工作正常,我可以从 html 页面发送邮件,但我面临的唯一问题是我无法看到状态 div(成功或失败)。

最初页面被重定向到 php 文件,没有任何状态消息。我已经使用 php 中的 header() 将页面重定向添加到实际的 mailsend.html。现在我想在发送操作后有一个状态,无论邮件是否已发送。

下面是代码sn-p。请帮忙。提前致谢。

mailSend.html 代码:

<?php if(!empty($statusMsg)){ ?>
    <p class="statusMsg <?php echo !empty($msgClass)?$msgClass:''; ?>"><?php echo $statusMsg; ?></p>
<?php } ?>
<form action="example.php" method="post" enctype="multipart/form-data">
   <div class="form-group">
       <input style = "padding-left:2%; width: 97%;" type="text" name="name" class="form-control" value="" placeholder="Name" required="">
   </div>
   <div class="form-group">
       <input style = "padding-left:2%; width: 97%;" type="email" name="email" class="form-control" value="" placeholder="Email address" required="">
   </div>
   <div class="form-group">
       <input style = "padding-left:2%; width: 97%;" type="text" name="subject" class="form-control" value="" placeholder="Subject" required="">
   </div>
   <div class="form-group">
       <textarea name="message" class="form-control" placeholder="Write your message here" required="" style = 'border :0.5px solid '></textarea>
   </div>
   <div class="form-group">
       <input type="file" name="attachment" class="form-control" style = 'border :0.5px solid; height: auto;'>
   </div>
   <div class="submit">
       <input type="submit" name="submit" class="btn" value="SUBMIT" style= 'float : right;'>
   </div>
</form>

example.php 代码:

    <?php
                //first we leave this input field blank
                $recipient = "";
                //if user click the send button
                if(isset($_POST['submit'])){
                    //access user entered data
                   $recipient = $_POST['email'];
                   $subject = $_POST['subject'];
                   $message = $_POST['message'];
                   $sender = "From: xyz@gmail.com";
                   //if user leave empty field among one of them
                   if(empty($recipient) || empty($subject) || empty($message)){
                       ?>
                       <!-- display an alert message if one of them field is empty -->
                        <div class="alert alert-danger text-center">
                            <?php echo "All inputs are required!" ?>
                        </div>
                       <?php
                    }else{
                        
                        $uploadStatus = 1;
        
                        // Upload attachment file
                        if(!empty($_FILES["attachment"]["name"])){
                            
                            // File path config
                            $targetDir = "uploads/";
                            $fileName = basename($_FILES["attachment"]["name"]);
                            $targetFilePath = $targetDir . $fileName;
                            $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
                            
                            // Allow certain file formats
                            $allowTypes = array('pdf', 'doc', 'docx', 'jpg', 'png', 'jpeg');
                            if(in_array($fileType, $allowTypes)){
                                // Upload file to the server
                                if(move_uploaded_file($_FILES["attachment"]["tmp_name"], $targetFilePath)){
                                    $uploadedFile = $targetFilePath;
                                }else{
                                    $uploadStatus = 0;
                                    $statusMsg = "Sorry, there was an error uploading your file.";
                                }
                            }else{
                                $uploadStatus = 0;
                                $statusMsg = 'Sorry, only PDF, DOC, JPG, JPEG, & PNG files are allowed to upload.';
                            }
                        }
                        
                        if($uploadStatus == 1){
            
            // Recipient
            $toEmail = 'abc@gmail.com';

            // Sender
            $from = 'xyz@gmail.com';
            $fromName = 'example';
            
            // Subject
            $emailSubject = 'Contact Request Submitted by '.$recipient;
            
            // Message 
            $htmlContent = '<h2>Contact Request Submitted</h2>
                <p><b>Name:</b> '.$recipient.'</p>
                <p><b>Email:</b> '.$sender.'</p>
                <p><b>Subject:</b> '.$subject.'</p>
                <p><b>Message:</b><br/>'.$message.'</p>';
            
            // Header for sender info
            $headers = "From: $fromName"." <".$from.">";

            if(!empty($uploadedFile) && file_exists($uploadedFile)){
                
                // Boundary 
                $semi_rand = md5(time()); 
                $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
                
                // Headers for attachment 
                $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
                
                // Multipart boundary 
                $message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
                "Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n"; 
                
                // Preparing attachment
                if(is_file($uploadedFile)){
                    $message .= "--{$mime_boundary}\n";
                    $fp =    @fopen($uploadedFile,"rb");
                    $data =  @fread($fp,filesize($uploadedFile));
                    @fclose($fp);
                    $data = chunk_split(base64_encode($data));
                    $message .= "Content-Type: application/octet-stream; name=\"".basename($uploadedFile)."\"\n" . 
                    "Content-Description: ".basename($uploadedFile)."\n" .
                    "Content-Disposition: attachment;\n" . " filename=\"".basename($uploadedFile)."\"; size=".filesize($uploadedFile).";\n" . 
                    "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
                }
                
                $message .= "--{$mime_boundary}--";
                $returnpath = "-f" . $recipient;
                
                // Send email
                $mail = mail($toEmail, $emailSubject, $message, $headers, $returnpath);
                
                // Delete attachment file from the server
                @unlink($uploadedFile);
            }else{
                 // Set content-type header for sending HTML email
                $headers .= "\r\n". "MIME-Version: 1.0";
                $headers .= "\r\n". "Content-type:text/html;charset=UTF-8";
                
                // Send email
                $mail = mail($toEmail, $emailSubject, $htmlContent, $headers); 
            }
            
            // If mail sent
            if($mail){
                $statusMsg = 'Your contact request has been submitted successfully !';
                $msgClass = 'succdiv';
                ?>
                        <!-- display a success message if once mail sent sucessfully -->
                        <div class="alert alert-success text-center">
                            <!--<?php echo "Your mail successfully sent to $recipient"?>-->
                            <!--readfile('submitResume.html');-->
                            <?php 
                                header('Location: mailSend.html') ;
                                echo "Your mail successfully sent to $recipient"
                            ?>
                        </div>
                       <?php
                       $recipient = "";
                $postData = '';
            }else{
                $statusMsg = 'Your contact request submission failed, please try again.';
                 ?>
                        <!-- display an alert message if somehow mail can't be sent -->
                        <div class="alert alert-danger text-center">
                            <?php echo "Failed while sending your mail!" ?>
                        </div>
                       <?php
            }
            
        }    
       }
    }
?> 

【问题讨论】:

    标签: php html html-email email-attachments


    【解决方案1】:

    您正在发送到一个 .html 页面,默认情况下该页面不处理 PHP 代码,除非在服务器上专门配置,否则服务器只会提供它。将页面从mailSend.html 重命名为mailSend.php,它应该可以解决它。确保更改您的代码以发送到 .php 页面。

    For further reading see here

    您需要传递消息本身或让脚本知道要显示哪条消息的方法。最简单的方法是通过$_GET 传递它,将其附加到您尝试重定向的 URL 的末尾。像这样:

    $target_url = mailSend.php;
    $get_data = '?statusMsg=' . urlencode($statusMsg) . '&$msgClass=' . urlencode($msgClass);
    header( 'Location: ' . $target_url . $get_data );
    

    然后您可以通过全局 $_GET 变量在 mailSend.php 上恢复。如:

    $statusMsg = urldecode($_GET['statusMsg']);
    $msgClass= urldecode($_GET['msgClass']);
    

    还有其他方法可以将数据从一个页面转移到另一个页面,但我会留给您进行研究。因为它超出了简单答案的范围。

    【讨论】:

    • 另外,变量 $statusMsg 也不会出现在第二页中,因此您还需要提出一种解决方法
    • 嗯,好的,我也会添加那部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 2013-12-17
    • 2014-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    相关资源
    最近更新 更多