0

背景

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

问题

我正在尝试实现这个脚本,它将发送带有附件的电子邮件,相关博客在这里

我正在运行以下代码,它调用上面的脚本。它尝试发送的文件是同一文件夹中的 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):无法打开流:第 77 行的 C:\XAMPP\htdocs\tutorials\attachment_email.php 中没有这样的文件或目录警告:C:\XAMPP\htdocs\tutorials 中的非法字符串偏移“类型”第 81 行的 \attachment_email.php 警告:第 81 行的 C:\XAMPP\htdocs\tutorials\attachment_email.php 中的非法字符串偏移“路径”

  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

4 回答 4

0

它无法找到您的附件(它正在寻找它们C:\XAMPP\htdocs\tutorials\)。您的文件与脚本相关的位置在哪里?如果它们在您的根目录中,您应该能够在附件路径前加上以下内容:

$attachment = array(
  'url' => $_SERVER['DOCUMENT_ROOT'].'/dom.txt',
  'filename' => $_SERVER['DOCUMENT_ROOT'].'/dom.txt',
);
于 2013-08-23T14:37:58.517 回答
0

似乎是一件显而易见的事情,但根据您使用的脚本的实际代码,$attachments应该是以下结构的数组:

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

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

于 2013-08-23T14:54:20.797 回答
0

不幸的是,我通过修改 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,有点罗嗦,但不会遗漏一些东西。)

于 2013-09-24T14:27:12.380 回答
-1

我认为你应该试试这个它的工作,这是我的 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" );

html

    <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>
于 2018-11-22T05:30:27.120 回答