【问题标题】:Very simple PHPmailer Not Working非常简单的 PHPmailer 不工作
【发布时间】:2014-01-27 11:49:03
【问题描述】:

我第一次尝试使用 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();


?>

更新!!!

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

<?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(); ?>

【问题讨论】:

  • 您必须首先声明 $mail 不能像它存在那样使用它并且是 PHPMailer 类型。首先使用$mail = new PHPMailer(); 创建$mail。您还包括 smtp 类,因此您可能必须在使用前对其进行配置。非常基本的例子phpmailer.worxware.com/index.php?pg=examplebsmtp
  • 啊啊啊,我忘了开括号和右括号,我试试看。

标签: php email phpmailer


【解决方案1】:

您似乎从未初始化过$mail。在您的任何 $mail-&gt; 方法之前添加以下行。请参阅他们的 documentation 中的示例。

$mail = new PHPMailer;

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

你有一个错字:$mail-Send(); 应该是$mail-&gt;Send();

【讨论】:

    【解决方案2】:

    这里好像打错了:

    $mail->Send();
    

    【讨论】:

      【解决方案3】:
      //send the message, check for errors
      $mail-Send();
      

      应该是 $mail->send(); ?

      【讨论】:

        【解决方案4】:

        当您使用 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!";
        }
        

        【讨论】:

        • 谢谢 我在stackoverflow的帖子中看到了类似的东西,为什么需要指定主机两次?您已将主机分配给您的 mail.yourdomain.com 服务器,然后再次分配给 smtp.gmail.com 服务器。
        • 我的错,如果您使用的是 gmail smtp,则不需要
        • 明白了,谢谢,现在我只使用默认的 php mail() 函数,但我计划在我的托管站点上设置 phpmailer 后使用 smtp。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多