Send Email With Attachment From localhost Or Online Server Using PHP

By : PHPErrorCode July 12, 2017 3343 Views php

Most of the web projects have the email sending functionality. Before uploading to the live server email functionality need to be checked on the local server. But PHP mail() function will not work at the localhost. In this post, we’ll show how you can send email from localhost in PHP. Using this simple PHP code you can send email from any online server or localhost server, like WAMP , XAMPP or any others Live Server

I am assuming that you have a google email / gmail account, because this is the easiest way to send email via smtp. If you have accounts in hotmail, rediffmail or any other emails service it can be done too.

the folloving steps to configure phpmailer library to send email online and local server

Setp 1: Gmail Account Setup.

To using Gmail SMTP server, you should need to change account access for less secure apps. Don’t worry! Just follow the below steps

  1. Login to your google account.
  2. Go to the Less secure apps settings page –
  3. From Access for less secure apps section, select Turn on.
Step 2: download PHPMailer package

we have to download phpmailer package from this   link......  PHPMailer is used by creating an PHPMailer object and using its methods to send email as shown below in the code.

Step 3: Add the HTML form to your page

User fills in his name attachment or email id and the is sent to him/her. There are two files – index.php for user input and send_mail.php for sending email using PHPmailer.

<form action="send_email.php" method="post">
    <input name="name" type="text" required/><br>
    <label>Email Id</label>
    <input type="text" name="email" required/><br>
    <input name="submit" type="submit" id="submit" class="submit" value="Submit" />
Setp 4: Add the PHP mail() function code

In the code below, we use PHPMailer by creating a PHPMailer object and using its methods to define the contents of the mail and send the mail.



// Display all error
error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);
require_once "phpmailer/phpmailer.php";


$body  ='<html><body>';
$body .='<table>';
$body .='<tr><td>sender name : '.$sender_name.'</td></tr>';
$body .='<tr><td>sender email : '.$sender_email.'</td></tr>';
$body .='<tr><td><img src="'.$attechment_path.'"/></td></tr>';
$body .='<tr><td>My sample add body text</td></tr>';
$body .='</table>';
$body .='</body></html>';
$mail = new PHPMailer(true);

// enables SMTP debug information
$mail->SMTPDebug = 0;

// enable SMTP authentication
$mail->SMTPAuth = true;

// sets the prefix to the server
$mail->SMTPSecure = 'ssl';

// sets GMAIL as the SMTP server
$mail->Host = '';

// set the SMTP port for the GMAIL server
$mail->Port = 465;

// your gmail address
$mail->Username = '';

// your gmail password
$mail->Password = 'your_password';

// add a subject on send the email
$mail->Subject = ' Sample email - ';

// Sender email address and name
$mail->SetFrom($sender_email, $sender_name);

// reciever address, person you want to send

// add message body part

// set the attachment documment path

    $msg = "Mail send successfully";
catch (phpmailerException $e) 
    $msg = $e->getMessage();
catch (Exception $e) 
    $msg = $e->getMessage();
echo $msg;