将已完成的PDF作为带有字段值的电子邮件附件发送


sending completed PDF as email attachment with field values

我有一个在线PDF表单,用户完成该表单,然后单击提交按钮,该按钮将表单作为附件发送到电子邮件地址,并将数据存储在数据库中。但是,当发送到电子邮件地址时,不会显示已完成的单个字段值。我怎样才能做到这一点?非常感谢。

//define the receiver of the email 
$to = 'test@*****.com'; 
//define the subject of the email 
$subject = 'Completed PDF form'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with 'r'n 
$headers = "From: noreply@test.com'r'nReply-To: noreply@test.com"; 
//add boundary string and mime type specification 
$headers .= "'r'nContent-Type: multipart/mixed; boundary='"PHP-mixed-".$random_hash."'""; 
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('purchase_order_form.pdf'))); 
//define the body of the message. 
ob_start(); //Turn on output buffering  
?> 
--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 
--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit
Hello World!!! 
This is simple text email message. 
--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit
--PHP-alt-<?php echo $random_hash; ?>-- 
--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: application/zip; name="purchase_order_form.pdf"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment  
<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 
<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail( $to, $subject, $message, $headers ); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 

下面是您使用Swiftmailer的示例,您会发现它真的更容易!

require_once '/path/to/swift-mailer/lib/swift_required.php';
// Create the Transport
$transport = Swift_MailTransport::newInstance();
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create the message
$message = Swift_Message::newInstance()
  // Give the message a subject
  ->setSubject($subject)
  // Set the From address with an associative array
  ->setFrom(array('noreply@test.com'))
  // Set the To addresses with an associative array
  ->setTo(array($to))
  // Give it a body
  ->setBody($message)
  // Optionally add any attachments
  ->attach(Swift_Attachment::fromPath('purchase_order_form.pdf'))
  ;
// Send the message
$result = $mailer->send($message);
echo $mail_sent ? "Mail sent" : "Mail failed";