邮件功能仅在第一个实例上发送

时间:2017-11-03 05:49:06

标签: php email

我有一些代码可以发送电子邮件给我和另一个地址。 但是,它只发送代码中mail()函数的第一次调用。即第一次迭代有效但第二次无效。它不是电子邮件地址的问题,因为如果我翻转它的工作原理。有什么建议吗?

<?PHP    
$to = 'email1@test.com';
$subject = 'test title 1';
$message = '
<html>
<head>
<title>Test 1</title>
</head>
<body>
<h1>test 1</h1>
<p>test 1</p>
</body>
</html>
';
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
$headers[] = 'From: Test <donotreply@test.com>';
mail($to, $subject, $message, implode("\r\n", $headers));

$to = 'email2@test.com';
$subject = 'test title 2';
$message = '
<html>
<head>
<title>Test 2</title>
</head>
<body>
<h1>test 2</h1>
<p>test 2</p>
</body>
</html>
';
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
$headers[] = 'From: Test <donotreply@test.com>';
mail($to, $subject, $message, implode("\r\n", $headers));
?>

1 个答案:

答案 0 :(得分:0)

试试此代码,它只发送一封电子邮件,因为 $ headers [] 数组。

<?PHP   
$headers1 = $headers2 = array(); 
$to = 'email1@test.com';
$subject = 'test title 1';
$message = '
<html>
<head>
<title>Test 1</title>
</head>
<body>
<h1>test 1</h1>
<p>test 1</p>
</body>
</html>
';
$headers1[] = 'MIME-Version: 1.0';
$headers1[] = 'Content-type: text/html; charset=iso-8859-1';
$headers1[] = 'From: Test <donotreply@test.com>';
mail($to, $subject, $message, implode("\r\n", $headers1));

$to = 'email2@test.com';
$subject = 'test title 2';
$message = '
<html>
<head>
<title>Test 2</title>
</head>
<body>
<h1>test 2</h1>
<p>test 2</p>
</body>
</html>
';
$headers2[] = 'MIME-Version: 1.0';
$headers2[] = 'Content-type: text/html; charset=iso-8859-1';
$headers2[] = 'From: Test <donotreply@test.com>';
mail($to, $subject, $message, implode("\r\n", $headers2));