Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
raj@gmail.com
xxxxxxName of the Clientxxxxxx
xxxxxxAddress of the Clientxxxxxx
raj2@gmail.com
xxxxxxName of the Clientxxxxxx
xxxxxxAddress of the Clientxxxxxx

Now I am looping through this file contents with the below class and sending mails.
PHP
<?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("contact@xyz.com", "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.
PHP
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. As I am Beginner to CI Kindly help me to get out of this.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900