codeigniter4 sending email with attachments




In this section, we’ll see how to sent email with attachments using email library.

To send the email with attachments, first we need to load the email’s library.

        $email = \Config\Services::email(); // where email() is static method of Services class.

        $email->setFrom('from@example.com', 'Your Name');
        $email->setTo('to@example.com');
	    $email->setCC('cc@example.com');
	    $email->setBCC('bcc@example.com');
	    $email->setSubject('Email Subject');
         
            // file attach here //
	    $email->attach('/path/to/file1.txt');
	    $email->attach('/path/to/file2.txt');
	    $email->attach('https://nexladder.com/images/nex_logo.png');

	    $email->setMessage('Message');

	    $email->send();

That’s it!. Please share your thoughts or suggestions in the comments below.

Leave a Reply

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