Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
发送带有PHP问题的电子邮件pdf附件_Php_Html_Email - Fatal编程技术网

发送带有PHP问题的电子邮件pdf附件

发送带有PHP问题的电子邮件pdf附件,php,html,email,Php,Html,Email,我试图做一个脚本发送电子邮件和附加PDF或其他类型的文件以后 我一直在学习各种教程,已经走到了死胡同 有人看到我的代码有什么问题吗?我想我已经看了太久了 <?php include ("../../includes/auth.php"); $id = $_GET['id']; $date = date('y-m-d h:i:s'); $recipient = $_GET['email']; $lname = $_GET['lname'];

我试图做一个脚本发送电子邮件和附加PDF或其他类型的文件以后

我一直在学习各种教程,已经走到了死胡同

有人看到我的代码有什么问题吗?我想我已经看了太久了

<?php
    include ("../../includes/auth.php");

    $id = $_GET['id'];
    $date = date('y-m-d h:i:s');

    $recipient = $_GET['email'];
    $lname = $_GET['lname'];
    $fname = $_GET['fname'];
    $subject ="Enquiry Form - $date";
    //-------------attachment stuff----------
    $fileatt = "/test.pdf";
    $fileatttype = "application/pdf";
    //$fileattname = "newname.pdf";
    $file = fopen($fileatt, 'rb');
    $data = fread($file, filesize($fileatt));
    fclose($file);
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    //-------------------------------------
    $headers = "From: test person <info@test.co.uk>";
    $headers .= "\nMIME-Version: 1.0\n".
    "Content-Type: multipart/mixed;\n".
    " boundary=\"{$mime_boundary}\"";
    $message = "This is a multi-part message in MIME format.\n\n" .
    "--{$mime_boundary}\n".
    "Content-Type: text/plain; charset=\"iso-8859-1\"\n".
    "Content-Transfer-Encoding: 7bit\n\n". $message ."\n\n";
    $data = chunk_split(base64_encode($data));
    $message .= "–-{$mime_boundary}\n" .
    "Content-Type: {$fileatttype};\n" .
    " name=\"{$fileattname}\"\n" .
    "Content-Disposition: attachment;\n" .
    " filename=\"{$fileatt}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data . "\n\n" ."--{$mime_boundary}--\n";
    $message ."Dear ".$fname." ".$lname." Test message".$date;

    echo $recipient."<br />";
    echo $subject."<br />";
    echo $message."<br />";
    echo $headers."<br />";

    mail($recipient, $subject, $message, $headers);

    include ("../../includes/dbconn.php");

    $set_datesent = "UPDATE _leads SET `Covering_letter_sent` = '$date' WHERE `ID` = '$id'";
    //echo $set_datesent;
    $result = mysql_query ($set_datesent, $connection)
    or die ("Failed to perform query :  $sql <br />".mysql_error());
?>

我强烈建议使用像这样的库。它是免费的,大大简化了你的生活。

我建议使用Pear Mail\u mime软件包(http://pear.php.net/package/Mail_Mime/). 我曾经手工处理附件,因为我不知道有这样的库(愚蠢的我),当我发现它的时候,我立即摆脱了我糟糕的脚本,开始使用Mail_mime。它几乎没有小故障,但见鬼,它比手工编写代码容易得多。

我建议您使用类似于库的东西。然后发送附件就变得像下面的代码一样简单易读了。您必须确保安装了邮件库,但这是一项相当简单的任务

require_once('Mail.php');
require_once('Mail/mime.php');

$text = wordwrap($message, 70); // You'd put the text version of your message here as $message

$mime = new Mail_Mime();
$mime->setTXTBody($text);
$mime->setFrom($name . ' <' . $from_email . '>'); // The sender name and email ID go here
$mime->addAttachment($fpath, $ftype); // File path and type go here
$mime->setSubject($subject); // Subject goes here

$body = $mime->get()
$headers = $mime->headers();

$mail =& Mail::factory('mail');
$mail->send($to_email, $headers, $body); // The recipients email address goes here

unset($mime, $mail);
require_once('Mail.php');
需要_once('Mail/mime.php');
$text=wordwrap($message,70);//您可以将消息的文本版本作为$message放在此处
$mime=新邮件_mime();
$mime->setTXTBody($text);
$mime->setFrom($name.'');//发件人姓名和电子邮件ID在此处显示
$mime->addAttachment($fpath,$ftype);//文件路径和类型转到此处
$mime->setSubject($subject);//主题在这里
$body=$mime->get()
$headers=$mime->headers();
$mail=&mail::工厂('mail');
$mail->send($to_email,$headers,$body);//收件人的电子邮件地址在此处
unset($mime,$mail);

通过这种方式,其他事情也变得容易得多,例如,编写HTML邮件。

尝试提供完整路径$fileatt=“/test.pdf”;你在哪里指定你的问题是什么?还有。两者在功能/可用性方面大致相当。@webnet您好,您能帮我使用swiftmailer吗?我已经使用文档编写了一个脚本,我只是遇到了一些错误:(糟糕的时候)。