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

使用php发送带有附件的邮件,php,email,Php,Email,我收到了这封带有附件的代码邮件。。。。。。。。。。但我犯了个错误 <?php $name = "Reviewbox"; $email = "jmaster@gmail.com"; $to = "$name <$email>"; $from = "Reviewbox "; $subject = "Here is your attachment"; $fileatt = "doc.pdf"; $fileatttype = "a

我收到了这封带有附件的代码邮件。。。。。。。。。。但我犯了个错误

<?php
    $name = "Reviewbox";
    $email = "jmaster@gmail.com";
    $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.";}
    ?>


但是当我运行这段代码时,我得到了一个错误“Parse error:syntax error,C:\wamp\www\working\mail1.php中的意外“@”,在第3行“。。。。。如何解决这个问题

您使用的是
“…”
而不是引号
“…”
。看起来像是从网页复制的代码,该网页将引号转换为“奇特的引号”,并粘贴到文本文件中

将所有出现的
替换为



可能重复-@etbal ya这不是我的代码…..我从网络上获取了..如何解决这个问题..答案在下面@Radu的答案中。请不要说你已经编写了代码,然后说你从web上复制了代码…找一个带有php语法突出显示的编辑器,不要使用记事本…道具来花时间做这些。@Radu现在发布给我这个问题很多错误。“警告:fopen(doc.pdf)[function.fopen]:打开流失败:第19行的C:\wamp\www\working\mail1.php中没有错误".. 如何操作………..它无法打开文件
doc.pdf
,如错误所述。确保传递文件的完整路径,而不仅仅是文件名。您是如何给出完整路径的?你写了什么?@Radu我给了$fileatt=”file:///C:/wamp/www/working/doc.pdf". 那是你写的吗
 <?php
    $name = "Reviewbox";
    $email = "jmaster@gmail.com";
    $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.";}
    ?>