In this post, I am going to explain “How to Send Mail with Attachment in PHP”. In web application generally implement email send functionality such as user email verification with terms and condition files, orders details, order invoice mail etc.
So in this post we have explained how send email with attachment in PHP easily using PHPMailer. PHPMailer is the best way to send mail with attachment or html mail for that you need to download php mailer library. PHPMailer library handle any type of files in mail and you can send any type of mail using phpmailer library in php.
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 |
<?php require_once('mailer/PHPMailer-master/class.phpmailer.php'); $mail = new PHPMailer(true); try { $mail->setFrom('from@example.com', 'Webprepration'); // Add from email $mail->addAddress('webprepration@gmail.com'); // Change recipient email $mail->addReplyTo('info@example.com', 'Information'); // Add reply mail $mail->addCC('cc@example.com'); $mail->addBCC('bcc@example.com'); //Attachments $mail->addAttachment('testfile.pdf'); // Add attachments with your path and file name $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body <b>in bold!</b>'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } ?> |