Jacob Buller
Jacob Buller

Reputation: 137

Very simple PHPmailer Not Working

I'm trying to use PHPmailer for the first time and even with a simple set up I am having trouble getting the email to go through. I am using the simple default php mail() function that phpmailer uses, I've used mail() with arguments before on other forms and those emails went through but on this particular site I want to have attachements sent as well. Here is my code so far, note that the attachments, name, email, or checkbox have not been pulled and added to the php code, I'm just getting the darn to work and send whatever is in the textarea. Please help!!!

--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('[email protected]', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('[email protected]', 'First Last');
//Set the subject line
$mail->addAddress('[email protected]', '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();
?>

Thanks for the quick responses!! I made the changes to send() and initiated the class but it still didn't send crap to my yahoo.com email address. Heres the code now....

<?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('[email protected]', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('[email protected]', 'First Last');
//Set the subject line
$mail->addAddress('[email protected]', '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!!!

Ok so here my code now. Not only does it not send a message to my email but it also doesn't redirect me to either page... Any suggestions?

<?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('[email protected]', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('[email protected]', 'First Last');
//Set the subject line
$mail->addAddress('[email protected]', '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(); ?>

Upvotes: 0

Views: 1858

Answers (4)

Veer
Veer

Reputation: 371

As you are using SMTP. The correct way to use smtp is like this

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   = "[email protected]";  // GMAIL username
$mail->Password   = "yourpassword";            // GMAIL password

$mail->SetFrom('[email protected]', 'First Last');

$mail->AddReplyTo("[email protected]","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 = "[email protected]";
$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!";
}

Upvotes: 1

nodeffect
nodeffect

Reputation: 1840

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

should be $mail->send(); ?

Upvotes: 1

mseifert
mseifert

Reputation: 5670

Looks like a typo here:

$mail->Send();

Upvotes: 1

Sam
Sam

Reputation: 20486

It looks like you never initialized $mail. Add the following line before any of your $mail-> methods. See the example in their documentation.

$mail = new PHPMailer;

To learn more about class constructors, check the PHP documentation.

And you have a typo: $mail-Send(); should be $mail->Send();

Upvotes: 2

Related Questions