【问题标题】:sending an mail with attachment using php使用php发送带有附件的邮件
【发布时间】:2011-05-30 10:41:00
【问题描述】:

我有这个代码 - 带有附件的邮件.......但我收到一个错误

<?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.";}
    ?>

但是当我运行此代码时,我收到错误消息“解析错误:语法错误,第 3 行 C:\wamp\www\working\mail1.php 中的意外 '@'”..... 如何解决这…………

【问题讨论】:

  • @etbal ya 那不是我的代码.....我从网上拿的..如何解决这个......
  • 答案在下面@Radu 的答案中。请不要说你写了代码然后说你是从网上复制的......
  • 获取一个带有php语法高亮的编辑器,不要使用记事本...

标签: php email


【解决方案1】:

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

将所有出现的 替换为"

【讨论】:

  • @Radu 现在它给我发了这么多错误。 “警告:fopen(doc.pdf)[function.fopen]:无法打开流:第 19 行的 C:\wamp\www\working\mail1.php 中没有错误”.. 怎么办...... .........
  • 它无法打开文件doc.pdf,因为错误状态。确保传递文件的完整路径,而不仅仅是文件名。
  • 你是如何给出完整路径的?你写了什么?
  • @Radu 我给了 $fileatt = "file:///C:/wamp/www/working/doc.pdf"。是这样写的
【解决方案2】:
 <?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.";}
    ?>

【讨论】:

  • 花时间去做的道具。
猜你喜欢
  • 2011-02-18
  • 2016-05-03
  • 2023-03-21
  • 2012-08-02
  • 2013-06-20
  • 2012-01-15
相关资源
最近更新 更多