Dear Reader, let us know that, “How to send mail in PHP with attachment using PHP Mailer“. So in this Tutorial, We Learn Step By Step Mail Sending Process using phpmailer in PHP.
Mail Sending functionality is very important for any Organisation to notify any user to send information Immediately.
In PHP mail() function allows us to send email directly from a script. The mail() function accepts the no. of parameters like
PHPMailer is one of the most popular open source PHP libraries to send emails. t has become a PHP developer’s favorite way of sending emails programmatically
i) Email
ii) Subject
iii) Message
iv) CC Email or BC Email
In the above Parameters Email, Subject and Message Parameters are Mandatory and Rest of Parameters are Optional.
Lets first we see a demo by click on bellow “Demo” Button
Step 1:- Now we need to create index.php file in our project like “htdocs/webpreprations/demo/how-to-send-mail-using-php-mailer-in-php/index.php” and paste bellow code
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 |
<?php include('../header.php'); if(isset($_REQUEST['send'])) { $to_email = $_REQUEST['email']; /*--------------------- for sending mail start ------------------*/ require 'PHPMailer/PHPMailerAutoload.php'; $mail = new PHPMailer; // SMTP configuration $mail->isSMTP(); $mail->Host = 'smtp.gmail.com'; $mail->SMTPAuth = true; $mail->SMTPDebug = 0; $mail->Username = 'test786webpreparations@gmail.com'; $mail->Password = 'Testwebpreparations123'; $mail->SMTPSecure = 'TLS'; $mail->Port = 587; $mail->setFrom('webpreparations786test@gmail.com', 'Web Preparations'); $mail->addReplyTo('webpreparations786test@gmail.com', 'Web Preparations'); // Add a recipient $mail->addAddress($to_email); $mail->addAddress('webpreparations@gmail.com'); // Add cc or bcc $mail->addCC('webpreparations786test@gmail.com'); $mail->addBCC('webpreparations786test@gmail.com'); // Email subject $mail->Subject = 'Mail Send By Web Preparations'; // Set email format to HTML $mail->isHTML(true); // Email body content $mailContent = "<h1>How to send mail using PHPMailer with Attachment</h1> <p>“The best preparation for tomorrow is doing your best today.” No one is born successful, success requires preparation .So prepare yourself online at very ease...</p>"; $mailContent .= "<a href='http://www.webpreparations.com'><button type='submit' name='send' class='btn btn-info' style='background-color:#449D44; color:#fff; font-weight:bold;height:50px; border:1px;'>Click Her for Visit Web Preparations</button></a>"; // for send an attatchment $path = "upload/"; $file_name = "webpreparations-1510425974.pdf"; $mail->Body = $mailContent; $mail->addAttachment($path.$file_name); $mail->SMTPOptions = array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ) ); // Send email if(!$mail->send()) { echo '<div class="alert alert-danger">Mail could not be sent.</div>'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo '<div class="alert alert-success">Mail has been sent successfully..</div>'; } /*--------------------- for sending mail close ------------------*/ } ?> <html> <head> <title>send mail using mail function in php</title> </head> <div class="container"> <h2>Mail sending form with Attachment using phpmailer of Web Preparations</h2> <form action="" method="POST"> <div class="form-group"> <label for="email">Email:</label> <input type="email" class="form-control" id="email" placeholder="Please enter your email" name="email"> </div> <button type="submit" name="send" class="btn btn-info">Send Mail</button> </form> </div> </body> </html> |
Step 2:- Now we need to download phpmailer here
Step 3:- Now we extract zip folder in our project’s root directry like “htdocs\webpreprations\demo\mail\PHPMailer” as we can see in bellow snapshot
Step 4:- Now we create “upload” folder for containing attachments and copy attachment file like “.pdf”, “.docx” etc file which we want to attach to mail
Step 5:- Now run our project, so we need to type in browser localhost/your_project_name/mail/file_name.php
like
http://localhost/webpreprations/demo/how-to-send-mail-using-php-mailer-in-php/index.php then you can see the result as in below snapshot, you can Send mail By click on Send Mail button
Congratulations you have successfully Completed Mail Sending Process using phpmailer with attachments in PHP, if you like this post and was helpful for you then share this post on social media and if you have any query then please contact us or comment below and don’t Forget Like and Follow us on Social Media, Thanks.