附加生成的pdf以形成并发送到phpmailer

时间:2017-06-22 16:49:09

标签: php pdf phpmailer

我能够生成pdf并将生成的pdf作为附件发送到输入的email-id。但我想要的东西,当我点击电子邮件时,它应生成pdf并打开一个表单,其中包含用户输入字段,如收件人,邮件,主题....等...而生成的pdf应显示为附件

现在点击电子邮件时会打开一个用户输入表格,然后当我点击发送时,它会生成pdf并发送该pdf。

这是我的代码

我的html表单

<form method="post" action="send_mail.php" enctype="multipart/form-data">
<div class="formSep">
<label class="req">To Email: </label>
<input type="text" name="email" /> &nbsp;&nbsp;&nbsp;&nbsp; <select name="email"><option value="">Select one</option><?php $s1 = mysql_query("select * from lead_contact where company_id=".$company."");
while($r1 = mysql_fetch_array($s1)) { $name = $r1['firstname'].' '.$r1['lastname'];
$cid = $r1['con_id'];
$cemail = $r1['email']; ?>
<option value="<?php echo $cemail;?>"><?php echo $name;?></option>
<?php
}
?>
</select>
</div>
<input type="hidden" name="order_id" value="<?php echo $order_id; ?>" />
<input type="hidden" name="company" value="<?php echo $company; ?>" />

<div class="formSep">
<label class="req">Subject</label>
<input type="text" name="subject" /></div>

<div class="formSep">
<label class="req"> Message</label>


<div class="w-box" id="n_wysiwg">
<div class="w-box-header">
<h4>WYSIWG Editor</h4>
</div>
<div class="w-box-content cnt_no_pad">
<textarea name="message" id="wysiwg_editor" cols="30" rows="10"></textarea>
</div>
</div>
</div>


<div class="formSep">

<input type="submit" name="submit" value="Submit" class="btn btn-info" /></div>

</form>

send_email.php

<?php
if($_POST['submit'] == "Submit")
{

    $id = $_POST['order_id'];
    $company = $_POST['company'];
    include("../admin_auth.php");


include("../connect.php");
require('invoice.php');

$pdf = new PDF_Invoice( 'P', 'mm', 'A4' );
$pdf->AddPage();


//MY PDF CODE GOES HERE

$pdf->Output("D:/wamp/www/folder/uploads/".$id.".pdf","F");

$path = "D:/wamp/www/folder/uploads/".$id.".pdf";
require("class.phpmailer.php");
require("class.smtp.php");



$to = $_POST['email'];
$from = $_SESSION['email'];
$message = $_POST['message'];

$subject = $_POST['subject'];

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = "smtp";
$mail->Host = "hostname";

$mail->SMTPAuth = true;
$mail->Username = 'user';
$mail->Password = 'password';
$mail->Port = 587;
$mail->From=$_SESSION['email'];
$mail->FromName=$_SESSION['name'];
$mail->Sender=$email;


$mail->AddAddress($to);



$mail->AddBCC("test@test.com");
$mail->AddAttachment($path, '', $encoding = 'base64', $type = 'application/pdf');
$mail->Subject = $subject;

$mail->CharSet="windows-1251";
$mail->CharSet="utf-8";

$mail->IsHTML(true);


$mail->Body = $message;


if(!$mail->Send())
{
echo "Error sending: " . $mail->ErrorInfo;;
}
else
{
echo "Email Sent!";
}
}
?>

我没有得到如何获得附加了pdf的表单,以便用户可以在正文中写入,添加cc并发送电子邮件

2 个答案:

答案 0 :(得分:1)

试试这个..

$path = "D:/wamp/www/folder/uploads/".$id.".pdf";

$mail->addCC('cc@example.com');    //Cc stands for carbon copy
$mail->addBCC('bcc@example.com'); //Bcc stands for blind carbon copy

$mail->AddAttachment($path, '', $encoding = 'base64', $type = 'application/pdf');

注意:参考PHP MAILER:

检查一下: https://github.com/PHPMailer/PHPMailer

  

Cc 代表抄送,这意味着其地址出现在之后   Cc:标头将收到消息的副本。此外,抄送   标题也会出现在收到的邮件的标题内。

     

Bcc 代表盲目抄送,类似于Cc,除外   在此字段中指定的收件人的电子邮件地址   不会出现在收到的邮件标题和收件人中   或者抄送字段不会知道发送到这些地址的副本。

答案 1 :(得分:0)

如果您正确使用基本的PHP语法,这确实很有帮助。这没有意义:

$mail->AddAttachment($path, '', $encoding = 'base64', $type = 'application/pdf');

所有你需要的是:

$mail->AddAttachment($path);

它将为您完成其余的工作。