Tuesday, November 18, 2014

Learn How to Send an Email by Using PHPmailer

By using class.phpmailer.php file, we can send mail operation very easily. You need to follow simple steps to do run this query. Check out the live demo to get a detail idea.

Send an Email by Using PHPmailer

                                         DOWNLOAD                                 LIVE DEMO

Here, we will see that how the function is working and how the email will be sent. To complete this program in PHP, we need some files like,
  • index.php
  • send-mail.php
  • main.php
  • class.phpmailer.php
all above files are very important. 

Index File – We need to make a form to provide input to make an operation.
Send-mail.php File – We have to make the subject, address, body of the content, etc.
Main.php File – It is required to attach class.phpmailer.php which is a php class file.
We have to attach that in send-mail.php page.

INDEX.PHP 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Send an attached mail using php</title>
<link type="text/css" rel="stylesheet" href="css/style.css" media="all" >
</head>
<body>
<div class="box">
<h1 align="center">Send mail using PHPMailer</h1><br>
<form enctype="multipart/form-data" method="post" action="send-mail.php">
<input type="email" name="emailid" class="cmail" autocomplete="off" />
<input type="submit" name="submit" class="csubmit" value="Send Mail" />
</form>
</div>
</body>
</html>

STYLE.CSS

@charset "utf-8";
/* CSS Document */
* {
    margin:0 auto;
    padding:0px;
}
h1 {
    font-family:Tahoma, Geneva, sans-serif;
    font-size:24px;
    text-transform:uppercase;
    color:#036;
}
.box {
    width:500px;
    min-height:100px;
    padding:15px;
    border:1px red dashed;
    text-align:center;
    margin-top:50px;
}
.cmail {
    width:350px;
    height:35px;
    border:#033 solid 2px;
    padding:5px;
    font-family:Tahoma, Geneva, sans-serif;
    font-size:14px;
    font-weight:bold;
    color:#090;
}
.csubmit {
    width:100px;
    height:35px;
    background-color:#003;
    padding:6px;
    border:#033 solid 2px;
    color:#FFF;
    text-transform:uppercase;
}

SEND-MAIL.PHP

<?php
  require_once('class.phpmailer.php');
  if(isset($_POST['submit']))
  {
    $mailid=new PHPMailer();
    $body=file_get_contents('main.php'); //content
    $mailid->SetFrom('arunkumarmaha@outlook.com', 'www.2my4edge.com');
    $mailadd=$_POST['emailid'];
    $mailid->AddAddress($mailadd, "Guest");
    $mailid->Subject= "Arunkumar maha for 2my4edge.com sent you a Mail";
    $mailid->AltBody= "View the mail";
    $mailid->MsgHTML($body);
    $mailid->AddAttachment("img/arunkumarmaha.jpg");//attachment
    if(!$mailid->Send()) {
      echo "Mailer Error: ".$mailid->ErrorInfo;
    } else {
     echo "<div style='width:500px; margin:0 auto; min-height:100px;
     padding:10px; border:#006 solid 2px; text-align:center;'>
      <span style='font-family:Tahoma, Geneva, sans-serif;
      font-size:18px; text-transform:uppercase; color:#006;'>
      Your mail is sent to</span> '
      <span style='font-family:Tahoma, Geneva, sans-serif;
      font-size:16px; text-transform:lowercase;
      color:#F00;'>".$_POST['emailid']."</span> '
      <span style='font-family:Tahoma, Geneva, sans-serif;
      font-size:18px; text-transform:uppercase; color:#006;'>
      Check Email in Spam too</span></div>";
      echo '<meta http-equiv="refresh" content="10;url=http://www.2my4edge.com">';
    }
}
?>

MAIN.PHP

<div class="mail-box" align="center">
<a href="http://www.2my4edge.com"><img alt="Logo" class="image" src="img/my-logo.png" ></a>
<h1 align="center"> Simple Demo for send mail concept using PHP </h1>
<h2 align="center"> Check the attachments too </h2>
</div>

In a main.php will be a body content of the mail. That's too attached in send-mail.php file.  

0 comments:

Post a Comment