0

我正在尝试为使用 phpmailer 通过 html 表单发送的电子邮件设置“收件人”地址。最终目标是利用下拉列表让用户根据需要选择要联系的人。但是,由于我对 PHP 知之甚少,因此我将一步一步地进行,并且已经陷入困境。

我正在使用我购买的网站附带的示例,但尝试对其进行修改以包含一个 to 字段。现在我只是试图让一个新变量出现但没有成功。这是我现在使用的 HTML 表单:

<form name="contact" method="post" action="phpmailer.php" class="af-form" id="af-form">
<div class="af-outer af-required">
    <div class="af-inner">
        <label for="toemail" id="toemail_label">To Whom:</label>
        <input type="text" name="toemail" id="toemail" size="30" value="" class="text-input span8"/>
        <label class="error" for="toemail" id="toemail_error">To field is required.</label>
    </div>
</div>
<div class="af-outer af-required">
    <div class="af-inner">
        <label for="name" id="name_label">Your Name:</label>
        <input type="text" name="name" id="name" size="30" value="" class="text-input span8"/>
        <label class="error" for="name" id="name_error">Name is required.</label>
    </div>
</div>
<div class="af-outer af-required">
    <div class="af-inner">
        <label for="email" id="email_label">Your Email:</label>
        <input type="text" name="email" id="email" size="30" value="" class="text-input span8"/>
        <label class="error" for="email" id="email_error">Email is required.</label>
    </div>
</div>
<div class="af-outer af-required">
    <div class="af-inner">
        <label for="input-message" id="message_label">Your Message:</label>
        <textarea name="message" id="input-message" cols="30" class="text-input span8"></textarea>
        <label class="error" for="input-message" id="message_error">Message is required.</label>
    </div>
</div>
<div class="af-outer af-required">
    <div class="af-inner">
        <input type="submit" name="submit" class="form-button btn btn-large" id="submit_btn" value="Send Message!" />
    </div>
</div>

姓名、电子邮件和消息字段被很好地提取并填充到我收到的电子邮件中。但是,“toemail”字段不显示。这是我用于 phpmailer 的 process.php。

    <?php
if ((isset($_POST['toemail'])) && (strlen(trim($_POST['toemail'])) > 0)) {
    $toemail = stripslashes(strip_tags($_POST['toemail']));
} else {$toemail = 'No to address entered';}
if ((isset($_POST['name'])) && (strlen(trim($_POST['name'])) > 0)) {
    $name = stripslashes(strip_tags($_POST['name']));
} else {$name = 'No name entered';}
if ((isset($_POST['email'])) && (strlen(trim($_POST['email'])) > 0)) {
    $email = stripslashes(strip_tags($_POST['email']));
} else {$email = 'No email entered';}
if ((isset($_POST['message'])) && (strlen(trim($_POST['message'])) > 0)) {
    $message = stripslashes(strip_tags($_POST['message']));
} else {$message = 'No phone entered';}
ob_start();d
?>
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
<table width="550" border="1" cellspacing="2" cellpadding="2">
  <tr bgcolor="#eeffee">
    <td>To Whom:</td>
    <td><?=$toemail;?></td>
  </tr>
  <tr bgcolor="#eeffee">
    <td>Name</td>
    <td><?=$name;?></td>
  </tr>
  <tr bgcolor="#eeeeff">
    <td>Email</td>
    <td><?=$email;?></td>
  </tr>
  <tr bgcolor="#eeffee">
    <td>Message</td>
    <td><?=$message;?></td>
  </tr>
</table>
</body>
</html>
<?
$body = ob_get_contents();

$to = 'personalemailtousefortesting.com';
$email = 'email@example.com';
$fromaddress = "you@example.com";
$fromname = "Online Contact";

require("phpmailer.php");

$mail = new PHPMailer();

$mail->From     = "mail@yourdomain.com";
$mail->FromName = "Contact Form";
$mail->AddAddress("mypersonalemailfortestingpurposes@gmail.com","Name 1"); // addresses here

$mail->WordWrap = 50;
$mail->IsHTML(true);

$mail->Subject  =  "Contact form submitted";
$mail->Body     =  $body;
$mail->AltBody  =  "This is the text-only body";

if(!$mail->Send()) {
    $recipient = 'mypersonalemailfortestingpurposes@gmail.com';
    $subject = 'Contact form failed';
    $content = $body;   
  mail($recipient, $subject, $content, "From: mail@yourdomain.com\r\nReply-To: $email\r\nX-Mailer: DT_formmail");
  exit;
}
?>

我收到的电子邮件很好地填充了名称、电子邮件和消息字段,但是,对于“toemail”输入,我收到了“不输入地址”的回退错误消息。

这是第一个问题。如果有人需要查看,我将发布我正在使用的 phpmailer.php。我不知道我是否需要先在其中定义变量。

在我开始工作之后,我需要让它适用于下拉列表,这样用户就不能只将电子邮件发送给他们想要的任何人。

然后,我需要将其设置为实际的发送地址。我不知道如何为发送目的设置实际地址。我会假设我改变了

    $mail->AddAddress("mypersonalemailfortestingpurposes@gmail.com","Name 1"); // addresses here

像...

    $mail->AddAddress($toemail,"Name 1"); // addresses here

...但很可能这是不正确的。

你能提供的任何帮助都会很棒。

如果您需要查看我的 phpmailer.php,我也可以发布。

再次感谢,

-洛基柯尔特 TumTum

4

1 回答 1

0

以防万一你没有找到这个,这是 PHP 手册网站上列出的内容。

<?php
// multiple recipients
$to  = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';

// subject
$subject = 'Birthday Reminders for August';

// message
$message = '
<html>
<head>
  <title>Birthday Reminders for August</title>
</head>
<body>
  <p>Here are the birthdays upcoming in August!</p>
  <table>
    <tr>
      <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
    </tr>
    <tr>
      <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
    </tr>
    <tr>
      <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
    </tr>
  </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
?>
于 2013-06-18T03:25:39.940 回答