PHP Email- Create PDF And Send With Attachment - codex

Latest

Coding help in website development.

BANNER 728X90

Sunday 30 April 2017

PHP Email- Create PDF And Send With Attachment

Description:

With The help of this code you can submit your form and generate pdf and send via gmail     as attachment.

First Step:

Add form in your page-

<form class="form-horizontal" action="" method="post">

                    <div class="form-group">
                        <label class="control-label col-sm-2" for="mailto">Mail To:</label>
                        <div class="col-sm-10">
                            <input type="email" class="form-control" id="mailto" placeholder="Enter mailto"                                  name="mailto" value="<?php echo $_POST['mailto']; ?>">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label col-sm-2" for="mailfrom">Mail From:</label>
                        <div class="col-sm-10">
                            <input type="email" class="form-control" id="mailfrom" placeholder="Enter                                         mailfrom" name="mailfrom" value="<?php echo $_POST['mailfrom']; ?>">
                        </div>
                    </div>


                    <div class="form-group">
                        <label class="control-label col-sm-2" for="">Mail Subject:</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="mailsubject" placeholder="Enter                                        mailsubject" name="mailsubject" value="<?php echo $_POST['mailsubject']; ?>">
                        </div>
                    </div>


                    <div class="form-group">
                        <label class="control-label col-sm-2" for="firstname">Firstname:</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="firstname" placeholder="Enter                                           firstname" name="firstname" value="<?php echo $_POST['firstname']; ?>">
                        </div>
                    </div>



                    <div class="form-group">
                        <label class="control-label col-sm-2" for="lastname">Lastname:</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="lastname" placeholder="Enter                                              lastname" name="lastname" value="<?php echo $_POST['lastname']; ?>">
                        </div>
                    </div> 


                    <div class="form-group">
                        <label class="control-label col-sm-2" for="description">Description:</label>
                        <div class="col-sm-10">
                            <textarea class="form-control" id="description"  name="description" style="                                          height: 268px;"><?php echo $_POST['description']; ?></textarea>
                        </div>
                    </div> 



                    <div class="form-group">        
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" class="btn btn-success">Submit</button>
                        </div>
                    </div>
                </form>



Second Step:


Download And Include "html2pdf" Library File in Your Action File:



require_once('html2pdf/html2pdf.class.php');


Third Step:


Past php code in you action file-



        /**/
        $mailto = $_POST['mailto'];
        $mailfrom = $_POST['mailfrom'];
        $mailsubject = $_POST['mailsubject'];
        $firstname = $_POST['firstname'];
        $lastname = $_POST['lastname'];
        $description = $_POST['description'];


        $description = wordwrap($description, 100, "<br />");
        /* break description content every after 100 character. */


        $content = '';

        $content .= '
<style>
table {
border-collapse: collapse;
}

table{
width:800px;
margin:0 auto;
}

td{
border: 1px solid #e2e2e2;
padding: 10px; 
max-width:520px;
word-wrap: break-word;
}


</style>

';
        /* you css */



        $content .= '<table>';

        $content .= '<tr><td>Mail To</td> <td>' . $mailto . '</td> </tr>';
        $content .= '<tr><td>Mail From</td>   <td>' . $mailfrom . '</td> </tr>';
        $content .= '<tr><td>Mail Subject</td>   <td>' . $mailsubject . '</td> </tr>';
        $content .= '<tr><td>Firstname</td>   <td>' . $firstname . '</td> </tr>';
        $content .= '<tr><td>Lastname</td>   <td>' . $lastname . '</td> </tr>';
        $content .= '<tr><td>Description</td>   <td>' . $description . '</td> </tr>';

        $content .= '</table>';


        require_once('html2pdf/html2pdf.class.php');


        $to = $mailto;
        $from = $mailfrom;
        $subject = $mailsubject;

        $html2pdf = new HTML2PDF('P', 'A4', 'fr');

        $html2pdf->setDefaultFont('Arial');
        $html2pdf->writeHTML($content, isset($_GET['vuehtml']));

        $html2pdf = new HTML2PDF('P', 'A4', 'fr');
        $html2pdf->WriteHTML($content);


        $message = "<p>Please see the attachment.</p>";
        $separator = md5(time());
        $eol = PHP_EOL;
        $filename = "pdf-document.pdf";
        $pdfdoc = $html2pdf->Output('', 'S');
        $attachment = chunk_split(base64_encode($pdfdoc));




        $headers = "From: " . $from . $eol;
        $headers .= "MIME-Version: 1.0" . $eol;
        $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;

        $body = '';

        $body .= "Content-Transfer-Encoding: 7bit" . $eol;
        $body .= "This is a MIME encoded message." . $eol; //had one more .$eol


        $body .= "--" . $separator . $eol;
        $body .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $eol;
        $body .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
        $body .= $message . $eol;


        $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 . "--";

        if (mail($to, $subject, $body, $headers)) {

            $msgsuccess = 'Mail Send Successfully';
        } else {

            $msgerror = 'Main not send';
        }




IF ANY ERROR:
{
  • If mail is not sending from your server so create mail id on your server and use as send(from) email. for test you can use both same  mail id
  • If mail is sent but you not get any email so check you spam and create mail id on your server.
}

Screenshort









PLEASE COMMENT IF THIS HELP YOU. 

PHP Email Create PDF And Send With Attachment

13 comments:

  1. Sir,if we want to attach a file then what to edit from above code...

    ReplyDelete
    Replies
    1. sir its showing mail not send what can be the reason?

      Delete
    2. meybe you not included whole library in your server. please upload whole library

      Delete
  2. very well done :D, but what if i just want to send an image generated by html2canvas to this method?

    ReplyDelete
  3. Great works! i have a question, is it have (UTF-8) character support, the Arabic characters showing like these (?????????????????) characters, anyone help me in this mater?

    ReplyDelete
  4. sir mail is not going sir what will i do sir

    ReplyDelete
  5. i did not get any mail my to address sir what will have to do

    ReplyDelete
  6. can i convert webpage as pdf instead of body ?

    ReplyDelete
  7. Casino Roll - Casino Roll
    A progressive 이스포츠 jackpot is a type of bet that 넷마블 바카라 pays out at odds between 1-60%. A progressive jackpot will bet365 코리아 pay out as a percentage 토토가입머니 of the first bet 암호화폐란 amount. For a progressive jackpot of

    ReplyDelete