PHP Mailer中的变量“to”地址

时间:2013-06-18 02:49:42

标签: email phpmailer email-integration php

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

我正在使用我购买的网站附带的示例,但尝试将其修改为包含到字段。现在我只是试图让一个新变量显示但没有成功。这是我现在使用的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,我也可以发布。

再次感谢,

-Rocky Colt TumTum

1 个答案:

答案 0 :(得分: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);
?>