Hello Reader's, If you have developed the web project in Codeigniter and want to send attachments using emails then in this blog you can learn how to do this.
Sending the attachments can be done by several way in php, We will do this by using PHP Mailer since this is short and easy to send emails. So lets get started doing this:-
Step2:- Create new controller for sending the attachments and put the PHP mailer code as below:-
$email = new PHPMailer();
$email->From = 'email@example.com';
$email->FromName = 'sendername';
$email->Subject = 'This is a email test blog to send attachmetns';
$email->Body = $msg;
$email->AddAddress( 'firstname.lastname@example.org' );
$sourceAttachment = 'link the path of the your attachment herer';
$email->AddAttachment( $sourceAttachment , 'attachment.pdf' );
In the code above creating a new PHPMailer() will require the file which we have download in step1.
Extract the folder and paste it to somewhere it can be accessible by this controller.
Now in the beginning of this code you need to call PHPMailerAutoload.php file which can be done by line as follows:-
Now you just need to run this code and your email will send to destination email address. If you are not getting emails then in that case you need to use the SMTP. By using SMPT email is passes through a valid authentic process. For doing this you just need to turn on the SMPT by putting the code in the above code as follows:
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'email@example.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->Port = 587;
And this functionality can be also done by using simple php mail() function. But that will requires huge lines of code which is lead to many bugs to takes more time for fixing. You can see this code is really short and easy to implement and works fine.