【问题标题】:Get a line from txt file to php从txt文件中获取一行到php
【发布时间】:2020-06-30 19:01:03
【问题描述】:

我不想使用这个从 php 发送电子邮件:

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?> 

我有一个 .txt 文件:

...
send_email_to : example@example.com
...

所以,我想做的是获取send_email_to 并将其放在php.ini 上的$to 上。有什么办法吗?

【问题讨论】:

  • 当然。您可以阅读文本文件,解析相关行,并更改您的 $to var。
  • 你有没有尝试过?为什么要问这个问题?

标签: php file email


【解决方案1】:

如果整个文件内容格式与您在示例中提供的一样,您可以执行以下操作:

$options = array();

$fileResource = fopen('file.txt', 'r');
while (($line = fgets($fileResource)) !== false) {
    list($key, $value) = explode(' : ', $line);
    $options[$key] = trim($value);
}
fclose($fileResource);

echo $options['send_email_to'] ?? null; //example@example.com

【讨论】:

  • 不太确定list() 会明白你的意思,因为explode() 返回一个数组。
  • list 将简单地将 explode(...)[0][1] 中的值重新分配给新变量。
【解决方案2】:

你可以这样做。

foreach(file("file1.txt") as $line) {
$pieces = explode(":", $line);
$to = trim($pieces[1]);
//Send email here if you want to send email to each one in the file.
}

【讨论】:

  • 除非每次行中不存在冒号时都会收到警告和无效电子邮件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多