I have a form on a website that customers fill. When user filles the form and clicks submit data is sent to a pdf(invoice) I've already created and certain spots in pdf are filled with that data. I did this using PDFTK library:
public function generate($data)
{
$filename = date("d-m-Y-His") . ".pdf";
$pdf = new Pdf('./form.pdf');
$pdf->fillForm($data)
->flatten()
->saveAs('./completed/' . $filename);
$path = './completed/' .$filename;
return $path;
}
The problem is, i dont know how to send this filled pdf via PHPMailer library, as pdf is recquired to be a string in order to work with phpmailer.
$pdf = new GeneratePDF;
$response = $pdf->generate($data);
$email = new PHPMailer();
$email->SetFrom('you#example.com', 'Your Name');
$email->Subject = 'Test';
$email->Body = 'Test';
$email->AddAddress( 'mail exmp' );
$email->AddAttachment( $response , 'NameOfFile.pdf' );
$email->Send();
And how to actually save file in cpanel server, as when i do this it doesnt work on a server.
pdf is recquired to be a string in order to work with phpmailer
This is not true. addAttachment() requires that you pass in a path to a local file on disk, so you could do:
$email->addAttachment(generate(), 'NameOfFile.pdf');
You can pass in a binary string using addStringAttachment(), but that's not what you're asking for here.
Related
I use dompdf to generate a PDF file, Then I send it using PHPmailer. But the issue is that the file name of the generated PDF name is not correct.
Here is the code:
//PDF from dompdf
$pdf = $dompdf->output();
//PHPmailer confiurgation
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
//PDF name
$name = 'محمد احمد علي';
$pdfName = str_replace(" ", "_", $name); //Replace spaces with _
$pdfName .= ".pdf";
//Attach PDF
$mail->AddStringAttachment($pdf, $pdfName);
The PDf name should be محمد_احمد_علي.pdf in this case. But it's _احمد_علي.pdf
I tried:
$encoding = 'base64';
$type = 'application/pdf';
$mail->AddStringAttachment($pdf, $pdfName, $encoding, $type);
Also:
$mail->Encoding = "base64";
But no success.
I don't see that there is an issue with the string name, As when I run the following code:
$name = 'محمد علي احمد';
$name = str_replace(' ', "_", $name);
echo $name . '.pdf';
I get the expected result.
How to solve this issue?
Update
I just tried:
$dompdf->stream($filename);
To download the PDF without sending it. And it's the same result, So I don't know where the issue is coming from.
New Update
In case this happens to anyone in the future.
I found out why this is happening. Dompdf is using basename() on the file name. And with my Arabic name that function makes it go wrong like that.
It's really sad how programming languages and libraries are ignoring RTL languages like Arabic.
So running this code:
echo basename('محمد احمد علي');
Would return احمد علي
I am trying to add an inline image to an email sent using the mailgun api using only the contents of the image file.
As far as i know mailgun-php only allows for paths to be specified in the inline element.
Is there a way to add images using their contents without modifying the mailgun-php library?
I've tried inlining the whole thing inside of html img tag but that doesn't work in all email clients (for instance gmail):
<img src="data:image/png;base64,BASE64CONTENTSHERE==" />
I have not found a way to do this without modifying the mailgun-php code so I eneded up using the following to store the file in the temporary folder.
$prefix = 'someprefix'; // Used to easily identify the file if needed
$filename = uniqid($prefix, true).'.png';
$path = sys_get_temp_dir().DIRECTORY_SEPARATOR.$filename;
$f = fopen($path, 'w');
fwrite($f, $binaryImageContent);
fclose($f);
Then to send the email and remove the temporary file
$mailgun = new \MailgunClient();
$response = $mailgun->sendEmail(
$fromEmail,
$replyTo,
array($toEmail),
$subject,
$html,
array(
'inline' => $path
)
);
unlink($path);
Below is what the MailgunClient class looks like, I had to explicitly set the Guzzle http client, since the default was failing. Note MAILGUN_API_KEY and MAILGUN_DOMAIN have to be set as constants somewhere else in the code using define('MAILGUN_API_KEY', 'key_here'); and define('MAILGUN_DOMAIN', 'domain.com');
<?php
use Mailgun\Mailgun;
class MailgunClient {
public function sendEmail($from, $replyTo, $to, $subject, $html, $inline = null) {
$client = new \Http\Adapter\Guzzle6\Client();
$mailgun = new \Mailgun\Mailgun(MAILGUN_API_KEY, $client);
$domain = MAILGUN_DOMAIN;
$params = array();
$params['from'] = $from;
$params['h:Reply-To'] = $replyTo;
$params['subject'] = $subject;
$params['text'] = $html;
$params['html'] = $html;
$params['inline'] = $inline;
$params['to'] = $to;
$mailgun->sendMessage($domain, $params, $inline);
}
}
Finally in the email use the following tag to reference the inlined image. The src attribute has to be cid:the_filename_created to match what the library outputs in the email.
<img src="cid:'.$filename.'.png" alt="logo" style="width:auto;height:150px;max-height:150px;"/>
I am using a free extension of opencart which allows me to download invoice in PDF to my local system (Downloads folder).
I want two things to happen
I want to download PDF file in my root. (if I am working on localhost I want the file to download in root\abc\ )
The other thing i want is that there is an email textbox and email button what I want is that I want to send email to user(eAddress written in textbox) with auto attachment of that downloaded pdf file located in(root\abc)
CODE :
$pdf = (isset($this->request->get['pdf'])) ? true : false;
$eFlag= (isset($this->request->get['email'])) ? true : false;
if ($pdf){
$this->response->setOutput(pdf($this->render(),$this->data['orders']));
}
elseif ($eFlag) {
$fp = fopen(DIR_DOWNLOAD . "meeru.pdf","wb");
$dataHtml = $this->render();
$name = $this->data['orders'];
if (count($name) > 1) {
$name = "Orders";
}else{
$name = 'Order_'.$name[0]['order_id'];
}
$pdf = new DOMPDF;
$pdf->load_html($dataHtml );
$pdf->render();
fwrite($fp , $pdf->output( array("compress" => 0) ));
fclose($fp );
$this->response->setOutput("File Saved");
}
else{
$this->response->setOutput($this->render());
}
Its downloading just one time (for the first time in root/download)
I'm using mPDF to generate PDF's when a button is clicked and the i save them inside a folder. I am looking for a way to add the PDF to an attachment using PHPmailer. Here is what I've tried:
$dir = $_SERVER['DOCUMENT_ROOT'].dirname($_SERVER['PHP_SELF']);
$pdfexist = $dir."/classes/pdf/feestructure_".$student['rollno'].".pdf";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->From="xyz#abc.com";
$mail->FromName="xyz";
$mail->addAddress("xyz#abc.com");
//echo $email."<br/>";
$mail->addAddress("xyz#abc.com");
$mail->addAddress("xyz#abc.com");
$mail->Subject = 'XYZ';
$pdfstring = $pdfexist;
$mail->AddStringAttachment($pdfstring, "feestructure_".$roll.".pdf", $encoding = 'base64', $type = 'application/pdf');
The size of my generated pdf is 13k but its showing 1 k in mail attachment.help me guys.
Here is the output from mpdf:
$mpdf->WriteHTML(file_get_contents("$dir/feestructure_pdf.php?rollno=$rollno"));
$pdfname="feestructure_".$rollno.".pdf";
$mpdf->Output("classes/pdf/".$pdfname,"F");
Your $pdfexists / $pdfstring variable contains a file path, not binary PDF data, so you should be using AddAttachment(), not AddstringAttachment(). AddAttachment attaches files (like your PDF), AddStringAttachment attaches strings, like what you might get back from a web call or a database.
$mail->AddAttachment($pdfstring, "feestructure_".$roll.".pdf", $encoding = 'base64', $type = 'application/pdf');
I have tried email extensions in yii twice.
1.YII-MAIL
2.PHP MAILER
Now i would like to try out swift mailer.I have downloaded the package from here http://swiftmailer.org/download and added it to the extensions folder in YII.
Here i have a form ,with fields for name,email,phone and an attachment.
I am saving the file uploaded to a folder called resumes under images folder ,at the same time i am sending a mail with all details along with the uploaded file as an attachment .But on clicking create button i am getting this error
include(Swift_Message.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory
Here is the controller action i have tried so far
public function actionCreate()
{
$this->layout='static_inner';
$model=new LriCareer;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['LriCareer']))
{
$rnd = rand(0,9999);
$model->attributes=$_POST['LriCareer'];
if($uploadedFile=CUploadedFile::getInstance($model,'career_resume'))
{
$fileName = "{$rnd}-{$uploadedFile}"; // random number + file name
$model->career_resume = $fileName;
if($model->save())
{
$uploadedFile->saveAs(dirname(Yii::app()->basePath) . '/images/resumes/'.$fileName);
$message = new YiiMailMessage;
$first_name="hello";
$message->setBody($first_name);
$message->subject = 'My Subject';
$message->addTo('fazeela.ma#longriverinfotech.com');
$message->from = Yii::app()->params['adminEmail'];
$uploadedFile = CUploadedFile::getInstanceByName('fileupload'); // get the CUploadedFile
$uploadedFileName = $uploadedFile->tempName; // will be something like 'myfile.jpg'
$swiftAttachment = Swift_Attachment::fromPath($uploadedFileName);
$message->attach($swiftAttachment);
}
}
else
{
if($model->save())
$this->redirect(array('view','id'=>$model->career_id));
}}
$this->render('create',array(
'model'=>$model,
));
}
I dont see Swift_message.php in the folder.Can any one out there can look into the problem
There is a Swiftmailer Extension for Yii Framework. I've personally used in a couple of projects and its awesome.
How to attach Files:
$message = new YiiMailMessage;
$message->setBody($first_name);
$message->subject = 'My Subject';
$message->addTo('my#domain.com');
$message->from = Yii::app()->params['adminEmail'];
$uploadedFileName = CUploadedFile::getInstance($model,'career_resume');
$uploadedFileName = $uploadedFile->tempName; // will be something like 'myfile.jpg'
$swiftAttachment = Swift_Attachment::fromPath($uploadedFileName);
$message->attach($swiftAttachment);