使用HTML内容和附件在PHP上发送电子邮件

时间:2016-03-24 11:26:15

标签: php html email

如果我使用附件,如何以HTML格式发送内容?在标题中我还有其他内容吗?

首先我获取文件,而不是类型消息,而不是像mime_boundaty那样发送它。 我需要在一个地方更改内容类型。 它有可能吗?

$files = array();
function reArrayFiles(&$file_post) {

    $file_ary = array();
    $file_count = count($file_post['name']);
    $file_keys = array_keys($file_post);

    for ($i=0; $i<$file_count; $i++) {
        foreach ($file_keys as $key) {
            $new_arr = array();
            array_push($new_arr,$file_post[$key][$i]);
            $file_ary[$i][$key] = $new_arr;
        }
    }

    return $file_ary;
}
foreach($_FILES as $file) {
    $file = reArrayFiles($file);
    foreach($file as $name=>$file1) {
        for($i=0; $i<count($file1);$i++){
        }
            if($file1['error'][0]!='4')
                array_push($files,$file1);
    }
}

// email fields: to, from, subject, and so on
$to = "contact@amil.com";
$from = $_POST['mail']; 
$subject = "Quote request: ".$_POST['ttle'].' '; 
$message = "Full name:".$_POST['name'].' ';
$message.= "Email:".$_POST['mail'].' '; 
$message.= "Description:".$_POST['descr'].' ';
$headers = "From: $from";

// 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 = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
$message .= "--{$mime_boundary}\n";

// preparing attachments
for($x=0;$x<count($files);$x++){

    // echo $files[$x];
    //  echo '<br>-----------------------<br>';         
    $file = fopen($files[$x]['tmp_name'][0],"rb");
    $data = fread($file,filesize($files[$x]['tmp_name'][0]));
    fclose($file);
    $data = chunk_split(base64_encode($data));
    $name = $files[$x]['name'][0];
    $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" . 
    "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" . 
    "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
    $message .= "--{$mime_boundary}\n";
}
// send

$ok = mail($to, $subject, $message, $headers); 
   if ($ok) { 
        header('Location: index.php'); 
   } else { 
        header('Location: index.php');
   } 
}   
?>`

1 个答案:

答案 0 :(得分:0)

使用 phpmailer 类。 这是一个有用的方法:

function sendmailatt($to,$cc,$subject,$htmlbody,$attachpath)
{
//iconv_set_encoding("internal_encoding", "UTF-8");
        $table = $htmlbody;
        $mail = new PHPMailer;
        //$mail->CharSet = 'UTF-8';
        $mail->isSMTP();  
        $mail->Host = 'host server'; 
        $mail->SMTPAuth = true;   
        $mail->Username = 'user'; 
        $mail->Password = 'password';  
        $mail->SMTPSecure = 'tls'; 
        $mail->Port = 587; 
        $mail->IsHTML(true);
        $mail->setFrom('mailfrom', 'alias');
        //$to2=explode($to, ';');
        $to3 = explode(";", $to);
        foreach($to3 as $to1)
        {
            $mail->addAddress($to1);
        }
        $cc2 = explode(";", $cc);
        foreach($cc2 as $cc1)
        {
            $mail->addCC($cc1);
        }
        $mail->addReplyTo('replymail');
        $mail->addAttachment($attachpath);         // Add attachments
        //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
        $mail->Subject = $subject;
        $mail->Body    = $table;
        //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
        if(!$mail->send()) 
        {
            echo 'Error';
            echo 'Errore: ' . $mail->ErrorInfo;
        } else 
        {
            echo '<script>alert("DONE");</script>';
        }
}

关于附件:

$table.='</tr></table>';
    if (file_exists("EXCEL REPORT/".$subject." ".date("M-d-Y").".xls")) 
    {
        unlink("EXCEL REPORT/".$subject." ".date("M-d-Y").".xls");
        file_put_contents("EXCEL REPORT/".$subject." ".date("M-d-Y").".xls", $table); 
        sendmailatt($to,$cc,$subject." ".date("M-d-Y"),"BODY","EXCEL REPORT/".$subject." ".date("M-d-Y").".xls");
    } 
    else {
       file_put_contents("EXCEL REPORT/".$subject." ".date("M-d-Y").".xls", $table); 
       sendmailatt($to,$cc,$subject." ".date("M-d-Y"),"BODY","EXCEL REPORT/".$subject." ".date("M-d-Y").".xls");
    }