PHP 订阅者邮件列表

发布于 2024-09-25 06:16:56 字数 1436 浏览 7 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮云壑 2024-10-02 06:16:56

数据库方式确实是最好的一种。如果您更愿意使用文本文件方法,我会建议这样的操作:

将数据插入文件

$email = "the email";
$firstName = "the first name";
$lastName = "the last name";

$new_line = "$email|$firstName|$lastName\n"; // |  could be other character

$file = fopen("subscribers.txt", "a");
fputs($file, $new_line);
fclose($file);

读取和解析数据

$subscribers = array();

$handle = @fopen("subscribers.txt", "r");
if ($handle) {
    while (!feof($handle)) {
        $line = fgets($handle, 4096);

        //parsing the line
        $ar = explode('|', $line);

        //$ar[0] holds the email
        if(key_exists(0, $ar)){
          $email = $ar[0];
        }else{
          $email= '';
        }

        //$ar[1] holds the first name
        if(key_exists(1, $ar)){
          $firstName = $ar[1];
        }else{
          $firstName = '';
        }

        //$ar[2] holds the last name
        if(key_exists(2, $ar)){
          $lastName = $ar[2];
        }else{
          $lastName = '';
        }

        $temp = array(
          'email' => $email,
          'firstName' => $firstName,
          'lastName' => $lastName
        );

        $subscribers[] = $temp;
        //

    }
    fclose($handle);
}

For循环订阅者并使用您的函数发送电子邮件

foreach($subscribers as $subscriber){
    //the email
    $subscriber['email'];

    //the firstname
    $subscriber['firstName'];

    //the lastname
    $subscriber['lastName'];
}

The database way is, indeed, the best one. If you rather use the text file approach i would suggest something like this:

Inserting Data into the file

$email = "the email";
$firstName = "the first name";
$lastName = "the last name";

$new_line = "$email|$firstName|$lastName\n"; // |  could be other character

$file = fopen("subscribers.txt", "a");
fputs($file, $new_line);
fclose($file);

Reading and Parsing Data

$subscribers = array();

$handle = @fopen("subscribers.txt", "r");
if ($handle) {
    while (!feof($handle)) {
        $line = fgets($handle, 4096);

        //parsing the line
        $ar = explode('|', $line);

        //$ar[0] holds the email
        if(key_exists(0, $ar)){
          $email = $ar[0];
        }else{
          $email= '';
        }

        //$ar[1] holds the first name
        if(key_exists(1, $ar)){
          $firstName = $ar[1];
        }else{
          $firstName = '';
        }

        //$ar[2] holds the last name
        if(key_exists(2, $ar)){
          $lastName = $ar[2];
        }else{
          $lastName = '';
        }

        $temp = array(
          'email' => $email,
          'firstName' => $firstName,
          'lastName' => $lastName
        );

        $subscribers[] = $temp;
        //

    }
    fclose($handle);
}

For looping through the subscribers and using you function to send email

foreach($subscribers as $subscriber){
    //the email
    $subscriber['email'];

    //the firstname
    $subscriber['firstName'];

    //the lastname
    $subscriber['lastName'];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文