Sendig email with an attachment in Laravel 7 using mail() and fpdf - php

I am trying to send email with an attachment via mail function and FPDF library.
But when I send it, the email comes just with some strings and encoded characters.
Here is my code bellow:
Code to send email
$message = "<p>Please see the attachment.</p>";
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "test.pdf";
// encode data (puts attachment in proper format)
$pdfGenerate = new RelatorioManutencaoController();
$pdfdoc = $pdfGenerate->imprimir(base64_encode($manutencao->id));
// return $pdfdoc;
$attachment = chunk_split(base64_encode($pdfdoc));
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream;
name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
$sender = "diakuzena#gmail.com";
$emissor = "diakuzena#gmail.com";
$assunto = "Assunto do email";
$header = 'MIME-version:1.0' . "\r\n";
$header .= 'Content-type:text/html; charset=iso-8859-1' . "\r\n";
$header .= 'From:' . $sender . '<' . $emissor . '>';
mail($sender, $assunto, $arquivo, $header);
mail("diakuzena#gmail.com", $assunto, $body, $header);
Someby please help me solving this problem.

Try using the command: php artisan make:mail this generates a specific controller for sending mail, in the resources add the mail template and in the controller generated by the command in the build function include the following code :
public function build()
{
// attach method with a file in your system
return $this->view('emails.your mail template')
->attach('/path/to/file');
}
docs of attachments!
or for inline attach in the html template add this:
<body>
Here is an image:
<img src="{{ $message->embed($pathToImage) }}">
</body>
docs of inline-attachments!
to send it, just call this mail handler:
Mail::to($request->user())->send(new YourGenerateMailController());
For more information check the documentation
https://laravel.com/docs/7.x/mail

Related

how to attach mail in my output file using fpdf

$path=$_SERVER['DOCUMENT_ROOT']."/unwant/test3.pdf";
$filename="test3.pdf";
$pdf->Output($filename,'F');
i got my output file in this code $pdf->Output($filename,'F'); i want to attach my output file in mail.
require('lib/fpdf/fpdf.php');
$pdf = new FPDF('P', 'pt', array(500,233));
$pdf->AddFont('Georgiai','','georgiai.php');
$pdf->AddPage();
$pdf->Image('lib/fpdf/image.jpg',0,0,500);
$pdf->SetFont('georgiai','',16);
$pdf->Cell(40,10,'Hello World!');
// email stuff (change data below)
$to = "myemail#example.com";
$from = "me#example.com";
$subject = "send email with pdf attachment";
$message = "<p>Please see the attachment.</p>";
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "test.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// send message
mail($to, $subject, $body, $headers);
To use PHPMailer:
Download the PHPMailer script from here:
http://github.com/PHPMailer/PHPMailer
Extract the archive and copy the script's folder to a convenient
place in your project.
Include the main script file --
require_once('path/to/file/class.phpmailer.php');
Now, sending emails with attachments goes from being insanely difficult to incredibly easy:
$attachment= $pdf->Output('attachment.pdf', 'S');
$mailer->AddStringAttachment($attachment, 'attachment.pdf');

Multiple FPDF's via mail in PHP

How can I dynamically send multiple FPDF's via mail in PHP ?
I am a new user. The scripts work fine individually. mail.php also works when I attach one pdf. But I need to attach 2 of them to the same mail. Could someone please help me find a way around? Thank you.
I have atttached one of the created pdf's. There's another one with the same code snippets. I have also attached the script I used to send the email.
createpdf.php
<?php
$email_id = $_GET['e'];
$connect=mysqli_connect("localhost", "user",
"password","db") or die("Connection failed1:".mysqli_connect_error());
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
$email_id = $email_id;
$query = $connect->query("SELECT first_name FROM `db` WHERE email_id = '$email_id'");
$array = Array();
while($result = $query->fetch_assoc()){
$pdf->SetFont('Arial','B',12);
$pdf->Cell(40,10,'Dear ' .$result['first_name']. ', This is the 1st doc.');
}
$pdf->Output();
?>
mail.php
<?php
require('createpdf.php');
$to = $_GET['e'];
$subject = "test";
$message = "<p>Please see the attachment.</p>";
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "test.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: X < noreply#test.com >\n";
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed;
boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream;
name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// send message
mail($to, $subject, $body, $headers);
?>

Sending attachment in email function but not able to get it

I am using this function for attaching pdf in my mail. It's working fine but when the user tries to open the pdf file he gets a message telling him that file can't be opened, because of a problem with file formate.
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = "\r\n";
// attachment name
$filename = $invoice.'.pdf';
// encode data (puts attachment in proper format)
$attachment = chunk_split(base64_encode($filename));
//$attachment = $filename;
// main header
$from = "test#productionserver.in";
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol;
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
mail($to,$subject,$body,$headers);
Does anyone have an idea what I made wrong ?
$attachment = chunk_split(base64_encode($filename));
You are encoding the filename as your attachment. You need to encode the actual file and not just its name.
$attachment = chunk_split(base64_encode(file_get_contents($filename)));

PHP mail PDF attachment gets file corrupted

I'm having a big headache with this issue and I wonder if any1 could help me with this. In my tests and BCC I always see the PDF attachment correctly, but maybe 10% of the people see the PDF file as being corrupted (some people I know that they are using Outlook and I'm using Mail from Mac).
function mail_attachment($content, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "Invitation.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $content;
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: Myself <".$from_mail.">\nBCC: me#hotmail.com".$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed;{$eol}\tboundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"utf-8\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message;
$body .= $eol.$eol;
// message
$body .= "Content-Type: text/plain; charset=\"utf-8\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;*/
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator.$eol;
// send message
$em = mail($mailto, $subject, $body, $headers);
return $em;}
What could possibly be happening? I always see it working but few people can't open the file..
It's been a while, but finally got this problem solved. The issue is on PHP_EOL which in my case is returning \n, while some systems the email should have \r\n as line break.
To fix this issue just place the new $eol:
$eol = "\r\n";
The way you have set the headers seems right to me. However, couple things I noticed/do differently:
$headers .= "Content-Type: multipart/mixed;{$eol}\tboundary=\"".$separator."\"".$eol;
Take this */ away from the end
$body .= $message.$eol;*/
And for the content disposition:
"Content-Disposition: attachment; filename=\"" . $filename . "\"".$eol;
Also, body and the attachment headers should be combined to the headers, no need to send body separately in mail():
return mail($mailto, $subject, "", $headers);

send email with pdf attachment

I'm trying to send an email using the mail() function in php with a pdf attachment.
I'm running the script on localmachine. I set up the smtp ip in php.ini.
I can send a text email perfectly but with an attachment I get the following error:
Warning: mail() [function.mail]: SMTP server response: 503 Unexpected command or sequence of commands in C:\AppServ\www\PhpProject1\CV-Generator\testemail2.php on line 55
Can anyone tell me what's wrong please?
Here is my code:
<?php
// download fpdf class (http://fpdf.org)
require('./pdf/fpdf.php');
// fpdf object
$pdf = new FPDF();
// generate a simple PDF (for more info, see http://fpdf.org/en/tutorial/)
$pdf->AddPage();
$pdf->SetFont("Arial","B",14);
$pdf->Cell(40,10, "this is a pdf example");
// email stuff (change data below)
$to = $_GET['send'];
$from = "info#asaltechd.com";
$subject = "send email with pdf attachment";
$message = "<p>Please see the attachment.</p>";
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "example.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// main header (multipart mandatory)
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;
// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
// send message
mail($to, $subject, "", $headers);
?>
The attachment doesn't go in the headers! They should only declare the MIME headers:
// main header (multipart mandatory)
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol; // see below
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
// message
$msg = "--".$separator.$eol;
$msg .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$msg .= $message.$eol.$eol;
// attachment
$msg .= "--".$separator.$eol;
$msg .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= "Content-Disposition: attachment".$eol;
$msg .= $attachment.$eol;
$msg .= "--".$separator."--".$eol;
// send message
mail($to, $subject, $msg, $headers);
Note also that you should NEVER have 2 consecutive line terminations within the headers - SMTP uses a blank line as the seperator between headers and the body.
Also, the EOL should NOT be the default on your operating system - it should be the EOL sequence as defined by SMTP - i.e. CR+LF
I use PHP's SwiftMailer (http://swiftmailer.org/):
require_once('../lib/swiftMailer/lib/swift_required.php');
...
$body="Dear $fname,\n\nPlease find attached, an invoice for the period $startDate - $endDate\n\nBest regards,\n\nMr X";
$message = Swift_Message::newInstance('Subject goes here')
->setFrom(array($email => "no-reply#mydomain.com"))
->setTo(array($email => "$fname $lname"))
->setBody($body);
$message->attach(Swift_Attachment::fromPath("../../invoices_unpaid/$id.pdf"));
$result = $mailer->send($message);
I would suggest that you use PHP Mailer to send emails from your PHP. I've used it with great success on many different configurations. The class has all necessary methods for handling encodings, attachments, custome headers, sending via sendmail, etc., etc.

Categories