带有生成附件的mail()未在客户端邮件上格式化

时间:2012-11-22 17:07:35

标签: php

我对带有生成附件的邮件有问题,并通过mail()发送。 我使用here中的示例3。

它工作正常。但是当我添加一些php定义(例如:$ somthing = $ _POST ['sth'])时,到达的电子邮件没有格式化,看起来像源代码。

<?php
    //session_start();
    include('funkcje.php');
    //$db=polaczMy();
    $nazwa = uzupelnij($_POST['nazwa']);
    $vorname= uzupelnij($_POST['vorname']);
    $strasse = uzupelnij($_POST['strasse']);
    $kod = uzupelnij($_POST['kod']);
    $tel = uzupelnij($_POST['tel']);
    $mail = uzupelnij($_POST['mail']);
    $datum_od  = uzupelnij($_POST['datum_od']);
    $datum_to  = uzupelnij($_POST['datum_to']); 
    $ort = uzupelnij($_POST['ort']);
    $zeit = uzupelnij($_POST['zeit']);
    $mietteilung = uzupelnij($_POST['mietteilung']);
    $service = uzupelnij($_POST['service']);

    function uzupelnij($przesyl){
        if(empty($przesyl)){ 
            $przesyl = "";
            return $przesyl;
        }
    }

        $to = '***@***.net'; 
        $subject = 'Anfrage';  
        $random_hash = md5(date('r', time())); 
        $headers = "From: ***@***.de\r\n"; 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
        $attachment = "BEGIN:VCARD\nVERSION:3.0\nN:".$vorname.";;;;\nFN:".$vorname."\nORG:".$nazwa."\nADR;TYPE=work:;;".$strasse.";".$ort.";;;\nEMAIL;type=INTERNET;type=WORK;type=internet:".$mail."\nTEL;type=WORK;type=pref:".$tel."\nEND:VCARD"; 
        ob_start();
        ?>
--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 
--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit
Hello World!!! 
This is simple text email message. 
--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 
--PHP-alt-<?php echo $random_hash; ?>-- 
--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: text/vcard; name="attachment.vcf"  
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment  
<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 
<?php 
        //copy current buffer contents into $message variable and delete current output buffer 
        $message = ob_get_clean(); 
        //send the email 
        $mail_sent = @mail( $to, $subject, $message, $headers ); 
        //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
        echo $mail_sent ? "<span style=\"margin-top: 20px; font-size: 18px; font-weight: bold;\">Vielen Dank! Ihre Anfrage wurde versendet. Wir werden bald mit Ihnen Kontakt aufnehmen. </span>" : "Mail failed"; 
        //session_destroy();
// }
?>

和邮件:

 Content-Type: multipart/mixed; boundary="PHP-mixed-26680626f5767c87be4bc162d5e80e8e"

--PHP-mixed-26680626f5767c87be4bc162d5e80e8e  
Content-Type: multipart/alternative; boundary="PHP-alt-26680626f5767c87be4bc162d5e80e8e" 
--PHP-alt-26680626f5767c87be4bc162d5e80e8e  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit
Hello World!!! 
This is simple text email message. 
--PHP-alt-26680626f5767c87be4bc162d5e80e8e  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 
--PHP-alt-26680626f5767c87be4bc162d5e80e8e-- 
--PHP-mixed-26680626f5767c87be4bc162d5e80e8e  
Content-Type: text/vcard; name="attachment.vcf"  
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment  
BEGIN:VCARD
VERSION:3.0
N:dgsdg;;;;
FN:dgsdg
ORG:bgdfg
ADR;TYPE=work:;;dgsdg;gdsg;;;
EMAIL;type=INTERNET;type=WORK;type=internet:gsdgdf
TEL;type=WORK;type=pref:gfgs
END:VCARD 
--PHP-mixed-26680626f5767c87be4bc162d5e80e8e-- 

1 个答案:

答案 0 :(得分:1)

$headers = "From: ***@***.de\r\n"; 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 

这将导致标题中出现空行,因此标题将在From字段结束,Content-type将被视为邮件正文。

在每个边界标记之前,您还缺少一个空行。

更重要的是:

function uzupelnij($przesyl){
    if(empty($przesyl)){ 
        $przesyl = "";
        return $przesyl;
    }
}

您认为它有什么作用?它返回null或空字符串,因此您从POST数据分配的每个变量都是空的。