1

教育我:)

我有一个简单的 HTML 表单,由 3 个字段组成:姓名、电子邮件和消息。

我在下面创建了一些 PHP 代码,它将充当输入到包含固定消息和附件的表单中的电子邮件地址的自动回复器。

我希望将第二封电子邮件发送到一个固定的电子邮件地址给一家公司,该地址仅包含输入表格的姓名、电子邮件和消息。

<?php

  $field_fullname = $_POST['cf_mercury']; // cf_name is a convention used by the HTML form
  $field_email = $_POST['cf_jupiter'];
  $field_message = $_POST['cf_uranus'];

require_once('class.phpmailer.php');

$mail             = new PHPMailer(); // defaults to using php "mail()"

$body = $field_message;

$mail->SetFrom("company@address.co.uk", "ETAP Centre"); 

$mail->AddReplyTo("company@address.co.uk", "ETAP Centre");

$address = "email@address.co.uk";
$mail->AddAddress($field_email, $field_fullname);

$mail->Subject    = 'Auto Response: Message from the ETAP Centre';

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$mail->AddAttachment("");      // attachment
$mail->AddAttachment(""); // attachment

if(!$mail->Send()) {
  { ?>
    <script language="javascript" type="text/javascript">
        alert('Message failed. Please, send your email to company@address.co.uk');
        window.location = 'index.html';
    </script>
<?php
}
} else {
  ?>
    <script language="javascript" type="text/javascript">
        alert('Thank you for contacting the ETAP Centre. We will contact you shortly.');
        window.location = 'index.html';
    </script>
<?php
}
?>

任何和所有的帮助都将不胜感激,如果我能做更多的事情来表达我的问题并增加它的可理解性,请告诉我。谢谢!

4

3 回答 3

1

答案是通过$mail将第二封电子邮件中的变量更改为$mail2并添加mail2 = clone $mail;. 这允许用户完全控制两封带有可选附件的电子邮件,以及使用任一电子邮件中表单捕获的变量。谢谢你的帮助!:)

<?php

      $field_fullname = $_POST['cf_mercury']; // cf_name is a convention used by the HTML form
      $field_email = $_POST['cf_jupiter'];
      $field_message = $_POST['cf_uranus'];

    require_once('class.phpmailer.php');

    // E-Mail to Client

    $mail             = new PHPMailer(); // defaults to using php "mail()"

    $body = "Thank you for contacting the whoever";

    $mail->SetFrom('company@address.co.uk', 'Enter Sender Name'); 

    $mail->AddReplyTo('company@address.co.uk', 'Enter Sender Name');

    $address = $field_email;
    $mail->AddAddress($address, $field_fullname);

    $mail->Subject    = 'Auto-Response: Thank you for contacting the ETAP Centre, '.$field_fullname;

    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

    $mail->MsgHTML($body);

    $mail->AddAttachment("");      // attachment
    $mail->AddAttachment(""); // attachment

    $sent = $mail->Send();


    // E-Mail to Company

    $mail2 = clone $mail;

    $mail2             = new PHPMailer(); // defaults to using php "mail()"

    $body = $field_message;

    $mail2->SetFrom($field_email, $field_fullname); 

    $mail2->AddReplyTo($field_email, $field_fullname);

    $address = "company@address.co.uk";
    $mail2->AddAddress($address, "Enter Recipient Name");

    $mail2->Subject    = 'Message via the NAME website from '.$field_fullname;

    $mail2->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

    $mail2->MsgHTML($body);

    $mail2->AddAttachment(""); // attachment - leave incase they are needed in the future
    $mail2->AddAttachment(""); // attachment

    $sent = $mail2->Send();

    if($mail_status) {
      { ?>
<script language="javascript" type="text/javascript">
            alert('fail');
            window.location = 'index.html';
        </script>
<?php
    }
    } else {
      ?>
<script language="javascript" type="text/javascript">
            alert('success');
            window.location = 'index.html';
        </script>
<?php
    }
    ?>
于 2013-04-23T08:40:19.007 回答
1
<?php
$field_fullname = $_POST['cf_mercury']; // cf_name is a convention used by the HTML form
$field_email = $_POST['cf_jupiter'];
$field_message = $_POST['cf_uranus'];

require_once('class.phpmailer.php');

$mail             = new PHPMailer(); // defaults to using php "mail()"

$body = "Message";

$mail->SetFrom('company@address.co.uk', 'ETAP Centre'); 

$mail->AddReplyTo('company@address.co.uk', 'ETAP Centre');

$address = $field_email;
$mail->AddAddress($address, $field_fullname);

$mail->Subject    = 'Auto-Response: Thank you for contacting the ETAP Centre, '.$field_fullname;

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";     // optional, comment out and test

$mail->MsgHTML($body);

$mail->AddAttachment("/websites/123reg/LinuxPackage22/da/mt/ec/damtechdesigns.co.uk/public_html/proofs/etap/etapbooklet.pdf");      // attachment
$mail->AddAttachment(""); // attachment
$mail->Send();

// 给公司发电子邮件

 $mail             = new PHPMailer(); // defaults to using php "mail()"

 $body = $field_message;

 $mail->SetFrom($field_email, $field_fullname); 

 $mail->AddReplyTo($field_email,$field_fullname);

 $address = "company@address.co.uk";
 $mail->AddAddress($address, "ETAP Centre");

 $mail->Subject    = 'Message via the ETAP Centre website from '.$field_fullname;

 $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

 $mail->MsgHTML($body);

 $mail->AddAttachment(""); // attachment
 $mail->AddAttachment(""); // attachment
 $sent = $mail->Send();
 if($sent)
 {
  header("location:yoururl");
 }
 else
 {
  header("location:yoururl");
 }
于 2013-04-22T15:50:28.560 回答
1

只需在if 语句$mail = new PHPMailer(); // defaults to using php "mail()"之前重复以公司电子邮件地址开头的块作为收件人。Message Failed并且只包括您要发送的字段(即姓名、电子邮件和消息)。

或者,如果您愿意,您可以通过将公司电子邮件地址添加为电子邮件地址来发送相同电子邮件的副本BCC,方法是添加$mail->AddBCC

于 2013-04-22T15:19:57.937 回答