Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
How To Send Email With Attachment In PHP

How to Send Email with Attachment in PHP

Posted on July 27, 2018December 17, 2022 By XpertPhp

In this article, we will explain to you how to send email with attachment in PHP. we can also use the PHP mail function to send emails with attachments, but here we are using the phpmailer function. if you want to install phpmailer then you can install it via the composer or you can also download via Github.

we will give a simple example of how to send Email with Attachment in PHP. so you can see the below example.

Read Also:
How to Send mail with Multiple Attachment in PHP

sometimes we use send email functionality such as sending invoices to send newsletters and contact forms etc.

In this example, we will create the form using the bootstrap. we have to pass the form attribute “multipart/form-data“ for the file upload. so we will take some fields to create the form and pass the form attribute “multipart/form-data“.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
require 'PHPMailer/PHPMailerAutoload.php';
try {
if(isset($_POST['btnSend'])) {
$mail = new PHPMailer;
if(empty($_POST["name"])){
print 'Name is too short or empty!';
exit;
}
if(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)){ //email validation
print 'Please enter a valid email!';
exit;
}
if(empty($_POST["subject"])){ //check emtpy subject
print 'Subject is required';
exit;
}
if(empty($_POST["message"])){ //check emtpy message
print 'Too short message! Please enter something.';
exit;
}
$sender_name    = filter_var($_POST["name"], FILTER_SANITIZE_STRING); //capture sender name
$sender_email   = filter_var($_POST["email"], FILTER_SANITIZE_STRING); //capture sender email
$subject        = filter_var($_POST["subject"], FILTER_SANITIZE_STRING);
$message        = filter_var($_POST["message"], FILTER_SANITIZE_STRING); //capture message
$mail->FromName   = $sender_name;
$mail->AddAddress($sender_email);
$mail->From     = "admin@gmail.com";
$mail->Subject  = $subject;
$message_body =  "Message from $sender_name\n";
$message_body .=  "------------------------------\n";
$message_body .=  "$message\n";
$message_body .=  "------------------------------\n";
$message_body .=  "$sender_name\n";
$message_body .=  "$sender_email\n";
$body = preg_replace('/\\\\/','', $message_body);
$mail->MsgHTML($body);
$mail->IsSendmail();
$mail->AddReplyTo("admin@gmail.com");
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";
$mail->WordWrap   = 80;
$mail->AddAttachment($_FILES['attachment']['tmp_name'], $_FILES['attachment']['name']);
$mail->IsHTML(true);
$mail->Send();
echo 'The message has been sent.';
}
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <title>How to Send Email with Attachment in PHP - XpertPhp</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
  <form method="post" enctype='multipart/form-data'>
   <div class="form-group">
      <label for="name">Name:</label>
      <input type="text" class="form-control" id="name" placeholder="Enter Name" name="name">
    </div>
    <div class="form-group">
      <label for="email">Email:</label>
      <input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
    </div>
    <div class="form-group">
      <label for="subject">Subject:</label>
      <input type="text" class="form-control" id="subject" placeholder="Enter Subject" name="subject">
    </div>
    <div class="form-group">
      <label for="message">Message :</label>
      <textarea class="form-control" id="message" name="message"></textarea>
    </div>
    <div class="form-group">
      <label for="attachment">Attachment :</label>
      <input type="file" class="form-control" id="attachment" name="attachment" />
    </div>
    <button type="submit" name="btnSend" class="btn btn-default">Send</button>
  </form>
</div>
</body>
</html>
See also  Laravel 7 Send Email Example Tutorial

Php Tags:php send email, php send email form, send mail

Post navigation

Previous Post: How to send an email with HTML template using PHP and Ajax
Next Post: How to Send mail with Multiple Attachment in PHP

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Angular 15 CRUD Application Example Tutorial
  • Laravel 10 Form Validation Example Tutorial
  • Angular 15 Custom Form Validation Example
  • Laravel 10 Send Email Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com