Create Dynamic PDF in MPDF & Send as Attachment with Email in PHP using PHPMailer

  • Post author:
  • Post category:PHP
  • Post comments:0 Comments

Welcome folks I am back with another blog post. In this post we will be extending our previous post in which we have made a Dynamic PDF from HTML Form using MPDF Library. So I will suggest please read that before reading this one. In this post we will be extending the application by adding a feature which will allow users to actually send the generated PDF file as email by using PHPMailer in PHP. For mailing purpose we will be using a online service called This service allows us to test our application by sending test emails before you move on to production. Let’s get started by building this app from scratch.

First of all we will be installing the library PHPMailer from composer. Composer is a free program by which we can install dependencies by issuing some simple commands.

First of all install composer from here. Then after installing composer just type the below command to install the PHPMailer dependency in your project.


After the installation is complete you will see this message as shown below



Now after installing the library just move on to the project which we have created earlier in the previous post. Just open the index.php file and copy paste the following code if you don’t have the code or if you are reading this for the first time.


We have made this in the previous post. It will generate the PDF from the HTML Form as the user can enter details in it. So now we will make a separate function to send the generated email to any email address. For this we will remove the download function as we don’t want to generate the pdf we just want to store it in a variable. And then we can use that variable and send that to our custom function which will actually send it as a attachment with the email.

But before that first of all create a account on Create a free account and then it will give you free information such as smtp username and password that information you need to use in the PHP Script later on.





After creating the account on mailtrap. You need to go to the index.php file and include the PHPMailer Library into your php script. Just paste the 2 lines of code given below.



After including the library in the php script. Just delete this line from the php script which tells to invoke the download once the pdf file is ready.



Now instead we will store the PDF File in a variable and send it through attachment. Now after deleting this line just write this line in place of this line as shown below

Now after that we will store all the information such as firstname, lastname, email and message in a separate variable called as data so that we can also send this information as a label in the email. Just declare an array something like this as shown below.


Now after that we will create the custom function which will actually send the email with pdf attachment. Just create a new function something like this as shown below.



Now it is expecting two variable namely the pdf data to be attached as email and also the details of the user which has been stored in an array called as data. Now copy paste all the code of the PHPMailer library this is the boilerplate code of the library so that we just need to make some tweaks to this code to make our app.



Now in this code you need to replace our smtp values such as username, password ,host etc. I will show mine to you of account. Yours may be different. Just include it as accordingly. In order to get the information in mailtrap steps are given below.



After including the following details you will get a similar sendEmail function as shown below. Just check your also.



Now we just have to call the function to send the email so just call the function like below.



As you press the Send Button in the Application you will see a bunch of text spitted out at last it will say message has been sent something like this as shown below.



Now if we check the mailtrap inbox you will find a email in it like this as shown below.





Now we have successfully sent the email in the mailtrap inbox but one thing is missing i.e. the pdf attachement now we have to pass the email as well as the PDF as an attachment for this we will add a single of code in the script or in the custom function . Just add this line of code in order to include the pdf attachment.



Now if we send it we will see a attachment along with the email something like this as shown below.




This completes our tutorial on how to send email along with PDF Attachments using PHP Mailer. Thanks for reading this post.

Leave a Reply