2012-03-26 36 views
0

添付ファイルがあるメールを送信するために見つかったクラスを使用していますが、メッセージを送信しても表示されません。私はHTMLメッセージを送信しようとしていますが、普通のものでも送信しません。 エラーを検出できません。 電子メールはメッセージが空である場合にのみ送信されます 誰かが見てくださいできますか? コード:添付ファイル付きメールはHTMLメッセージを送信しません

<?php 

class mailer{ 
    var $email_to; 
    var $email_subject; 
    var $headers; 
    var $mime_boundary; 
    var $email_message; 

    //sets up variables and mail email 
    function mailer($email_to,$email_subject,$email_message,$headers){ 
     $this->email_to=$email_to; 
     $this->email_subject=$email_subject; 
     $this->headers = $headers; 
     $semi_rand = md5(time()); 
     $this->mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
     $this->headers .= "\nMIME-Version: 1.0\n" . 
       "Content-Type: multipart/mixed;\n" . 
       " boundary=\"{$this->mime_boundary}\""; 
     $this->email_message .= "This is a multi-part message in MIME format.\n\n" . 
       "--{$this->mime_boundary}\n" . 
       "Content-Type:text/html; charset=\"iso-8859-1\"\n". 
       $email_message . "\n\n"; 
    } 

    //adds attachment 
    function attach($fileatt_type,$fileatt_name,$fileatt_content){ 
     $data = chunk_split(base64_encode($fileatt_content)); 
     $this->email_message .= "--{$this->mime_boundary}\n" . 
     "Content-Type: {$fileatt_type};\n" . 
     " name=\"{$fileatt_name}\"\n" . 
     "Content-Transfer-Encoding: base64\n\n" . 
     $data . "\n\n" . 
     "--{$this->mime_boundary}\n"; 
     unset($data); 
     unset($file); 
     unset($fileatt); 
     unset($fileatt_type); 
     unset($fileatt_name); 
    } 

    //send email 
    function send(){ 
     return mail($this->email_to, $this->email_subject, $this->email_message, $this->headers); 
    } 



    //extra functions to make life easier 

    //send email with imap 
    function imap_send(){ 
     return imap_mail($this->email_to, $this->email_subject, $this->email_message, $this->headers); 
    } 

    //read file and add as attachment 
    function file($file){ 
     $o=fopen($file,"rb"); 
     $content=fread($o,filesize($file)); 
     fclose($o); 
     $name=basename($file); 
     $type="application/octet-stream"; 
     $this->attach($type,$name,$content); 
    } 

    //read directory and add files as attachments 
    function dir($dir){ 
     $o=opendir($dir); 
     while(($file=readdir($o)) !==false){ 
      if($file != "." && $file != ".."){ 
       if(is_dir($dir."/".$file)){ 
        $this->dir($dir."/".$file); 
       }else{ 
        $this->file($dir."/".$file); 
       } 
      } 
     } 
    } 
} 

メッセージ移入:クラスを呼び出す

$message = "<span style='color:red;'>NOTICE: test tesx.."; 
$message .= "test text"; 
$message .= "Please text test.</span>"; 
$message .= "If you encounter any problems please contact at [email protected]"; 

:添付ファイル付きのメールを送信するために

 $mailer=new mailer($mail_to,$subject,$message,"From: $from_mail"); 
$mailer->file($fileName); 

$test=$mailer->send(); 
+0

どのようなエラーが表示されますか? –

+0

いいえメッセージなしで電子メールが送信されます。ファイルは、HTMLのdosentだけを添付します –

+0

どのように$ email_messageを設定していますか? –

答えて

2

<?php 

//If there is no error, send the email 
if(isset($_POST['ur_submit_button_name'])) { 
$uname=$_POST['uname']; 
$to = $_POST['mailid']; 
$mobileno=$_POST['mobile']; 
$location=$_POST['location']; 
$from = "Yourname <[email protected]>"; 
$subject = "This is the subject"; 

$separator = md5(time()); 

// carriage return type (we use a PHP end of line constant) 
$eol = PHP_EOL; 

// attachment name 
$filename = "ip.zip";//store that zip file in ur root directory 
$attachment = chunk_split(base64_encode(file_get_contents('ip.zip'))); 

// main header 
$headers = "From: ".$from.$eol; 
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\""; 

// no more headers after this, we start the body! // 

$body = "--".$separator.$eol; 
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol; 
$body .= "This is a MIME encoded message.".$eol; 

// message 
$body .= "--".$separator.$eol; 
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol; 
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol; 
$body .= $message.$eol; 

// attachment 
$body .= "--".$separator.$eol; 
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol; 
$body .= "Content-Disposition: attachment".$eol.$eol; 
$body .= $attachment.$eol; 
$body .= "--".$separator."--"; 

// send message 
if (mail($to, $subject, $body, $headers)) { 
$mail_sent=true; 
echo "mail sent"; 
} else { 
$mail_sent=false; 
echo "Error,Mail not sent"; 

} 
} 

?> 
+0

私は仮定しているいくつかのコードがありません –

+0

それは確かにそれを試すことができます。 –

+0

これは古いスレッドですが、新しい問題を解決しました。私は突然、「additional_headersで見つかった複数のまたは不正な形式の改行」というエラーが発生しました。私がラインで見つけた「解決策」のどれも私のために働いていませんでした(そして多くがありました)。拡張された検索でこのスレッドが見つかり、問題を解決しました! – seveninstl

関連する問題