• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to send emails using CI 4

#1
I don't know this is silly question or not, but I did search for this in forum and google also but not getting any help. However, I came to know that PHPmailer and other libraries are coming in future, but for now, what is the code/how to do code that can send email to users.

I am developing simple sign in/sign up functionality using CI 4 where users needs to confirm their email address when they sign up. So I did try one or two codes but email is not sent to users. Here is code samples that I did try but not getting success. So can anyone please help me, how can I send emails to user.

Quote:Using simple PHP code :

PHP Code:
$to '[email protected]';
$subject 'the subject';
$message 'hello';
$headers 'From: [email protected]"\r\n" .
    
'Reply-To: [email protected]"\r\n" .
    
'X-Mailer: PHP/' phpversion();

if(
mail($to$subject$message$headers)) {
    echo 
'send';
}
else {
    echo 
'not send';    

Reply

#2
The PHP mail function nowadays only serves for internal testing.
Any SMTP server will reject emails from an unauthenticated account.

First register an account in gmail and then in the configuration of the Security section account, enable "Less secure app access"
Now every time you want to send an email you do it with that account, attached example below.

Anyway, using PHPMailer is the best option.

PHP Code:
if (!empty($_POST)) {

 
   $host 'ssl://smtp.gmail.com';
 
   $port '465';
 
   $user '[email protected]';
 
   $pass 'your-mail-password';
 
   $rcpt $user;
 
   $subj 'Web Form';
 
   $body '';
 
   $status false;

 
   foreach ($_POST as $i => $j) {
 
       $body .= strtoupper($i) . ': '$j "\r\n";
 
   }
 
   $body wordwrap(stripslashes($body), 70"\r\n");

 
   if (($fp = @fsockopen($host$port)) !== FALSE && substr(fgets($fp1024), 03) != '220') {
 
       $status 'CONNECTION FAIL';
 
   } else {
 
       fputs($fp"HELO $user\r\n");
 
       if (substr(fgets($fp1024), 03) != '250') {
 
           $status 'HELO FAIL';
 
       } else {
 
           fputs($fp"AUTH LOGIN\r\n");
 
           if (substr(fgets($fp1024), 03) != '334') {
 
               $status 'AUTH LOGIN FAIL';
 
           } else {
 
               fputs($fpbase64_encode($user) . "\r\n");
 
               if (substr(fgets($fp1024), 03) != '334') {
 
                   $status 'AUTH USER FAIL';
 
               } else {
 
                   fputs($fpbase64_encode($pass) . "\r\n");
 
                   if (substr(fgets($fp1024), 03) != '235') {
 
                       $status 'AUTH PASS FAIL';
 
                   } else {
 
                       fputs($fp"MAIL FROM: <$user>\r\n");
 
                       if (substr(fgets($fp1024), 03) != '250') {
 
                           $status 'MAIL FROM FAIL';
 
                       } else {
 
                           fputs($fp"RCPT TO: <$rcpt>\r\n");
 
                           if (substr(fgets($fp1024), 03) != '250') {
 
                               $status 'RCPT TO FAIL';
 
                           } else {
 
                               fputs($fp"DATA\r\n");
 
                               if (substr(fgets($fp1024), 03) != '354') {
 
                                    $status 'DATA FAIL';
 
                               } else {
 
                                   fputs($fp"To: $rcpt\r\nSubject: $subj\r\n\r\n$body\r\n.\r\n");
 
                                   if (substr(fgets($fp1024), 03) != '250') {
 
                                       $status 'BODY FAIL';
 
                                   } else {
 
                                       fputs($fp"QUIT \r\n");
 
                                       if (substr(fgets($fp1024), 03) != '221') {
 
                                           $status 'QUIT FAIL';
 
                                       } else {
 
                                           $status true;
 
                                       }
 
                                   }
 
                               }
 
                           }
 
                       }
 
                   }
 
               }
 
           }
 
       }
 
       fclose($fp);
 
   }

    if (
$status === true) {
 
       echo 'Your data was sent successfully.';
 
   } else {
 
       echo 'There was an error submitting your data (' $status ').';
 
   }


Reply

#3
There's a library in the works: https://forum.codeigniter.com/thread-73487.html
Reply

#4
Use this library https://github.com/PHPMailer/PHPMailer
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.