2016-04-28 28 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

相關問題