PHP Scripts – WordPress Tutorials

I am happy to write about PHPMailer class, let’s learn that how to send and email using PHPMailer. In this tutorial we will talk about how to use HTML Templates as an email, attatch files with your email using PHPMailer. Here we will use the following files to as sample for newbies.

  • index.php
  • send_email.php
  • contents.html
  • images/logo1.png
  • images/engr_mudasir.jpg

Our index file contains a form that will ask for an email address and on submit it will send an email to your email address you entered. In any other constraint you could say that it is front end page.

Demo                                                                  Download Code

index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<html>
<head>
<title>Send and Email Using PHPMaier</title>
</head>
<body>
<h2>Send an Email Using PHPMailer Demo</h2>
<form enctype="multipart/form-data" method="post" action="send_mail.php">
<table width="461" height="138" border="1">
<tr>
<td width="141">Enter Your Email Here</td>
<td width="187">
<input name="email" id="email" type="text" />
<input type="submit" name="btnsend" id="btnsend" value="Send Email" /></td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td><a href="">Main Tutorial Here</a></td>
<td><a href="http://www.engrmudasir.computersneaker.com">By Engr Mudasir</a></td>
</tr>
</table>
</form>
</body>
</html>

Our second file contains contents to send to the user as email body, this contains an image too.

contents.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<body style="margin: 10px;">
<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;">
<div align="center"><img src="images/logo1.png" style="height: 90px; width: 340p"></div><br>
<br>
&nbsp;This is a Demo of PHPMailer.<br>
<br>
This particular example uses <strong>HTML</strong>, with a &lt;div&gt; tag and inline<br>
styles.<br>
<br>
Also note the use of the PHPMailer logo above with no specific code to handle
including it.<br />
Included are two attachments:<br />
logo1.png is an attachment and used inline as a graphic (above)<br />
engr_mudasir.jpg is an attachment<br />
<br />
</div>
</body>

Third file is action for the above form, this action file will send an email to the email address entered in the form.

Include the PHPMailer class and create an object for the PHPMailer class. Object will call the functions from the PHPMailer class. Defined are the characteristics to be used and keep in focus to send email.

send_email.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
require_once('class.phpmailer.php');
if(isset($_POST['btnsend']))
{
$mail = new PHPMailer(); // defaults to using php "mail()"

$body = file_get_contents('contents.html');

$mail->SetFrom('engrmudasirmalik@gmail.com', 'ComputerSneaker.com');

$address = $_POST['email'];
$mail->AddAddress($address, "Guest");

$mail->Subject = "PHPMailer Demo Test From ComputerSneaker";

$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional

$mail->MsgHTML($body);

$mail->AddAttachment("images/logo1.png"); // attachment
$mail->AddAttachment("images/engr mudasir.jpg"); // attachment

if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "A test email sent to your email address '".$_POST['email']."' Please Check Email and Spam too.";
echo '&lt;meta http-equiv="refresh" content="5;url=http://www.computersneaker.com/send-email-using-phpmailer/"&gt;';
}
}

Demo                                                                  Download Code

And here comes the email as Email Body :

send email using phpmailer

I hope you enjoyed this tutorial, don’t forget to provide feedback and subscribe for our blog.

Random Posts


Categories: PHP, Web Development
  • ram

    hi it works fine to me…I attache password to send_mail.php..It works good…..I need to send parameters to contents.html as username , password who are register to my website. How to pass parameters to contents.html from send-mail.php page or other php pages…I used this phpmailer in my registration.php for user registration confirmation. I need to send username and passwords to the user mail through contents.html.

    • engrmudasirmalik

      Simple is that, simply first pass your username and password to your email body contents ans then send that email.
      as
      And then send the message.

      • sunil

        Hello I have the following error…

        Mailer Error: Could not instantiate mail function.

        How to resolve this problem? Please suggest.

        • engrmudasirmalik

          You need to send me your code, i will check and then will let you know about the error.
          Because there are three possibilities according to me. May be your SMTP is not working.
          Second is that you the email address to send is not in correct format.
          Third possibility is that From email is not correct.
          So let me know about the code and i will let you know about the error.

  • jimbavia

    Its giving me an error saying “Could not instantiate mail class”

    • http://www.computersneaker.com/ Mudasir Nazir

      you may have not download mail class properly, Send me the error!

  • guest

    I want to make a contact form which will send me messages from users ,, in that case how to send messages from their mails to my mail .. can u reply a small code example ??? and can i skip contact.html and directly assign watever i want to send in $body variable like this
    $body =”” .$_POST['message']. “”; will it go in html format ?? plzz HELP !!!!

    • http://www.computersneaker.com/ Mudasir Nazir

      yes for sure you can, but you have to assign some values to each variable in that case.

  • NITISH JAIN

    Hi thanks for this code.i have 1 problem. i have downloaded your code. now i want to set up the host , port, username and password. i am new in this. Would you please help me to configure those things please.?my email id is parakhnitish@gmail.com. Awaiting for your response.

  • govind

    hey Mudasir Malik…share your email adress..i having same problem as listed below,Mailer Error: Could not instantiate mail function.

  • Shoola Adewale

    Hello Mudasir Malik, I cannot pass variable to to the mail. Please how do I achieve this please.

    Thanks in advance..

    • Shoola Adewale

      No response yet.. Please can anyone help with the problem above..

      • http://www.computersneaker.com/ Mudasir Malik

        can you please send me the code where you are not able to pass your variable ?

  • Glen

    Hello. Thank you very much for posting this. It works great. Would it be possible to allow more than one email address that is separated by commas in your “Enter your email here” input box? Any help would be greatly appreciated.
    Thanks.

  • Peeyush

    hiii i want to use this code on browse button .. which changess are requare..

  • Preeti

    hi i have downloaded your code and used in my website but it is not working. I have changed sender email id with my Id but still doesnt work. Please help me out. Its urgent

  • Preeti

    weather i need to make changes in class.phpmailer.php ?