非常简单的PHPmailer不工作

时间:2014-01-09 04:22:28

标签: php email phpmailer

我第一次尝试使用PHPmailer,即使只是简单的设置,我也无法收到电子邮件。我正在使用phpmailer使用的简单的默认php mail()函数,我之前在其他表单上使用过带有参数的mail()并且这些电子邮件已经通过但是在这个特定的网站上我也希望发送附件。到目前为止,这是我的代码,请注意附件,名称,电子邮件或复选框尚未被提取并添加到php代码中,我只是想要工作并发送textarea中的任何内容。请帮忙!!!

- 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>

- PHPMailer的---------------------------

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


if (isset($_POST['message'])){
$body = $_POST['message'];
}

// Set PHPMailer to use the sendmail transport
//Set who the message is to be sent from
$mail->setFrom('myemail@gmail.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('myemail@gmail.com', 'First Last');
//Set the subject line
$mail->addAddress('myotheremail@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
$mail-Send();
?>

感谢您的快速回复!!我对send()进行了更改并启动了类,但它仍然没有向我的yahoo.com电子邮件地址发送垃圾。现在是代码......

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

$mail = new PHPMailer;

if (isset($_POST['message'])){
$body = $_POST['message'];
}


// Set PHPMailer to use the sendmail transport
//Set who the message is to be sent from
$mail->setFrom('myemail@gmail.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('myemail@gmail.com', 'First Last');
//Set the subject line
$mail->addAddress('myotheremail@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
$mail->send();


?>

UPDATE !!!

好的,现在我的代码。它不仅不会向我的电子邮件发送消息,而且也不会将我重定向到任何一个页面......有什么建议吗?

<?php ob_start()?>
<?php
require_once("_/includes/phpmailer/class.smtp.php");
require_once("_/includes/phpmailer/class.phpmailer.php");

$mail = new PHPMailer();

if (isset($_POST['message'])){
$body = $_POST['message'];
}


// Set PHPMailer to use the sendmail transport
//Set who the message is to be sent from
$mail->setFrom('myemail@gmail.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('myemail@gmail.com', 'First Last');
//Set the subject line
$mail->addAddress('myotheremail@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(); ?>

4 个答案:

答案 0 :(得分:2)

看起来你从未初始化$mail。在任何$mail->方法之前添加以下行。请参阅documentation

中的示例
$mail = new PHPMailer;

要了解有关类构造函数的更多信息,请查看PHP documentation

你有一个拼写错误:$mail-Send();应该是$mail->Send();

答案 1 :(得分:1)

这看起来像是一个错字:

$mail->Send();

答案 2 :(得分:1)

//send the message, check for errors
$mail-Send();

应该是$ mail-&gt; send(); ?

答案 3 :(得分:1)

正在使用SMTP。使用smtp的正确方法是这样的

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP

$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "tls";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 587;                   // set the SMTP port for the GMAIL server
$mail->Username   = "yourusername@gmail.com";  // GMAIL username
$mail->Password   = "yourpassword";            // GMAIL password

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";

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

$mail->MsgHTML($body);

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}