0

我对带有生成附件并由 mail() 发送的邮件有疑问。我从这里使用示例 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-- 
4

1 回答 1

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 数据分配的每个变量都是空的。

于 2012-11-22T17:36:00.137 回答