简体   繁体   English

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

[英]Sending a PHP email with attachment (failure)

Background 背景

I'm trying to send emails with attachments using a PHP script (PHP 5.4.16). 我正在尝试使用PHP脚本(PHP 5.4.16)发送带有附件的电子邮件。 Eventually I'll be trying to get it to work in Drupal, but for now it's standalone. 最终,我将尝试使其在Drupal中工作,但现在它是独立的。

Problem 问题

I'm trying to implement this script , which will send emails with attachments, the relevant blog is here . 我正在尝试实现此脚本 ,该脚本将发送带有附件的电子邮件,相关博客在此处

I'm running the following code, which calls the script above. 我正在运行以下代码,该代码调用上面的脚本。 The file it's trying to send is dom.txt in the same folder. 它尝试发送的文件是同一文件夹中的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();

The email arrives fine but the attachment doesn't work and I get the following errors. 电子邮件可以正常发送,但附件无法正常工作,并且出现以下错误。

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   

I think I'm doing something wrong with the path but I have no idea what. 我想我在路径上做错了,但我不知道该怎么做。 What am I doing wrong? 我究竟做错了什么?

What I've Tried 我尝试过的

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

And all sorts of variations like that, but I'm blundering really. 像这样的各种各样的变化,但我确实犯错了。

I started printing out variables from public function get_binary_attachments() in the script above to see where it's going wrong and printing $attachment gives: 我开始从上面脚本中的public function get_binary_attachments()中打印出变量,以查看出错的地方并打印$ attachment给出:

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

But $attachment_bin is empty, so something breaks there. 但是$ attachment_bin是空的,所以有什么地方中断了。 But it might still be related to path. 但是它可能仍然与路径有关。

EDIT -- 编辑-

Here is the relevant part of the script: 这是脚本的相关部分:

  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}

EDIT -- New Solutions tried: 编辑-新解决方案尝试:

  1. Changing attachment to the following array add's three new lines of errors : 更改对以下数组的附件会添加三行新错误:

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

    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 警告:file_get_contents(d):无法打开流:第77行的C:\\ XAMPP \\ htdocs \\ tutorials \\ attachment_email.php中没有此类文件或目录警告:C:\\ XAMPP \\ htdocs \\ tutorials中的字符串偏移量'type'不合法\\ attachment_email.php在第81行警告:C:\\ XAMPP \\ htdocs \\ tutorials \\ attachment_email.php在第81行的字符串偏移'path'不合法

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

    Warning: file_get_contents(.): failed to open stream: Permission denied in C:\\XAMPP\\htdocs\\tutorials\\attachment_email.php on line 77 警告:file_get_contents(。):无法打开流:第77行的C:\\ XAMPP \\ htdocs \\ tutorials \\ attachment_email.php中的权限被拒绝

  3. Changing the array to below, also does nothing. 将数组更改为下面,也不会执行任何操作。

     'path'=>'./dom.txt', 'type'=>'MIME/type' 

It is failing to find your attachments (it is looking for them in C:\\XAMPP\\htdocs\\tutorials\\ ). 它找不到您的附件(它在C:\\XAMPP\\htdocs\\tutorials\\寻找它们)。 Where are your files located in relation to the script? 与脚本有关的文件在哪里? If they were in your root directory, you should be able to prefix the attachment path with something like this: 如果它们在您的根目录中,则应该可以在附件路径之前加上以下前缀:

$attachment = array(
  'url' => $_SERVER['DOCUMENT_ROOT'].'/dom.txt',
  'filename' => $_SERVER['DOCUMENT_ROOT'].'/dom.txt',
);

Seems like an obvious thing, but according to the actual code of the script you are using $attachments should an array of the following structure: 看起来很明显,但是根据脚本的实际代码,您正在使用$attachments的数组应具有以下结构:

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

Which is not what is stated in the documentation to the script. 脚本文档中未说明的内容。

Unfortunately I solved this by tinkering with Mimemail the Drupal module. 不幸的是,我通过与Mimemail一起修改了Drupal模块来解决了这个问题。 If anyone finds this looking for Drupal help I would recommend using the Mimemail dev version (the alpha didn't work, it has a frustrating bug where the email only sends 80% of the time (as of 1st Sep '13) and this tutorial: 如果有人在寻找Drupal的帮助,我建议使用Mimemail开发版本(Alpha无法正常工作,它有一个令人沮丧的错误,其中电子邮件仅发送80%的时间(从13年9月1日开始),本教程:

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

Otherwise I'd go with Alfie's suggestion and use the PHPMailer library which I got working and didn't use. 否则,我会接受Alfie的建议,并使用我已经使用但未使用的PHPMailer库。

(I used http://www.askapache.com/php/phpfreaks-eric-rosebrocks-phpmailer-tutorial.html , a little wordy but doesn't leave stuff out.) (我使用了http://www.askapache.com/php/phpfreaks-eric-rosebrocks-phpmailer-tutorial.html ,虽然有点罗word,但并没有遗漏任何内容。)

I think you should try this one its work, this is my php code i am using sendgrid api v3 php because its not spam email 我认为您应该尝试这一工作,这是我使用sendgrid api v3 php的我的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 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM