dtry54612 2012-08-28 08:35
浏览 62
已采纳

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

I have an online PDF form which a user completes and then clicks a submit button which sends the form to an email address as an attachment and also stores the data in a database. However the completed individual field values do not appear when sent to an email address. How can I achieve this? many thanks.

//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 
 
$headers = "From: noreply@test.com
Reply-To: noreply@test.com"; 
//add boundary string and mime type specification 
$headers .= "
Content-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"; 
  • 写回答

1条回答 默认 最新

  • douxi4114 2012-08-28 09:14
    关注

    Here is your example using Swiftmailer, you will see that it's really easier !

    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"; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?