简体   繁体   English

发送php邮件到csv列表

[英]Sending php mail to csv list

Im trying to write a simple script to send a message to .csv list of e-mail addresses like this: 我试图编写一个简单的脚本来将消息发送到.csv电子邮件地址列表,如下所示:

mail1@mail.com,
mail2@mail.com,
mail3@mail.com,

Here is what I have tried: 这是我尝试过的:

<?
$csv = array();
$file = fopen('test.csv', 'r');

while (($result = fgetcsv($file)) !== false){
$csv[] = $result;   

$to          = $result[0];

$subject = "My mail subject";


$message .= '<html><body>';
$message .= 'My message here';
$message .= "</body></html>";


$from = "example@example.com";
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

mail($to, $subject, $message, $headers);
}

fclose($file);
?>

But the message is sent three times, and includes the message three times in the body. 但是该消息被发送了三遍,并且在体内包含了三遍。 How can I make the message only be included and sent one time to each line? 如何使消息仅包含并发送到每一行一次?

You should remove $subject, $message, $from and $headers definition from the while loop. 您应该从while循环中删除$ subject,$ message,$ from和$ headers定义。 Define all of them before and in the while define only the $to field and keep the mail() command. 在所有之前和同时定义所有它们,同时仅定义$ to字段并保留mail()命令。

Either follow the above suggestion (good approach) or simply remove concatenation operator "." 请遵循上述建议(好的方法),或者只是删除串联运算符“”。 form first assignment to variable $message ie change 对变量$ message进行第一个赋值,即更改

$message .= '<html><body>'; TO
$message = '<html><body>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM