代码之家  ›  专栏  ›  技术社区  ›  Dominic Woodman

发送带有附件的PHP电子邮件(失败)

  •  0
  • Dominic Woodman  · 技术社区  · 13 年前

    出身背景

    我正在尝试使用PHP脚本(PHP 5.4.16)发送带有附件的电子邮件。最终我将尝试让它在Drupal中工作,但目前它是独立的。

    问题

    我正在努力实现这一点 script ,将发送带有附件的电子邮件,相关博客为 here .

    我正在运行以下代码,它调用了上面的脚本。它试图发送的文件是同一文件夹中的dom.txt。

    require_once('attachment_email.php');
    
    $to="xxxxx@gmail.com";
    $from="xxxxx@gmail.com";
    $subject="Check out this report!";
    $message="Yo! Check out this report!";
    $attachment = array(
      'url' => 'dom.txt',
      'filename' => 'dom.txt',
    );
    
    print_r($attachment);
    
    $email = new AttachmentEmail($to, $from, $subject, $message, $attachment);
    $email->send();
    

    电子邮件很好,但附件不起作用,我收到了以下错误。

    Warning: Illegal string offset 'path' in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 77
    Warning: file_get_contents(d): failed to open stream: No such file or directory in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 77
    Warning: Illegal string offset 'type' in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 81
    Warning: Illegal string offset 'path' in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 81
    Warning: Illegal string offset 'path' in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 77
    Warning: file_get_contents(d): failed to open stream: No such file or directory in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 77
    Warning: Illegal string offset 'type' in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 81
    Warning: Illegal string offset 'path' in C:\XAMPP\htdocs\tutorials\attachment_email.php on line 81   
    

    我想我在路上做错了什么,但我不知道是什么。我做错了什么?

    我尝试过的

    'path' => 'dom.txt',
    'path' => './dom.txt',
    'url' => 'http://localhost/tutorials/dom.txt',
    

    还有各种各样的变化,但我真的犯了错误。

    我开始从中打印变量 public function get_binary_attachments() 在上面的脚本中查看哪里出了问题,打印$attachment会给出:

    Array ( [url] => ./dom.txt 
            [filename] => dom.txt ) 
    

    但是$attachment_bin是空的,所以有东西坏了。但它可能仍然和路径有关。

    编辑--

    以下是脚本的相关部分:

      73 public function get_binary_attachments() {
      74   $output = '';
      75   foreach($this->attachments as $attachment) {
      76  
      77     echo $attachment;
      78
      79     $attachment_bin = file_get_contents($attachment['path']);
      80     $attachment_bin = chunk_split(base64_encode($attachment_bin));
      81   
      82     dpm($attachment_bin);
      83     $output .= '--'. $this->boundary . PHP_EOL;
      84     $output .= 'Content-Type: '. $attachment['type'] .'; name="'. basename($attachment['path']) .'"' . PHP_EOL;
      85     $output .= 'Content-Transfer-Encoding: base64' . PHP_EOL;
      86     $output .= 'Content-Disposition: attachment' . PHP_EOL . PHP_EOL;
      87     $output .= $attachment_bin . PHP_EOL . PHP_EOL;
      88   }
      89   return $output;
      90 }
      91}
    

    编辑--尝试了新的解决方案:

    1. 更改以下数组的附件会增加三行新的错误:

      'path' => 'dom.txt',
      'type'=>'text/plain',
      'filename' => 'dom.txt',
      

      警告:file_get_contents(d):无法打开流:在C:\XAMPP\htdocs\tutorials\attachment_email.php的第77行中没有这样的文件或目录 警告:第81行C:\XAMPP\htdocs\tutorials\attachment_email.php中的非法字符串偏移量“type” 警告:C:\XAMPP\htdocs\tutorials\attachment_email.php中第81行的字符串偏移量“path”非法

    2. 使用 'path' => './dom.txt' ,将file_get_contents错误更改为:

      警告:file_get_contents(.):无法打开流:第77行C:\XAMPP\htdocs\tutorials\attachment_email.php中的权限被拒绝

    3. 将数组更改为以下值也不会有任何作用。

      'path'=>'./dom.txt',
      'type'=>'MIME/type'
      
    4 回复  |  直到 13 年前
        1
  •  0
  •   Alfie    13 年前

    它找不到您的附件(正在中查找 C:\XAMPP\htdocs\tutorials\ ). 相对于脚本,您的文件位于何处?如果它们在你的根目录中,你应该能够在附件路径前面加上这样的前缀:

    $attachment = array(
      'url' => $_SERVER['DOCUMENT_ROOT'].'/dom.txt',
      'filename' => $_SERVER['DOCUMENT_ROOT'].'/dom.txt',
    );
    
        2
  •  0
  •   bredikhin    13 年前

    看起来很明显,但根据您正在使用的脚本的实际代码 $attachments 应该是以下结构的数组:

    $attachments=array(
        array( // Attachment 1
            'path'=>'/path/to/file',
            'type'=>'MIME/type',
        ),
        array( // Attachment 2
        ),
        ...
    );`
    

    这并不是脚本文档中所述的内容。

        3
  •  0
  •   Dominic Woodman    13 年前

    不幸的是,我通过修改Mimemail的Drupal模块解决了这个问题。如果有人在寻找Drupal帮助时发现了这一点,我建议使用Mimemail开发版本(alpha版本不起作用,它有一个令人沮丧的错误,电子邮件只发送80%的时间(截至2013年9月1日)和本教程:

    http://rapiddg.com/blog-post/drupal-7-html-e-mail-pdf-attachments .

    否则,我会采纳Alfie的建议,使用我开始使用但没有使用的PHPMailer库。

    (我用过 http://www.askapache.com/php/phpfreaks-eric-rosebrocks-phpmailer-tutorial.html ,有点冗长,但没有遗漏内容。)

        4
  •  -1
  •   Samuel Liew cicero lopes    7 年前

    我认为你应该试试这个——它很管用, 这是我的php代码,我正在使用sendgrid api v3 php,因为它不是垃圾邮件

    $Email_to = $_POST['email']; //from html user entered email
    $file_name = $_FILES["upload-file-name"]['name']; //from html upload file it should be less then 2MB 
    $tmp_name = $_FILES['upload-file-name']["tmp_name"];
    require 'sendgrid-php/vendor/autoload.php';
    if(isset($_POST['submit'])) {
        $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $file_name));
    /*$uploadfile is use for user can upload file from any dir. if you are not using you can got error ="file_get_contents(): failed to open stream: No such file or directory php email attachment"*/
            if (move_uploaded_file($tmp_name, $uploadfile)) {
    
    
    $email = new \SendGrid\Mail\Mail();
    $email->setFrom("xxxxxx@xxx.com");
    $email->setSubject("Sending with SendGrid is Fun");
    $email->addTo($Email_to);  //send email to user entered email address in form
    $email->addContent("text/plain", "and easy to do anywhere, even with PHP");
    $email->addContent(
        "text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
    );
    
    $file_encoded = base64_encode(file_get_contents($uploadfile)); $email->addAttachment(
        $file_encoded,
        "application/pdf",
        "customized-file-name.pdf", //you can also add file name as it is like $file_name
        "attachment" );
    

    htlm公司

        <form method="POST" id="form" action="test-sandgrid.php" enctype="multipart/form-data"> 
    <!--enctype is mendetory to upload files-->
            <div class="container" style="width:auto!important">
                <h1 style="text-transform:none ; text-align: center">eCatalog Enquiry Form</h1>
                <p>Please fill in this form to submit your information.</p>
                <hr>
                <label for="file"><b>Upload CV </b></label><br>
                <input type="file" name="" class="form-control" accept=".pdf" value="photo" required/><br>  
    <!--name="upload-file-name"is mendetory , accept is optional i allowed to upload only .pdf file from user and when user select upload file olny .pdf file shows-->
    
    
                <label for="email"><b>Email address</b></label><br>
                <input type="email" class="form-control" placeholder="Enter Email" name="email" required /><br>
    
    <button type="submit" name="submit" value="submit">Submit Your Enquiry</button>
    
            </div>
        </form>
    
    推荐文章