使用php发送带有附件的邮件

问题描述

| 我有代码-带附件的邮件..........但我收到一个错误
<?PHP
    $name = \"ReviewBox\";
    $email = \"[email protected]\";
    $to = \"$name <$email>\";
    $from = \"ReviewBox \";
    $subject = \"Here is your attachment\";
    $fileatt = \"doc.pdf\";
    $fileatttype = \"application/pdf\";
    $fileattname = \"newname.pdf\";
    $headers = \"From: $from\";
    $file = fopen($fileatt,\'rb\');
    $data = fread($file,filesize($fileatt));
    fclose($file);
    $semi_rand = md5(time());
    $mime_boundary = \"==Multipart_Boundary_x{$semi_rand}x\";

    $headers .= \"\\nMIME-Version: 1.0\\n\" .
    \"Content-Type: multipart/mixed;\\n\" .
    \" boundary=\\\"{$mime_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\";

    $data = chunk_split(base64_encode($data));

    $message .= \"–{$mime_boundary}\\n\" .
    \"Content-Type: {$fileatttype};\\n\" .
    \" name=\\\"{$fileattname}\\\"\\n\" .
    \"Content-disposition: attachment;\\n\" .
    \" filename=\\\"{$fileattname}\\\"\\n\" .
    \"Content-transfer-encoding: base64\\n\\n\" .
    $data . \"\\n\\n\" .
    \"-{$mime_boundary}-\\n\";

    if(mail($to,$subject,$message,$headers)) {
        echo \"The email was sent.\";
    }else {echo \"There was an error sending the mail.\";}
    ?>
但是,当我运行此代码时,我得到的错误为\“解析错误:语法错误,C:\\ wamp \\ www \\ working \\ mail1.PHP在第3行上的意外\'@ \'\” ... ..如何解决这个问题........     

解决方法

        您使用的是
“...”
而不是引号
\"...\"
。看起来像是从网页复制的代码,该代码将引号转换为\“ fancy quotes \”,然后粘贴到文本文件中。 将所有出现的
替换为
\"
。     ,        
 <?php
    $name = \"Reviewbox\";
    $email = \"[email protected]\";
    $to = \"$name <$email>\";
    $from = \"Reviewbox \";
    $subject = \"Here is your attachment\";
    $fileatt = \"doc.pdf\";
    $fileatttype = \"application/pdf\";
    $fileattname = \"newname.pdf\";
    $headers = \"From: $from\";
    $file = fopen($fileatt,\'rb\');
    $data = fread($file,filesize($fileatt));
    fclose($file);
    $semi_rand = md5(time());
    $mime_boundary = \"==Multipart_Boundary_x{$semi_rand}x\";

    $headers .= \"\\nMIME-Version: 1.0\\n\" .
    \"Content-Type: multipart/mixed;\\n\" .
    \" boundary=\\\"{$mime_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\";

    $data = chunk_split(base64_encode($data));

    $message .= \"–{$mime_boundary}\\n\" .
    \"Content-Type: {$fileatttype};\\n\" .
    \" name=\\\"{$fileattname}\\\"\\n\" .
    \"Content-Disposition: attachment;\\n\" .
    \" filename=\\\"{$fileattname}\\\"\\n\" .
    \"Content-Transfer-Encoding: base64\\n\\n\" .
    $data . \"\\n\\n\" .
    \"-{$mime_boundary}-\\n\";

    if(mail($to,$subject,$message,$headers)) {
        echo \"The email was sent.\";
    }else {echo \"There was an error sending the mail.\";}
    ?>