PHPMailer不能以简单的形式使用默认的mail()

时间:2014-01-09 13:36:35

标签: php phpmailer

所以我试图在最后几个网站使用邮件(arg)之后尝试使用phpMailer但是这个下一个网站我希望能够将文件附加到电子邮件中,我认为phpMailer会更容易实现这一点。

现在我有一个非常简单的表单和sendmail.php页面不起作用。

这是代码......

HTML -----------------------------

  <form role="form" action="_/includes/sendmail.php" enctype="multipart/form-data" method="POST" autocomplete="on">
    <div class="form-group">
        <label class="emph1">First and Last Name:</label>
        <input type="text" class="form-control" id="exampleInputEmail1" placeholder="First and Last Name">
    </div>
    <div class="form-group">
        <label class="emph1">Email Address:</label>
        <input type="email" class="form-control" id="exampleInputPassword1" placeholder="Email">
    </div>
    <div class="form-group">
        <label class="emph1">File/Form Attachments:</label>

        <?php 
        //Maximum file size (in bytes) must be declared before the file input field and can't be large than the setting for
        // upload_max_filesize in php.ini.
        // PHP will stop and compain once file is exceeded
        // 1 mb is actually 1,048,576 bytes.
        ?>

        <input type="hidden" name="MAX_FILE_SIZE" value="5000000"  />
        <input type="file" id="exampleInputFile" name="file_upload">
        <p class="help-block">Please use .jpg, .pdf, or Word based files.</p>
        <p> <?php echo $message;?>
    </div>
    <div class="form-group">
      <label class="emph1">Reason for contacting Derek Davis, PLLC:</label>
      <div class="checkbox">
        <label>Estate Planning<input type="checkbox"></label>
      </div>
      <div class="checkbox">
        <label>Family Law<input type="checkbox"></label>
      </div>
      <div class="checkbox">
        <label>Criminal Defense<input type="checkbox"></label>
      </div>
      <div class="checkbox">
        <label>Collections<input type="checkbox"></label>
      </div>
      <div class="checkbox">
        <label>Landlord-Tenant<input type="checkbox"></label>
      </div>
      <div class="checkbox">
        <label>Other<input id="checkbox-other" type="checkbox"></label>
      </div>

         <!-- jQuery input feature (displays when "other" checkbox is checked) --->

        <div class="form-group" id="input-other" style="display:none;">
            <label class="emph1" for="">If 'other' please specify:</label>
            <input type="text" class="form-control" id="otherProject" name="otherProject" placeholder="Enter short description here." value="" />
        </div>
   </div>

         <!-- End jQuery input feature -->

        <div class="form-group">
        <label class="emph1">Please leave us a brief message:</label>
        <textarea  class="full-width" type="text" name="message" rows="10" placeholder="Please be as specific as possible..." required>
        </textarea><br />     
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
        </form>

PHP --------------------------

<?php ob_start()?>
<?php
function redirect_to($new_location) {
header("Location:" . $new_location);
exit;   
}

require_once("_/includes/phpmailer/class.smtp.php");
require_once("_/includes/phpmailer/class.phpmailer.php");

$mail = new PHPMailer();

if (isset($_POST['message'])){
$body = $_POST['message'];
}
$mail->isMail();
// Set PHPMailer to use the sendmail transport
//Set who the message is to be sent from
//Set an alternative reply-to address
$mail->addReplyTo('jcbbuller@gmail.com', 'First Last');
//Set the subject line
$mail->addAddress('jcbbuller@yahoo.com', 'John Doe');

$mail->Subject = 'PHPMailer sendmail test';
//Replace the plain text body with one created manually
$mail->Body = $body;
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment($temp_file, $temp_file_name);

//send the message, check for errors
if ($mail->Send()) {
redirect_to("../../contact.php");
} else {
redirect_to("../../index.html");
}
?>
<?php ob_end_flush(); ?>

我理所当然地知道你会想要设置一个SMTP并为变量分配SMTP信息,但是现在我只是想让这个糟糕的事情发挥作用。我在php的末尾做了一个布尔值,所以我至少知道它是否执行但是页面没有重定向...任何建议都会受到欢迎!感谢

1 个答案:

答案 0 :(得分:1)

也许首先要做的是检查出了什么问题:

try {
    $mail->isMail();
    //....
}
catch (phpmailerException $e) {
    echo $e->errorMessage();
}