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.
Related
I am sending calendar invite using Laravel's mail api.
The calendar looks good on gmail but shows an attachment on outlook instead of proper calendar invitation.
Gmail's output:
while on outlook it seems to be an attachment:
I am creating a file with name invite.ics and I put the content inside the invite.ics file, I attach the file while sending the email.
$to = $row->to;
$subject = $row->subject;
$attachments = $row->attachment;
$cc = $row->cc;
$body = $row->body;
$calendar_invitation = $row->calendar_invitation;
\Mail::send(
'emailTemplates.dummy',
['emailBody'=>$row->body],
function(Message $message) use ($to,$subject,$attachments,$cc, $body, $calendar_invitation, $companyEmail)
{
$message->from($companyEmail, '');
$message->replyTo($companyEmail, 'Email Agent Evmeetings');
$message->to($to, '')->subject($subject);
$file = fopen("invite.ics","w");
echo fwrite($file,$calendar_invitation);
fclose($file);
$message->attach('invite.ics', array('mime' => "text/calendar"));
});
That's how I made it work
$message->from($companyEmail, '');
$message->replyTo($companyEmail, 'Email Agent Evmeetings');
$message->to($to, '')->subject($subject);
$message->setBody($calendar_invitation, 'text/calendar; charset="utf-8"; method=REQUEST');
$message->addPart($body, "text/html");
Added the calendar in body and changed the mime type to 'text/calendar; charset="utf-8"; method=REQUEST'
and used addPart($body, "text/html"); method to add html body in the email.
Full code:
\Mail::send('emailTemplates.dummy', ['emailBody'=>$row->body], function(Message $message) use ($to,$subject,$attachments,$cc, $body, $calendar_invitation, $companyEmail,$replyTo)
{
$message->from($companyEmail, trim(env("email_agent_name")));
$message->replyTo($replyTo, trim(env("email_agent_email")));
$message->to($to, '')->subject($subject);
$message->setBody($calendar_invitation, 'text/calendar; charset="utf-8"; method=REQUEST');
$message->addPart($body, "text/html");
$attachments = unserialize($attachments);
foreach($attachments as $attachment){
if(file_exists(public_path()."/".$attachment['location'])){
$message->attach(public_path()."/".$attachment['location'], array('as'=>$attachment['name'].".".pathinfo(parse_url($attachment['location'])['path'], PATHINFO_EXTENSION),
'mime' => mime_content_type ( public_path()."/".$attachment['location']) ));
}
}
$cc = unserialize($cc);
foreach($cc as $anotherEmail){
$message->cc($anotherEmail);
}
});
i just ran into a problem, when sending mails with attachments larger than about 2.5Mb from a server. Sending emails with smaller attachments work, but as soon the critical size of about 2 or 2.5Mb is reached, the mail is not send anymore.
The PDF files and merged target PDF are created without problem, no matter of the size. But only smaller PDF files are send by mail. Not even an empty mail is send, when the attachments are too large.
The process is a follows:
1) The php script creates several PDF files.
2) Those files are merged through gs
$finCmd = 'gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile='.$pathDest.$pdfFilename.' input1.pdf input2.pdf input3.pdf';
// Create PDF
$execResult = exec($finCmd);
3) The email body is created
protected function setBodyHtmlpart($content, $pdfFilepath = null, $pdfFilename = null) {
$content="<p><span style='font-size:10.0pt;font-family:\"Arial\",\"sans-serif\";color:black;'>".$content.'</span></p>';
$html = new MimePart($content.$this->getSignature());
$html->type = "text/html";
$body = new MimeMessage();
if ($pdfFilename != '') {
$pdfAttach = new MimePart(file_get_contents($pdfFilepath.$pdfFilename));
$pdfAttach->type = 'application/pdf';
$pdfAttach->filename = $pdfFilename;
$pdfAttach->encoding = \Zend\Mime\Mime::ENCODING_BASE64;
$pdfAttach->disposition = \Zend\Mime\Mime::DISPOSITION_ATTACHMENT;
$body->setParts(array($html, $pdfAttach));
} else {
$body->setParts(array($html));
}
return $body;
}
4) The email is send with:
protected function send($fromAddress, $fromName, $toAddress, $toName, $subject, $bodyParts)
{
// setup SMTP options
$options = new SmtpOptions(array(
'name' => 'XServer',
'host' => 'xServer',
'port' => 25,
'connection_class' => 'plain',
'connection_config' => array(
'username' => 'Xusername',
'password' => 'Xpassword',
),
));
$mail = new Message();
$mail->setBody($bodyParts);
$mail->setFrom($fromAddress, $fromName);
$mail->setTo($toAddress, $toName);
$mail->setSubject($subject);
$transport = new SmtpTransport($options);
$transport->send($mail);
}
Any hints are welcome, as i am totaly lost.
I thought there could be race problem: exec is not finished, but script already tries to send the mail and cancels. But i than would at least receive an empty email.
Edit:
Changing then Mime\Mime::ENCODING_BASE64 delivers the mails, but PDF files are corrupted.
Have you tried using type Octetstream
$pdfAttach->type = Mime::TYPE_OCTETSTREAM;
$pdfAttach->encoding = Mime::ENCODING_BASE64;
It seems like, that the issue is the mime encoding.
All options:
Zend_Mime::ENCODING_7BIT: '7bit' --> corrupted file
Zend_Mime::ENCODING_8BIT: '8bit'; --> corrupted file
Zend_Mime::ENCODING_QUOTEDPRINTABLE: 'quoted-printable' --> corrupted
file
Zend_Mime::ENCODING_BASE64: 'base64' --> file not send
did not work.
Developed a solution with PHPMailer.
Worked out.
Not sure what's the syntax of having html files rendered when added to Zend\Mail\Message?
Here's a piece of code I have:
$mail = new Mail\Message();
$html = file_get_contents('content.html');
$mail->setBody($html);
Is it sufficient to set it up like this or do I need to specify the type of content somehow?
Thanks.
You can attach it as a Mime part.
Example from the docs:
use Zend\Mail\Message;
use Zend\Mime\Message as MimeMessage;
use Zend\Mime\Part as MimePart;
$html = new MimePart($htmlMarkup);
$html->type = "text/html";
$body = new MimeMessage();
$body->setParts(array($text, $html, $image));
$message = new Message();
$message->setBody($body);
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'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();