当我向多个电子邮件帐户发送邮件时,我正在创建一个网站。我的HTML看起来像这样:
<!doctype html>
<html>
<head>
<title>Saada</title>
</head>
<form action="test1.php" method="POST">
<body>
<header>Küsitluse pealkiri</header>
<br>
<ol>
<li>testb@localhost <input type="button" value="X"><br> <br>
</ol>
<input ><input type="button" value="Lisa adressaat"> <br> <br>Tekst adressaadile:<br>
<textarea rows='4' name='tekst'></textarea>
<br><footer>
<INPUT TYPE="submit" VALUE="Saada" NAME="Saada">
</form>
<FORM METHOD="LINK" ACTION="Minu küsitlused.html">
<INPUT TYPE="submit" VALUE="Loobu">
</footer>
</body>
</html>
我的PHP代码正在关注(我正在使用PHPMailer):
<?php
include_once 'init/init.funcs.php';
require 'C:/xampp2/htdocs/Praks/phpmailertesting/PHPMailer_5.2.4/class.phpmailer.php';
$body = mysql_real_escape_string($_POST['tekst']);
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'localhost'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'testa@localhost'; // SMTP username
$mail->Password = 'gggggg'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'testa@localhost';
$mail->FromName = 'Indrek';
$mail->AddAddress('testb@localhost'); // Name is optional
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->AddAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->AddAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = $body;
$mail->AltBody = $body;
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
?>
我在某种程度上使它成功了。我插入文本字段的消息将被发送到我的PHP代码中定义的电子邮件地址。在这种情况下,它是testb@localhost
($mail->AddAddress('testb@localhost');
)。我现在要做的是,当我在HTML网站上的较小文本框中插入新的电子邮件地址并点击名为 Lisa adressaat 的提交按钮时,电子邮件将显示在上面的列表中(现在位于{{ 1}})当我点击 Saada 时,电子邮件将被发送到列表中的所有电子邮件地址。我应该在代码中添加什么来实现这一目标?
提前谢谢
答案 0 :(得分:1)
获取所有电子邮件地址并将其存储在数组中。一旦他们进入阵列,它只是循环所有这些。
$mailaddresses; //this is the array with the emailadresses, store the emailaddresses here
foreach($mailaddresses as $mailaddress)
{
$mail->AddAddress($mailaddress);
}
编辑:从输入字段获取电子邮件地址,其中电子邮件地址以逗号分隔。
在html中:
<input type="text" value="" name="mail">
在php文件中:
$mailaddresses = explode(',', str_replace(' ', '', $_POST['mail']));
//the foreach loop goes here
附加信息:str_replace
用于删除可能在逗号之后或之前插入的空格。 explode
将字符串拆分为放置逗号的位置,并将其作为值数组返回。
答案 1 :(得分:0)
只是做一个foreach声明
$emails_sent = array();
foreach ($emails as $key => $value) {
$mail->AddAddress($value, $value);
$emails_sent[$value] = (int) $mail->Send();
$mail->ClearAddresses();
}
$ email将是您的电子邮件列表