【问题标题】:Sending Multiple Emails by looping a text file in Codeigniter通过在 Codeigniter 中循环文本文件来发送多封电子邮件
【发布时间】:2015-06-24 14:29:21
【问题描述】:

我正在尝试在 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;
        }

    }

}

让我印象深刻的一点是,邮件只发送到最后一封邮件,留下所有上述邮件。

所以我所做的是,没有发送邮件,我只打印了所有电子邮件以及姓名和地址。它完美呼应而不会丢失任何电子邮件 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 中的样式也没有应用。

【问题讨论】:

    标签: php codeigniter email file-handling


    【解决方案1】:

    更新了这段代码

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

    【讨论】:

    • 你能详细解释一下吗。
    • echo count($file) 并检查行数
    • 是的,先生,我检查过了,它与 6 完美呼应,因为只有六行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 2015-05-12
    • 2020-02-24
    相关资源
    最近更新 更多