2016-04-28 46 views
-1

我正在使用下面的代码,它正在工作。但是附加的代码不起作用,并且发送邮件失败。这可能是做错了头?我正在尝试将表单数据发送到pdf并附上邮件

这里的$htmlTable变量用于收集表单后值并使用html转换为pdf函数将其转换为pdf。 但邮件不是用pdf发送的。

$pdf->WriteHTML2("<br><br><br>$htmlTable"); 
$pdf->SetFont('Arial','B',6); 
//Create PDF file with data 
$semi_rand = md5(time()); 
$pdf1 = $htmlTable; 

$file = $semi_rand . ".pdf"; 
$handle = fopen($file, 'w+'); 
fwrite($handle, $pdf); 
fclose($handle); 
// Additional headers 
    $subject = "User Form"; 
    $content = chunk_split(file_get_contents($file)); 
     $uid = md5(uniqid(time())); //unique identifier 

     //standard mail headers 
     $header = "From: ".$from."\r\n"; 
     $header .= "Reply-To: ".$_POST['email']. "\r\n"; 
     $header .= "MIME-Version: 1.0\r\n"; 


     //declare multiple kinds of email (plain text + attch) 
     $header .="Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; 
     $header .="This is a multi-part message in MIME format.\r\n"; 

     //plain txt part 

     //$header .= "--".$uid."\r\n"; 
    // $header .= "Content-type:text/plain; charset=iso-8859-1\r\n"; 
    // $header .= "Content-Transfer-Encoding: bit\r\n\r\n"; 
     //$header .= 'sample'. "\r\n\r\n"; 


     //attch part 
     $header .= "--".$uid."\r\n"; 
     $header .= "Content-Type: application/octet-stream; name=\"".$file."\"\r\n"; 
     $header .= "Content-Transfer-Encoding: base64\r\n"; 
     $header .= "Content-Disposition: attachment; filename=\"".$file."\"\r\n\r\n"; 
     $header .= $content."\r\n\r\n"; //chucked up 64 encoded attch 
     $success = mail($to,$subject,'PFA',$header); 
     if (!$success) { 
     echo "Mail to " . $to . " failed ."; 
     }else { 
     echo "Success : Mail was send to " . $to ; 
     print_r ($header); 
     } 
+1

你用[PHPMailer](https://github.com/PHPMailer/PHPMailer)标记了这个问题,但你没有使用它。你应该,那么你不必担心如何正确构建附件。通常,一次解决一个问题 - 在尝试发送PDF之前检查您的PDF版本是否正在运行。另外,看看'file_put_contents',这比用fopen拖拽要简单得多。 – Synchro

+0

是的,我的pdf版本正在工作,但我遇到问题发送邮件附件... – sourabh

+0

如何“附加代码”工作和“下面的代码”不工作? – symcbean

回答

2

您应该使用PHPMailer类,因为附加文件并通过电子邮件发送更简单。

发送一封电子邮件,附件可以是一个例子:

<?php 
require 'PHPMailerAutoload.php'; 

$mail = new PHPMailer; 

//$mail->SMTPDebug = 3;        // Enable verbose debug output 

$mail->isSMTP();          // Set mailer to use SMTP 
$mail->Host = 'smtp1.example.com'; // Specify main and backup SMTP servers 
$mail->SMTPAuth = true;        // Enable SMTP authentication 
$mail->Username = '[email protected]';     // SMTP username 
$mail->Password = 'secret';       // SMTP password 
$mail->SMTPSecure = 'tls';       // Enable TLS encryption, `ssl` also accepted 
$mail->Port = 587;         // TCP port to connect to 

$mail->setFrom('[email protected]', 'Mailer'); 
$mail->addAddress('[email protected]', 'Joe User'); 

$mail->addAttachment('/var/tmp/file.tar.gz');   // Add attachments 
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name 
$mail->isHTML(true);         // Set email format to HTML 

$mail->Subject = 'Attachment is here'; 
$mail->Body = 'Here goes the attachment; 

if(!$mail->send()) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
} else { 
    echo 'Message has been sent'; 
} 

我已经稍微修改可以在我上面共享的链路中找到的例子。

由于同步建议您可以使用addStringAttachment方法,以跳过整个写的PDF文件步骤,直接从内存中发送。

+1

如果您使用PHPMailer的'addStringAttachment'方法,您可以跳过整个将PDF写入文件步骤并直接从内存中发送。 – Synchro

相关问题