使用PHPmailer将电子邮件发送给许多地址 [英] Send email to many adressees using PHPmailer

查看:60
本文介绍了使用PHPmailer将电子邮件发送给许多地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个站点,以便可以将消息发送到多个电子邮件帐户.我的HTML看起来像这样:

I am making a site when I could send message to multiple email accounts. My HTML looks like that:

    <!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):

And my PHP code is following (I am using 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 的提交按钮时,该电子邮件将出现在上方列表中(现在是 testb @ localhost ),当我单击 Saada 时,电子邮件将被发送到列表中的所有电子邮件地址.我应该在代码中添加些什么来实现这一目标?

I have made it work to some extent. The message I insert into text field will be sent to email address defined in my PHP code. In this case it is testb@localhost ($mail->AddAddress('testb@localhost');). What I want to do now is that when I insert new email address into smaller textbox on my HTML site and click on submit button called Lisa adressaat the email will appear in list above (where right now is testb@localhost) and when i click on Saada the email will be delivered to all email addresses which are in the list. What should I add to my code to achieve this?

提前谢谢

推荐答案

获取所有电子邮件地址并将其存储在数组中.一旦将它们放入数组中,就只需遍历所有它们.

Get all the emailaddresses and store them in an array. Once they are in the array it's just a matter of looping over all of them.

$mailaddresses; //this is the array with the emailadresses, store the emailaddresses here
foreach($mailaddresses as $mailaddress)
{
    $mail->AddAddress($mailaddress);
}

从输入字段获取电子邮件地址,其中电子邮件地址之间用逗号分隔.

getting the emailaddresses from an inputfield, where the emailaddresses are separated by a comma.

在html中:

<input type="text" value="" name="mail">

在php文件中:

$mailaddresses = explode(',', str_replace(' ', '', $_POST['mail']));
//the foreach loop goes here

其他信息: str_replace 是为了消除可能在逗号之后或之前插入的空格. explode 会在放置逗号的位置拆分字符串,并将其作为值数组返回.

Additional info: str_replace is to get rid of spaces that are maybe inserted after or before the comma. explode splits the string where ever a comma is placed and returns them as an array of values.

这篇关于使用PHPmailer将电子邮件发送给许多地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆