【问题标题】:How to attach PDF to email using PHP如何使用 PHP 将 PDF 附加到电子邮件
【发布时间】:2017-07-06 09:25:51
【问题描述】:

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

我想将指定的 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");

【问题讨论】:

  • 帮自己一个忙,使用现有的库,如 PHPMailer。将消除这样做的所有痛苦。

标签: php forms pdf email-attachments


【解决方案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);

【讨论】:

  • 您好,您完成了吗,或者遇到任何错误,您还使用 php mailer 类使用您的服务器主机、用户名和密码使用 SMTP 发送邮件。 SMTP 还允许从本地主机发送邮件。所以如果你想要 SMTP 示例回复我,我也会向你提供他的详细信息
  • 在下面的例子中你需要传递 //$mail->SMTPDebug = 3; //启用详细调试输出 $mail->isSMTP(); //设置邮件使用 SMTP $mail->Host = 'smtp1.example.com;smtp2.example.com'; //指定主备SMTP服务器 $mail->SMTPAuth = true; //启用 SMTP 认证 $mail->Username = 'user@example.com'; //SMTP 用户名 $mail->Password = 'secret'; //SMTP 密码 $mail->SMTPSecure = 'tls'; //启用TLS加密,ssl也接受$mail->Port = 587; $mail->setFrom('from@example.com', 'Mailer'); $mail->addAddress('joe@example.net', 'Joe 用户'); // 添加收件人
  • 在您的 cpanel 中创建一封电子邮件并使用他的 smtp 详细信息,或者在此处传递 GMAIL 或任何其他电子邮件 smtp 详细信息以从您域中的 localhost 发送邮件,可能 gmail 或其他客户端详细信息不适用于安全性原因,为此,您必须将您的域电子邮件传递给它。 (abc@example.com) 其中 example.com 是您自己的域
  • 更新您的问题并添加最新代码,以便我检查并回复您。
  • 给我你的电话号码或邮箱,我会在whatapp上给你发错误信息
【解决方案2】:

从下面给出的链接下载 PHPMailer 库:

https://github.com/PHPMailer/PHPMailer

然后使用这段代码

require_once('../PHPMailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->From      = '';
$mail->FromName  = '';
$mail->Subject   = '';
$MESSAGE_BODY = "Name: ".$name." "."\r\n";
$MESSAGE_BODY .= "Email: ".$email." "."\r\n";
$MESSAGE_BODY .= "Contact No.: ".$cno." "."\r\n";
$MESSAGE_BODY .= "Message: ".$message." "."\r\n";
$mail->Body      = $MESSAGE_BODY;
$mail->AddAddress( '' );

$mail->AddAttachment($_FILES['file']['tmp_name'],
                 $_FILES['file']['name']);

if ($mail->Send())
{   
    echo "Mail Sent";
}
else
{
    echo "Could not send mail";
}

它会正常工作的。

【讨论】:

  • 我用过,但它无法正常显示“无法发送邮件”
  • 发件人 = ''; $mail->FromName = ''; $mail->主题 = ''; $MESSAGE_BODY = "名称:".$username." "."\r\n"; $MESSAGE_BODY .= "联系电话:".$contact." "."\r\n"; $MESSAGE_BODY .= "消息:".$message." "."\r\n"; $mail->正文 = $MESSAGE_BODY; $mail->AddAddress(''); $mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['username']); if ($mail->Send()) { echo "邮件已发送"; } else { echo "无法发送邮件"; } ?>
  • 是的,兄弟,但它仍然无法正常工作。 ;(请发给我完整的php代码..
  • 我已经给你发了完整的代码.....尝试先发送没有附件的电子邮件,还有一件事是你的应用程序是在线还是离线?
  • 只需将错误图像文件上传到 onionfiles.com 并给我链接。
猜你喜欢
  • 2012-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-12
  • 1970-01-01
  • 1970-01-01
  • 2010-12-07
  • 2012-12-14
相关资源
最近更新 更多