设置不同的"到"使用PHPMailer以2种不同的形式进行地址

时间:2014-10-29 22:27:38

标签: php forms

我有两个相同的表格,比方说“form01”和“form02”。提交表单时,将发送一封电子邮件。我想为这两个表单设置2个不同的电子邮件,因此2个不同的人将收到电子邮件通知。我可以在一个“process.php”中完成吗?我使用PHPMailer。

<form name="form01" id="form01" action="process.php">
<form name="form02" id="form02" action="process.php">

在“process.php”中:

<?php 

//Here I want to set 2 different "to" email address to 2 forms
//form01 ----> when submitted, send a email to person01 by "email01@domain.com"
//form02 ----> when submitted, send a email to person02 by "email02@mdoman.com"

?>

由于

2 个答案:

答案 0 :(得分:0)

即使您要发送两个电子邮件地址,也不需要两个表单。以下是两种可能的方法:

方法1


<强> HTML

<form action="process.php" method="post">
    <input type="text" name="email1">
    <input type="text" name="email2">
    <input type="submit">
</form>

<强> process.php

require('wherever/this/lives/PHPMailerAutoload.php');
$mail = new PHPMailer();

// put whatever validation you want in here, and ultimately add the emails
if (isset($_POST['email1']))
    $mail->AddAddress($_POST['email1'], 'Person One');

if (isset($_POST['email2']))
    $mail->AddAddress($_POST['email2'], 'Person Two');

// Add the other PHPMailer settings you need, and then send
$mail->send();

方法2


此方法允许用户在单个字段中键入多个电子邮件,类似于Outlook或Lotus Notes等电子邮件客户端

<强> HTML

<form action="process.php" method="post">
    <input type="text" name="emails">
    <input type="submit">
</form>

<强> process.php

require('wherever/this/lives/PHPMailerAutoload.php');
$mail = new PHPMailer();

// put whatever validation you want in here, and ultimately add the emails
if (isset($_POST['emails'])){

    $emailSeparator = ',';
    $emails = explode($emailSeparator, $_POST['emails']);

    $send = false;
    foreach($emails as $emailAddress){

       // ... assuming the email is valid, add it to the recipient list 
       // and set sent to true
       $mail->AddAddress($emailAddress);
       $send = true;

   }

   if ($send)
       $mail->send();

}

答案 1 :(得分:0)

您可以在每个表单中放置一个隐藏字段,并检查它是否已提交! HTML:

<form name="form01" id="form01" action="process.php">
    <input type="hidden" name="submitted_form" value="form_01" />
</form>
...
<form name="form02" id="form02" action="process.php">
    <input type="hidden" name="submitted_form" value="form_02" />
</form>

和PHP:

if ($_POST['submitted_form'] == 'form_01') {
    // was submitted form_01
}

if ($_POST['submitted_form'] == 'form_02') {
    // was submitted form_02
}