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



背景

我正在尝试使用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:XAMPPhtdocstutorialsattachment_email.php on line 77
Warning: file_get_contents(d): failed to open stream: No such file or directory in C:XAMPPhtdocstutorialsattachment_email.php on line 77
Warning: Illegal string offset 'type' in C:XAMPPhtdocstutorialsattachment_email.php on line 81
Warning: Illegal string offset 'path' in C:XAMPPhtdocstutorialsattachment_email.php on line 81
Warning: Illegal string offset 'path' in C:XAMPPhtdocstutorialsattachment_email.php on line 77
Warning: file_get_contents(d): failed to open stream: No such file or directory in C:XAMPPhtdocstutorialsattachment_email.php on line 77
Warning: Illegal string offset 'type' in C:XAMPPhtdocstutorialsattachment_email.php on line 81
Warning: Illegal string offset 'path' in C:XAMPPhtdocstutorialsattachment_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}

EDIT--尝试了新的解决方案:

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

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

    警告:file_get_contents(d):无法打开流:在C:\XAMPP\htdocs\tutorials\attachment_email.php的第77行中没有这样的文件或目录警告:C:\XAMPP\htdocs\tutorials\attachment_email.php中第81行的字符串偏移量"type"非法警告:第81行C:\XAMPP\htdocs\tutorials\attachment_email.php中的非法字符串偏移量"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:XAMPPhtdocstutorials中查找它们)。与脚本相关的文件位于何处?如果它们在你的根目录中,你应该能够在附件路径前面加上这样的前缀:

$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
    ),
    ...
);`

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

不幸的是,我通过修补Drupal模块的Mimemail解决了这个问题。如果有人在寻找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,有点冗长,但没有遗漏内容。)

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

相关内容

  • 没有找到相关文章

最新更新