如何使用PHP

时间:2017-07-06 09:25:51

标签: php forms pdf email-attachments

  

这是php代码及其工作,所有表单内容成功发送到邮件ID,但我想将PDF文件附加到电子邮件使用php这个功能。   但我怎么可能,我不知道。

我想将指定的PDF文件添加为电子邮件的文件附件。我该怎么做?

<?php
$name  = $_REQUEST["name"];
$phone = $_REQUEST["phone"];
$email = $_REQUEST["email"];
$address = $_REQUEST["address"];
$message = $_REQUEST["message"];
date_default_timezone_set("Asia/Calcutta");
$todayis = date("y-m-d H:i:s", time());
$subject = "PVC Order From Website ";
$to      = "iamrightful@gmail.com";
$msg .= "<table width='600' border='0' align='center' cellpadding='0' ellspacing='0' style='font-family:Arial, Helvetica, sans-serif; 
font-size:13pt; border:1px solid #0f3f6a;'>";
$msg .= "<tr>";
$msg .= "<td width='16' height='25' bgcolor='#f9cf7b'>&nbsp;</td>";
$msg .= "<td width='96' bgcolor='#f9cf7b'>Name</td>";
$msg .= "<td width='10' height='25' bgcolor='#f9cf7b'><strong>:</strong></td>";
$msg .= "<td height='25' bgcolor='#f9cf7b'>$name</td>";
$msg .= "</tr>";
$msg .= "<tr>";
$msg .= "<td height='25' bgcolor='#f9cf7b'>&nbsp;</td>";
$msg .= "<td height='25' bgcolor='#f9cf7b'>Mobile </td>";
$msg .= "<td height='25' bgcolor='#f9cf7b'><strong>:</strong></td>";
$msg .= "<td height='25' bgcolor='#f9cf7b'>$phone</td>";
$msg .= "</tr>";
$msg .= "<tr>";
$msg .= "<tr>";
$msg .= "<td height='25' bgcolor='#f9cf7b'>&nbsp;</td>";
$msg .= "<td height='25' bgcolor='#f9cf7b'>Email Id </td>";
$msg .= "<td height='25' bgcolor='#f9cf7b'><strong>:</strong></td>";
$msg .= "<td height='25' bgcolor='#f9cf7b'>$email</td>";
$msg .= "</tr>";
$msg .= "<tr>";
$msg .= "<td height='25'>&nbsp;</td>";
$msg .= "<td height='25'>Address</td>";
$msg .= "<td height='25'><strong>:</strong></td>";
$msg .= "<td height='25'>$address</td>";
$msg .= "</tr>";
$msg .= "<tr>";
$msg .= "<td height='25'>&nbsp;</td>";
$msg .= "<td height='25'>Discription</td>";
$msg .= "<td height='25'><strong>:</strong></td>";
$msg .= "<td height='25'>$message</td>";
$msg .= "</tr>";
$msg .= "</table>";
$headers = "From: $c_name < $c_email >.";
$headers = "Bcc: admin < $Bcc >.";
$headers .= "\r\nContent-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $msg, $headers);
header("Location:thankyou.html");

2 个答案:

答案 0 :(得分:1)

通常,您会将三个值传递给mail()函数以及一些标题。在下面的示例中,我跳过了消息值的值,因为消息被定义为MIME部分以及附件。

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
 $file = $path.$filename;
 $file_size = filesize($file);
 $handle = fopen($file, "r");
 $content = fread($handle, $file_size);
 fclose($handle);
 $content = chunk_split(base64_encode($content));
 $uid = md5(uniqid(time()));
 $header = "From: ".$from_name." <".$from_mail.">\r\n";
 $header .= "Reply-To: ".$replyto."\r\n";
 $header .= "MIME-Version: 1.0\r\n";
 $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
 $header .= "This is a multi-part message in MIME format.\r\n";
 $header .= "--".$uid."\r\n";
 $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
 $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
 $header .= $message."\r\n\r\n";
 $header .= "--".$uid."\r\n";
 $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
 $header .= "Content-Transfer-Encoding: base64\r\n";
 $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
 $header .= $content."\r\n\r\n";
 $header .= "--".$uid."--";
 if (mail($mailto, $subject, "", $header)) {
 echo "mail send ... OK"; // or use booleans here
 } else {
 echo "mail send ... ERROR!";
 }
}

以下是我如何使用此功能发送带有一个附加.zip文件的电子邮件的示例:

$my_file = "somefile.zip";
$my_path = "/your_path/to_the_attachment/";
$my_name = "Olaf Lederer";
$my_mail = "my@mail.com";
$my_replyto = "my_reply_to@mail.net";
$my_subject = "This is a mail with attachment.";
$my_message = "Hallo,rndo you like this script? I hope it will help.rnrngr. Olaf";
mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);

答案 1 :(得分:1)

从以下链接下载PHPMailer库:

https://github.com/PHPMailer/PHPMailer

然后使用此代码

ctrl+s = save record

它会正常工作。