LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / PHP Codes » How To Send Email With SMTP And Attachment Using phpmailer in PHP?

How To Send Email With SMTP And Attachment Using phpmailer in PHP?

How-To-Send-Email-With-SMTP-And-Attachment-Using-phpmailer-in-PHP
Sending an email is a very common activity in web applications and websites. For example, sending an email when a user wants to contact you from your website, a new user joins your network, sending a newsletter, sending greeting mail, or sending an invoice, etc. We can use the built-in mail() , a function that comes with PHP by default but that has some limitations and hard to understand with fewer features so that’s why we will go for PHPMailer Class to send an email programmatically.

PHPMailer is a PHP class library that is used for sending email and this class has more functionality compared to the simple PHP mail() function including file attachment also. It is very useful if you have SMTP details then you can use that details into your PHP script and from your application, you can send an email and you can also attach a file with email on form submission by using this PHPMailer class. This class or library is full features as we are mentioning some of the features in a list below that will let you know about this.

  • SMTP Configuration
  • SSL SMTP Connection
  • SMTP Host Selection
  • SMTP Sending Port
  • SMTP Auth Configuration
  • SMTP Username / Password
  • SMTP Security SSL/TLS Selection
  • Email From And Its Edited Name
  • Email To And Its Edited Name
  • Email CC And Its Edited Name
  • Email BCC And Its Edited Name
  • Plain Body And HTML Markup Body
  • Email Subject
  • Email Attachments
  • And Many More…

Table of Contents

How To Send Email With SMTP And Attachment Using phpmailer in PHP?

Before using the below code, you have to download 2 files as mentioned below from PHPMailer Class and save both it to your desired same directory and edit the path of one file in the below code.

  • class.phpmailer.php
  • class.smtp.php
// Send Email With SMTP And Attachment Using phpmailer
require 'class.phpmailer.php'; // Change pather where you save this
$mail = new PHPMailer;
$mail->SMTPDebug = 4;
$mail->IsSMTP(); //Sets Mailer to send message using SMTP
$mail->SMTPOptions = array(
    'ssl' => array(
    'verify_peer' => false,
    'verify_peer_name' => false,
    'allow_self_signed' => true
  )
);
$mail->Host = 'XXXXXXXXXX'; //Sets the SMTP hosts of your Email hosting,
$mail->Port = 'XXXXXXXXXX'; //Sets the default SMTP server port
$mail->SMTPAuth = XXXXXXXXXX; //Sets SMTP authentication. Utilizes the Username and Password variables
$mail->Username = 'XXXXXXXXXX'; //Sets SMTP username
$mail->Password = 'XXXXXXXXXX'; //Sets SMTP password
$mail->SMTPSecure = 'XXXXXXXXXX'; //Sets connection prefix. Options are "", "ssl" or "tls"
$mail->From = 'XXXXXXXXXX'; //Sets the From email address for the message
$mail->FromName = 'XXXXXXXXXX'; //Sets the From name of the message
$mail->AddAddress('XXXXXXXXXX', 'XXXXXXXXXX'); //Adds a "To EMail, To Email Name" address
$mail->AddReplyTo('XXXXXXXXXX', 'XXXXXXXXXX'); //Adds a "Reply EMail, Reply Email Name" address
$mail->addCC("cc@example.com");
$mail->addBCC("bcc@example.com");
$mail->IsHTML(true); //Sets message type to HTML 
$mail->Subject = 'XXXXXXXXXX'; //Email subject 
//$mail->Body = 'XXXXXXXXXX'; //Email Body 
$mail->MsgHTML('XXXXXXXXXX'); //Email HTML Body 
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; 
$mail->AddAttachment($attachment , 'RenamedFile.pdf'); //Adds a "Online Hosted File URL, File Name" address , TO Upload Multiple Attachments, Duplicate This Line
if(!$mail->send()) //Send an Email. Return true on success or false on error 
{ 
    echo "ERROR: Message Could Not Be Sent. '".$mail->ErrorInfo."'"; 
} else { 
    echo "SUCCESS: Message Sent Successfully."; 
}
// Send Email With SMTP And Attachment Using phpmailer

Final Words:

Be aware that the is placed well in your document. Rest all is in your hand if you want to customize it or play with it. That’s all we have. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allowed to us. Don’t forget to share this with your friends so they can also take benefit from it and leave.

Recommended For You:
How To Validate/Identify Deprecated HTML Tags In A WebPage Using PHP?

You Like It, Please Share This Recipe With Your Friends Using...

  

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *