繁体   English   中英

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

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

背景

我正在尝试使用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中的字符串偏移量'type'不合法\\ attachment_email.php在第81行警告: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' 

它找不到您的附件(它在C:\\XAMPP\\htdocs\\tutorials\\寻找它们)。 与脚本有关的文件在哪里? 如果它们在您的根目录中,则应该可以在附件路径之前加上以下前缀:

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

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

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

脚本文档中未说明的内容。

不幸的是,我通过与Mimemail一起修改了Drupal模块来解决了这个问题。 如果有人在寻找Drupal的帮助,我建议使用Mimemail开发版本(Alpha无法正常工作,它有一个令人沮丧的错误,其中电子邮件仅发送80%的时间(从13年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 ,虽然有点罗word,但并没有遗漏任何内容。)

我认为您应该尝试这一工作,这是我使用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

    <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