So I work with codeigniter and I generate a page.
I want to send a mail with the page attached as PDF but I also want not to save it on the server.
I found 2 code resources that look like doing exactly what I need:
<?php
$content = "
<page>
<h1>Exemple d'utilisation</h1>
<br>
Ceci est un <b>exemple d'utilisation</b>
de <a href='http://html2pdf.fr/'>HTML2PDF</a>.<br>
</page>";
require_once(dirname(__FILE__).'/html2pdf/html2pdf.class.php');
$html2pdf = new HTML2PDF('P','A4','fr');
$html2pdf->WriteHTML($content);
$html2pdf->Output('exemple.pdf');
?>
For creating a PDF using html2pdf library
<?php
require_once (dirname)(__FILE__).'/html2pdf/html2pdf.class.php');
require_once (dirname)(__FILE__).'/pjmail/pjmail.class.php');
?>
<?php
$pdf = new HTML2PDF ('P', 'A4');
$pdf->WriteHTML($content);
$content_pdf = $pdf->Output('document.pdf', true);
$mail = new PJmail();
mail->setAllFrom('webmaster#my_site.net', "My Site");
$mail->addrecipient('mymail#my_site.net');
$mail->addsubject("test");
$mail->text"Insert some text here...";
$mail->addbinattachement('test.pdf', $content_pdf);
echo $mail->sendmail();
?>
For sending the email with a PDF attachment.
Now I have to say that I am a beginner in PHP and I KIND OF understand what the code does. I don't know how to pass the link to the $content variable.
Someone told me that I should use a function like curl or file_get_contents but it is not clear to me at all. Could someone explain me a little?
I send you code with dom pdf in which email send with pdf and after sent email pdf delete
ob_start();
set_include_path(get_include_path() . PATH_SEPARATOR . "/path/to/dompdf");
require_once APPPATH . "core/dom_pdf/dompdf_config.inc.php";
$dompdf = new DOMPDF();
$dompdf->set_paper(DEFAULT_PDF_PAPER_SIZE, 'A4');
$dompdf->load_html($message);
$dompdf->render();
$output = $dompdf->output();
file_put_contents('pdf_name.pdf', $output);
$this->load->library('email');
$config = array (
'mailtype' => 'html',
'charset' => 'utf-8',
'priority' => '1'
);
$this->email->initialize($config);
$this->email->from('admin#admin.com', 'Admin');
$this->email->to(Reciever email);
$this->email->subject('Subject');
$this->email->message($message);
$this->email->attach(FCPATH."pdf_name.pdf");
$this->email->send();
<!-- If you want to delete file after send email -->
unlink(FCPATH."pdf_name.pdf");
Related
What is the correct way to generate an email attachment with PhpWord?
I tried multiple ways but I get a corrupt file with size 1kb. If I trigger a download of the file instead, the file is OK.
$fileName = 'file.docx';
$fileAttachment = tempnam(sys_get_temp_dir(), 'PHPWord');
$template->saveAs($fileAttachment);
$contentType = "application/octet-stream";
$notification
->setTemplate("template", $templateParams)
->setSubject($this->_("Email subject"))
->addRecipient("to", $email, $email)
->addAttachment($fileName, $fileAttachment, $contentType);
What am I doing wrong?
Thank you!
You could try to use file_get_contents function to read the contents of the file and then pass it as the second parameter to the addAttachment() method:
$fileAttachmentContent = file_get_contents($fileAttachment);
$notification
->setTemplate("template", $templateParams)
->setSubject($this->_("Email subject"))
->addRecipient("to", $email, $email)
->addAttachment($fileName, $fileAttachmentContent, $contentType);
PHPWord is for generating Word documents, but it does not send mails.
You can combine it with any PHP mailer library. This is a sample on how to do it using SwitfMailer:
<?php
require_once '[...]/phpword/src/PhpWord/Autoloader.php';
require_once '[...]/swiftmailer/lib/swift_required.php';
// create a new document with phpWord
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$text = $section->addText("Here your text...");
// Use Swift Mailer to create an attachment object...
$attachment = new Swift_Attachment(
$phpWord->save(),
'file.docx',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
);
// ...and send the email
$transport = Swift_SmtpTransport::newInstance('smtp.yoursmtp.com', 25);
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Here your subject')
->setFrom(['sender#sdomain.net' => 'Sender Name'])
->setTo(['receiver#rdomain.org' => 'Receiver Name'])
->setBody('Here the body message...')
->attach($attachment);
$result = $mailer->send($message);
You can use any other mail library you like. Or even use the PHP mail() function, but is more tricky to set the mail headers properly.
I'm using this library from Dompdf, after showing the file as a PDF I want to send it to my email, I have tried several methods but I have not found a solution so far.
Thanks in advance
$dompdf = new Dompdf();
$dompdf->loadHtml(html_entity_decode($output));
$dompdf->setPaper('A4');
$dompdf->render(); // Render the HTML as PDF
$dompdf->stream($invoiceFileName, array("Attachment" => false));
Maybe this way will work for you:
<?php
$dompdf = new Dompdf();
$dompdf->loadHtml(html_entity_decode($output));
$dompdf->setPaper('A4');
$dompdf->render(); // Render the HTML as PDF
// https://github.com/dompdf/dompdf/wiki/Usage#output
$output = $dompdf->output();
// Save PDF Document
file_put_contents('Document.pdf', $output);
// Use PHPMailer
// $mail->isSMTP();
// $mail->Host = 'smtp.example.org';
// $mail->SMTPAuth = true;
// $mail->Username = 'alice';
// $mail->Password = '123456'
// $mail->SMTPSecure = 'tls';
// $mail->Port = 25;
$mail->addAttachment('Document.pdf');
$mail->send();
?>
I gather from the DOMPDF docs that you can get a PDF binary string without saving it to a file by doing this:
$pdfData = $dompdf->output();
and then you can pass that directly to PHPMailer as a string attachment so your PDF data never has to be written to a temporary file:
$mail-addStringAttachment($pdfData, 'file.pdf');
PHPMailer will take care of the encoding and MIME type for you.
I am trying to create a PDF stream using FPDF library and to send the pdf over e-mail using Swift Mailer. Below is my code. The mail is sent successfully and even pdf is also attached but the pdf is blank. It has a size of 1Kb and can be opened as a pdf.
My code is :
<?php
include('./fpdf/fpdf.php');
require_once './lib/swift_required.php';
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Text(40, 10, "Dummy text");
$data=$pdf->Output('./emails/order.pdf', 'F');
$message = Swift_Message::newInstance('Subject')
->setFrom(array("admin#mysite.com" => 'Company Admin'))
->setTo('my#email.com')
->setBody('This is body text', 'text/html');
$attachment = Swift_Attachment::fromPath('./emails/order.pdf');
//$attachment = Swift_Attachment::newInstance($data, 'pdf_name.pdf', 'application/pdf');
$message->attach($attachment);
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);
I referred to this question previous question
I finally got it to work. I changed the code as follows:
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
as edited above and it worked. I think the problem was in in the AddPage.
I have a problem with PHPMailer. It seems that i have customers that do no receive the attachment in their outlook box. Below is the code i am using. I tested it at my private gmail where the attachment is visible.
Could it be because i am using a stringAttachment instead of a real file?
$mail = new SMail();
$mail->SetFrom('administratie#domain.nl', 'Domain b.v.');
$mail->AddAddress($invoice->SAddress()->email);
$mail->Subject = 'Factuur ' . $invoice->getReferenceNumber();
$mail->AddStringAttachment($this->getAction($invoice->invoiceId, null, 'S'), $invoice->getReferenceNumber() . '.pdf', 'base64', 'application/pdf');
$this->objTemplate->assign(array(
'title' => 'Uw factuur',
'referenceNumber' => $invoice->getReferenceNumber(),
));
$mail->MsgHTML($this->objTemplate->render('mail/invoice.tpl', false, true));
The problem was the fact that outlook blocked the inline image content on which also the attachment broke somehow.
I'm creating a CSV on the fly with PHP, I then need to attach this CSV file to the the Swift Mailer Message. I have tried using file_get_content on the created file aswell as using chunk_split(base64_encode(file_get_contents()) on the created file aswell as attaching the file before writing it to disk. Without writing to disk I get Rescource #183 in the CSV, with attaching it with file_get_content I get just a string in each row of the CSV file, anyone know what I'm doing wrong?
if(!file_exists(_PS_ORDERS_DIR_.$orderDate.'/'.$file_name.'.csv'))
{
if($file = fopen (_PS_ORDERS_DIR_.$orderDate.'/'.$file_name.'.csv', 'x+'))
{
foreach ($list as $fields)
{
fputcsv($file, $fields);
}
$attachment['mime'] = 'application/vnd.ms-excel';
$attachment['content'] = file_get_contents($file);
$attachment['name'] = $order.'order';
EDIT
Mail::Send(1, 'order_conf', 'Order CSV Attachment', $success, 'test#email.com', Address, NULL, NULL, $attachment); // attach and send
}
}
Attaching a file into a swift mailer:
$swift =& new Swift(new Swift_Connection_SMTP(MAIL_SMTP_URL, MAIL_SMTP_PORT));
$message =& new Swift_Message($subject);
$recpients =& new Swift_RecipientList();
$sender =& new Swift_Address('info#example.com', 'example.com');
$recpients->addTo('info#example.com', 'www.example.com');
$message->attach(new Swift_Message_Part('this is my body'));
$message->attach(new Swift_Message_Attachment($binarycontents, $fileTitle, $mimeType));
$swift->send($message, $recpients, $sender);
in your case the attaching would be:
$message->attach(new Swift_Message_Attachment(file_get_contents($file), $order.'order.csv', 'application/vnd.ms-excel'));
just for example ofcourse :)
//to set headers of csv file(first row)
$content = "user_id,first_name,last_name,phone,gender,pan_number\r\n";
//this can be dynamic data from database or from anywhere can loop this for multiple rows
$content .= "1,test_fn,test_ln,8888999900,M,ASDD3333\r\n";
//include swiftmailer library in order to run the below code
Yii::$app->mailer->compose()
->setFrom(array('test#test.com'=>'test'))
->setTo('testto#testto.com')
->setSubject('your subject' )
->setHtmlBody('<table><tr><td>your email body message here</td></tr> </table>')
->attachContent($content, ['fileName' => 'user.csv', 'contentType' => 'application/vnd.ms-excel'])->send();