Sending Multiple Emails by looping a text file in Codeigniter

368 Views Asked by At

I am trying to send multiple emails in CodeIgniter. All my emails along with the Name of Client and Address are in a text file. In the below manner:

[email protected]
xxxxxxName of the Clientxxxxxx
xxxxxxAddress of the Clientxxxxxx
[email protected]
xxxxxxName of the Clientxxxxxx
xxxxxxAddress of the Clientxxxxxx

Now I am looping through this file contents with the below class and sending mails.

<?php
/**
 * Sending Emails to Clients
 */
class Email_clients extends CI_Controller {

    function index(){

        $file = file("emails.txt");

        $this->load->library('email');

        $config['mailtype'] = "html";

        $this->email->initialize($config);      

        $i = 0;

        while($i<count($file)) {

            $email = $file[$i];
            $name = $file[$i+1];
            $address = $file[$i+2];

            //echo $email."<br/>".$name."<br/>".$address."<br/><br/>";

            $message = <<<HTML

<!doctype html>
<html>
<head>
    <link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
    <style>
        *{
            font-family: Open Sans;
            font-size: 14px;
        }
        body{
            background-color: rgb(255, 251, 242);
        }
        p{
            text-indent: 50px;
            text-align: justify;
        }
        p.footer-email{
            font-weight: bold;
            color: rgb(255, 121, 0);
            text-indent: 0px;
        }
        p.header-email{
            text-indent: 0px;
            font-weight: bold;
        }
        p.thanking-you{
            margin: 10px 15%;
        }
    </style>
</head>
<body>

    <p class="header-email">
        To,<br/>
        Respected Sir/Madam,<br/>
        $name<br/>
        $address
    </p>

    <p>Lorem Ipsum dolor Lorem Ipsum dolor Lorem Ipsum dolor Lorem Ipsum dolor</p>

    <p>Lorem Ipsum dolor Lorem Ipsum dolor Lorem Ipsum dolor Lorem Ipsum dolor</p>

    <p>Lorem Ipsum dolor Lorem Ipsum dolor Lorem Ipsum dolor Lorem Ipsum dolor</p>

    <p class="thanking-you">Thanking You,</p>

    <p class="footer-email">
    Regards,<br/>
    XXX XXX XXX Company,<br/>  
    India
    </p>
</body>
</html>         

HTML;

        $this->email->from("[email protected]", "XYZ XYZ");
        $this->email->to($email);
        $this->email->subject("XYZ");
        $this->email->message($message);

        if($this->email->send()){
            $this->email->clear();
            echo "<p style='color: green;'>Mail sucessfully sent to $name</p>";
        }
        else{
            echo "<p style='color: red;'>Mail failed to send to $name</p>";
            show_error($this->email->print_debugger());
        }

            $i += 3;
        }

    }

}

The point where I was strucked with this is, The mail is delivering only to the last email by leaving all the above one's.

So what I did is, without sending a mail just i printed all the emails along with the names & addressess. It is echoing out perfectly without missing any Email ID. But the mail is not delivering except to last email.

while($i<count($file)) {

            $email = $file[$i];
            $name = $file[$i+1];
            $address = $file[$i+2];

            echo $email."<br/>".$name."<br/>".$address."<br/><br/>";
}

Lastly the style is also not applying which is present in $message.

1

There are 1 best solutions below

3
On

updated this code

             function index(){

                    $file = file("emails.txt");

                    $this->load->library('email');

                    $config['mailtype'] = "html";

                    $this->email->initialize($config);      

                    $i = 0;

                    $linecount = 0;
                    $handle = fopen("emails.txt", "r");
                    while(!feof($handle)){
                      $line = fgets($handle);
                      $linecount++;
                    }
                    $linecount /=3;;

                    while($i<$linecount) {