通过在Codeigniter中循环文本文件来发送多个电子邮件

时间:2015-06-24 14:29:22

标签: php codeigniter email file-handling

我正在尝试在CodeIgniter中发送多封电子邮件。我的所有电子邮件以及客户名称和地址都在文本文件中。以下面的方式:

raj@gmail.com
xxxxxxName of the Clientxxxxxx
xxxxxxAddress of the Clientxxxxxx
raj2@gmail.com
xxxxxxName of the Clientxxxxxx
xxxxxxAddress of the Clientxxxxxx

现在我使用下面的类循环浏览此文件内容并发送邮件。

<?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;
        }

    }

}

我对此感到震惊的是,邮件只通过留下上述所有邮件而发送到最后一封电子邮件。

所以我所做的就是,在没有发送邮件的情况下,我打印了所有电子邮件以及名称和电子邮件。 addressess。它完美地回荡,没有遗漏任何电子邮件ID。但邮件除了发送电子邮件外没有送达。

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

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

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

最后,该样式也未应用 $ message 中的样式。

1 个答案:

答案 0 :(得分:0)

更新了此代码

             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) {