How to Send Email with Attachment in PHP
In this article, we will explain to you how to send mail with attachment in PHP. we can also use the PHP mail function to send email with attachment, 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.
sometimes we use to send email functionality such as send invoices to send newsletter and contact form 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->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->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> |
Read Also
How to Send mail with Multiple Attachment in PHP
How To Send An Email With HTML Template Using PHP And Ajax