Send an Email by using PHPmailer | 2my4edge

17 February 2022

Send an Email by using PHPmailer

How to send a Email using phpmailer, It is a simple one. here we have class.phpmailer.php file, that make send mail operation very simple and calm. here we just have to do some simple steps to do this method. Check this by the live demo.

send email using php mailer
DOWNLOAD          LIVE DEMO

This phpmailer class file makes to attach a file with a mail. here we are going to see about how the function is running and how the mail will be sent. for the send mail concept we need some files there are.
  1. index.php
  2. send-mail.php
  3. main.php
  4. class.phpmailer.php
those above four file are important in this program. in a index file we have to make a form to give a input to make an operation. in a send-mail.php file we have to make the subject, address, body of the content, etc. and the main.php file is to attach. class.phpmailer.php is a php class file. we must 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. I hope this is really helpful to you. stay connected with all social media for more updates.

16 comments:

  1. Hi arun Nice post i am not attached anything but its display your image.

    ReplyDelete
  2. Hi.mail sent message is displaying but mail is not sent

    ReplyDelete
  3. mail sent message is displaying but mail is not sent

    ReplyDelete
  4. mail is not sent.

    ReplyDelete
  5. arun it's showing 1Mailer Error: Could not instantiate mail function.

    ReplyDelete
  6. Yes, it will in localhost, upload to server and check it.

    @Gomathi

    ReplyDelete
  7. thank so much sir...

    ReplyDelete
    Replies
    1. how to solved this error --->Could not instantiate mail function

      Delete
  8. Could not instantiate mail function. error in server plz help

    ReplyDelete
  9. mail not send

    ReplyDelete
  10. "Database not connected" when Live Demo is clicked

    ReplyDelete
  11. how to solved this error --->Could not instantiate mail function... Code already on server.

    ReplyDelete
  12. Could not instantiate mail function... Code already on server. How to solve this problem!

    ReplyDelete
  13. can it send mails to inbox?

    ReplyDelete
  14. very interesting post.this is my first time visit here.i found so mmany interesting stuff in your blog especially its discussion..thanks for the post! Verifications IO

    ReplyDelete

^