To Send Mail to User from SMTP using PHPMailer Library with Attachments:
PHPMailer is a powerful library for sending email from PHP. For sending e-mail, you have to first download PHPMailer library from GitHub and include it in your code snippet below:

Example :

 <?php
                               
   	// Including PHP Mailer Library
	require_once('PHPMailer-master/class.phpmailer.php'); 

	function smtpmailer($to, $from, $from_name, $sub, $msg)
	{
        $mail = new PHPMailer();
        $mail->IsSMTP();  // set mailer to use SMTP
        $mail->SMTPAuth = true;  // turn on SMTP authentication

        $mail->Host = "smtp.example.com";  // specify main server
        $mail->Port = 25; // SMTP Port	   		
        $mail->Username = "username";  // SMTP username
        $mail->Password = "password";  // SMTP password

        $mail->IsHTML(true);  // set email format to HTML
		$mail->AddAddress($to);
        $mail->From = $from;
        $mail->FromName = $from_name;
		$mail->Subject = $sub;
		$mail->Body = $msg;
        $mail->AddReplyTo($from, $from_name);
        
        $mail->addCC("cc1@example.com");
		$mail->addCC("cc2@example.com");
        $mail->addBCC("bcc1@example.com");
		$mail->addBCC("bcc2@example.com");
        $mail->AddAttachment("test.pdf");  // add attachment
		
        if(!$mail->Send())
        {
            return "Sorry, Unable to send mail..." . $mail->ErrorInfo;
        }
        else
        {
            return "Mail sent Successfully...";
        }
    }
	
	$to = "receiver@example.com";
	$from = "sender@example.com";
    $from_name = "Sender";
	$sub = "Reg: Test Mail";
	$msg = "Dear User,
This is a test mail from CodePHPOnline."; echo smtpmailer($to, $from, $from_name, $sub, $msg); /* Output : Mail sent Successfully... */ ?>
About Me
Rahul Yadav

Rahul Yadav

Technical Architect

Kuala Lumpur, Malaysia

Connect Me:     

I'm a Full Stack Web Developer working in a MNC and passionate of developing modern web and mobile applications.

I have designed and developed CodephpOnline & CodephpOnline Wiki platform to expatiate my coding and technology learning experiences.

In my leisure time, I write technical articles on web development such as PHP, Laravel, CodeIgniter, Mediawiki, Linux, Angular, Ionic, ReactJS, NodeJS, AJAX, jQuery, Cloud and more.

Tag Cloud