2016-02-19 18 views
0

請幫我解決好的撒瑪利亞人。我正在嘗試從php代碼發送帶有附件的電子郵件。代碼工作正常,它發送電子郵件和所有附加文件。在PHP中發送電子郵件時,gmail未解釋HTML標記元素

但這個代碼的問題是,html標籤不interpred。它們看起來就像它們一樣。以下是問題的快照。 Email i recieve

這是我使用的代碼。我在哪裏錯了。我應該在哪裏編輯我的代碼。請幫助這個傢伙出去。感謝名單。

      <?php 

           if(isset($_POST['sendmail']) && isset($_FILES['file'])) 
           { 
            $message = '<html><body>'; 
            $message .= '<img src="http://jehucocloud.co.za/chamber/chamber/profile_img/fnb.png" alt="Website Change Request" />'; 
            $message .= '<table rules="all" style="border-color: #666;" cellpadding="10">'; 
            $message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . $organisation . "</td></tr>"; 
            $message .= "<tr><td><strong>Email:</strong> </td><td>" . $organisationemail . "</td></tr>"; 
            $message .= "<tr><td><strong>Subject:</strong> </td><td>" . $subject . "</td></tr>"; 
            $message .= "<tr><td><strong>Our Website (main):</strong> </td><td><a href='". $organisationwebsite ."'>" . $organisationwebsite . "</a></td></tr>"; 
            $message .= "<tr><td><strong>Message:</strong> </td><td>" . $text_message . "</td></tr>"; 
            $message .= "</table>"; 
            $message .= "</body></html>"; 

            $attachments = $_FILES['file']; 

            if(!empty($_POST['check_list'])){ 
             foreach($_POST['check_list'] as $report_id){ 
              if($report_id != null) 
              { 
               $recipient_email .= ','.$report_id; 
               $recipient_email[0] = ''; 
              } 
             } 
            } 

            if($recipient_email != null) 
            { 
             $file_count = count($attachments['name']); //count total files attached 
             $boundary = md5("sanwebe.com"); 

             if($file_count > 0){ 

               //header 
               $headers = "MIME-Version: 1.0\r\n"; 
               $headers .= "From:".$from_email."\r\n"; 
               $headers .= "Reply-To: ".$recipient_email."" . "\r\n"; 
               $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; 

               //message text 
               $body = "--$boundary\r\n"; 
               $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n"; 
               $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
               $body .= chunk_split(base64_encode($message)); 


               //attachments 
               for ($x = 0; $x < $file_count; $x++){  
                if(!empty($attachments['name'][$x])){ 

                 if($attachments['error'][$x]>0) //exit script and output error if we encounter any 
                 { 
                  $mymsg = array( 
                  1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini", 
                  2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", 
                  3=>"The uploaded file was only partially uploaded", 
                  4=>"No file was uploaded", 
                  6=>"Missing a temporary folder"); 
                  die($mymsg[$attachments['error'][$x]]); 
                 } 

                 //get file info 
                 $file_name = $attachments['name'][$x]; 
                 $file_size = $attachments['size'][$x]; 
                 $file_type = $attachments['type'][$x]; 

                 //read file 
                 $handle = fopen($attachments['tmp_name'][$x], "r"); 
                 $content = fread($handle, $file_size); 
                 fclose($handle); 
                 $encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045) 

                 $body .= "--$boundary\r\n"; 
                 $body .="Content-Type: $file_type; name=\"$file_name\"\r\n"; 
                 $body .="Content-Disposition: attachment; filename=\"$file_name\"\r\n"; 
                 $body .="Content-Transfer-Encoding: base64\r\n"; 
                 $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; 
                 $body .= $encoded_content; 
                } 
               } 

               $sentMail = @mail($recipient_email, $subject, $body, $headers); 

               if($sentMail) //output success or failure messages 
               {  
                ?> 
                 <div class="alert alert-success"> 
                  <button data-dismiss="alert" class="close" type="button">×</button> 
                  <strong>You send an Email to all the selected recipients!</strong> 
                 </div> 
                <?php 
               }else{ 
                ?> 
                 <div class="alert alert-error"> 
                  <button data-dismiss="alert" class="close" type="button">×</button> 
                  <strong>Could not send mail!</strong> 
                 </div> 
                <?php 
               } 
             } 
            } 
            else 
            { 
             ?> 
              <div class="alert alert-error"> 
               <button data-dismiss="alert" class="close" type="button">×</button> 
               <strong>You haven't selected recipients! You have to select atleast one recipient.</strong> 
              </div> 
             <?php 
            } 
           } 
          ?> 
          <form enctype="multipart/form-data" method="POST" action=""> 
           <fieldset> 
            <div class="control-group"> 
             <label class="control-label" for="input01">Subject</label> 
             <div class="controls"> 
              <input required type="text" class="span8" name="subject" value="<?php echo $subject; ?>" id="input01"> 
             </div> 
            </div> 
            <div class="control-group"> 
             <label class="control-label">Attachmnet</label> 
             <div class="controls"> 
              <input class="input-file" required name="file[]" multiple type="file"> 
             </div> 
            </div> 
            <div class="control-group"> 
             <label class="control-label">Message</label> 
             <div class="controls"> 
              <textarea required class="span12" rows="10" name="text_message" cols="10"><?php echo $text_message; ?></textarea> 
             </div> 
            </div> 
            <div class="control-group"> 
             <div class="span12"> 
              <div class="nonboxy-widget"> 
               <div class="widget-head"> 
               <h5 class="pull-left"><i class="black-icons list_images"></i> Select Companies</h5> 
              </div> 

               <table class="data-tbl-striped table table-striped table-bordered"> 
               <thead> 
                <tr> 
                <th class="center"> <input name="checkbox" type="checkbox" value="" class="checkall-task"> 
                </th> 
                <th> Logo </th> 
                <th> RegNo </th> 
                <th> Company </th> 
                <th class="center"> Type </th> 
                <th class="center"> Bank </th> 
                <th class="center"> Account Status </th> 
                <th class="center"> Action </th> 
                </tr> 
               </thead> 
               <tbody> 
               <?php 

                $query = "SELECT u.status,c.username,c.reg_no,c.enterprise_name,c.enterprise_type,c.bank,c.logo,c.email FROM company_details c,users u WHERE c.username=u.username order by c.enterprise_name"; 
                $subject_set = mysqli_query($connection,$query); 

                while($row = mysqli_fetch_array($subject_set)) 
                { 
                 if($row['status'] == 1 && $row['email']!='') 
                 { 
                 echo '<tr> 
                  <td class="center tr-task-check"><input name="check_list[]" type="checkbox" value="'.$row['email'].'"></td> 
                  <td class="center"><span class="user-thumb"><img src="../../y/profiles/'.$row['logo'].'" width="100" height="100" alt="User"></span></td> 
                  <td>'.$row['reg_no'].'</td> 
                  <td>'.$row['enterprise_name'].'</td> 
                  <td class="center">'.$row['enterprise_type'].'</td> 
                  <td class="center">'.$row['bank'].'</td> 
                  <td class="center"><span class="label label-success">Active</span></td> 
                  <td><div class="btn-group pull-right"> 
                   <button data-toggle="dropdown" class="btn dropdown-toggle"><i class="icon-cog "></i><span class="caret"></span></button> 
                   <ul class="dropdown-menu"> 
                    <li><a href="/chamber/view_business/?reg='.$row["reg_no"].'"><i class="icon-file"></i> View Details</a></li> 
                   </ul> 
                   </div></td> 
                  </tr>'; 
                 } 
                 else 
                 { 
                  echo '<tr> 
                  <td class="center tr-task-check"><input name="check_list[]" type="checkbox" value="'.$row['email'].'"></td> 
                  <td class="center"><span class="user-thumb"><img src="../../y/profiles/'.$row['logo'].'" width="40" height="40" alt="User"></span></td> 
                  <td>'.$row['reg_no'].'</td> 
                  <td>'.$row['enterprise_name'].'</td> 
                  <td class="center">'.$row['enterprise_type'].'</td> 
                  <td class="center">'.$row['bank'].'</td> 
                  <td class="center"><span class="label label-important">Expired</span></td> 
                  <td><div class="btn-group pull-right"> 
                   <button data-toggle="dropdown" class="btn dropdown-toggle"><i class="icon-cog "></i><span class="caret"></span></button> 
                   <ul class="dropdown-menu"> 
                    <li><a href="/chamber/view_business/?reg='.$row["reg_no"].'"><i class="icon-file"></i> View Details</a></li> 
                   </ul> 
                   </div></td> 
                  </tr>'; 
                 } 
                } 
               ?> 
               </tbody> 
               </table> 
              </div> 
             </div> 
            </div> 
            <div> 
             <button type="submit" name="sendmail" class="btn btn-info"><i class="icon-retweet icon-white"></i> Send</button> 
            </div> 
           </fieldset> 
          </form> 

回答

0

更新您的電子郵件標題,支持HTML。

// To send HTML mail, the Content-type header must be set 
$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

// Additional headers 
$headers .= 'To: Name <[email protected]>, Name2 <[email protected]>' . "\r\n"; 
$headers .= 'From: Your Name <[email protected]>' . "\r\n"; 
$headers .= 'Cc: [email protected]' . "\r\n"; 
$headers .= 'Bcc: [email protected]' . "\r\n"; 
+0

謝謝先生。你讓我意識到我不知道的東西。現在它正在工作。我只改變了$ body。=「Content-Type:text/plain; charset = ISO-8859-1 \ r \ n」;爲「body」。=「Content-Type:text/html; charset = ISO-8859-1 \ r \ n」; –