0

I'm running locally from my mac, when the info is entered and I click send the email is not showing up in the outgoing email account in sent mail, and not showing up in the incoming email either; spam folder included. There is not an error echo. The echo states the message is sent without error. I'm at a loss here. By the way I am a newbie at coding so please be gentle and explain in depth. I appreciate any help that anyone might be able to provide. Here is my code.

<form method="post" enctype="multipart/form-data">

  <p>
    E-Mail:
  </p>
  <p>
    <input type="text" name="receiver" />
  </p>

  <p>
    Subject:
  </p>
  <p>
    <input type="text" name"subject" />
  </p>

  <p>
    Note:
  </p>
  <p>
    <textarea name="message"></textarea>
  </p>

  <p>
  Select Photo:
  <input type="file" name="file" />
</p>

<input type="submit" name="submit" value="Send Email" />

</form>


<?php

if (isset($_POST['submit'])) {

  require "php-mailer-master/PHPMailerAutoload.php";

  $mail = new PHPMailer;

try {
  
$sender = "????????????@gmail.com";

$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '$sender';
$mail->Password = '????????????';
$mail->SMTPSecure = 'tls';
$mail->Port = '587';

$mail->isHTML();

$mail->setFrom($sender, 'Mailer');
$mail->addAddress($_POST["receiver"]);
$mail->addAttachment ($file_name);
$mail->isHTML(true);

$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["message"];

$mail->send();
  echo 'Message has been sent';
} catch (Exception $e) {
  echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

}

  ?>
  • To watch the delivery of your message, set `$mail->SMTPDebug = 2`. PHPMailer doesn't place sent messages in your sent mailbox - you need an IMAP client to do that, and there is a basic example in [the gmail example provided with PHPMailer](https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps). – Synchro Mar 11 '18 at 04:53

1 Answers1

0

The code below will send a message for me. I replaced the ??? with my username and password (also had to set my gmail account to allow access).

Changes I made to your code: I changed the require for my composer locally. You used single quotes around the $sender - needs to be double quotes. Added titles to the inputs (probably not needed). Also used the namespaced values for instantiating $mail PHPMAILER(); Commented out the attachment feature.

I would also recommend checking into sanitizing your user input. Something like:

$name = trim(filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL));

Code:

<form method="post" enctype="multipart/form-data">

<p>
    E-Mail:
</p>
<p>
    <input type="text" name="receiver" title="receiver1"/>
</p>

<p>
    Subject:
</p>
<p>
    <input type="text" name="subject" title="subj1"/>
</p>

<p>
    Note:
</p>
<p>
    <textarea name="message" title="message"></textarea>
</p>

<p>
    Select Photo:
    <input type="file" name="file"/>
</p>

<input type="submit" name="submit" value="Send Email"/>

</form>

<?php
require '../vendor/autoload.php';

if (isset($_POST['submit'])) {

    $mail = new \PHPMailer\PHPMailer\PHPMailer();

    try {

        $sender = "?????????@gmail.com";
        $mail->isSMTP();
        $mail->Host = 'smtp.gmail.com';
        $mail->SMTPAuth = true;
        $mail->Username = "$sender";
        $mail->Password = '?????????????';
        $mail->SMTPSecure = 'tls';
        $mail->Port = '587';

        $mail->isHTML();

        $mail->setFrom($sender, 'Mailer');
        $mail->addAddress($_POST["receiver"]);
//        $mail->addAttachment ($file_name);
        $mail->isHTML(true);

        $mail->Subject = $_POST["subject"];
        $mail->Body = $_POST["message"];

        $mail->send();
        echo 'Message has been sent';
    } catch (Exception $e) {
        echo 'Message could not be sent. Mailer Error: ', $mail-
>ErrorInfo;
    }

}

?>
DaveStSomeWhere
  • 2,475
  • 2
  • 22
  • 19
  • I used your code but had to change $mail = new \PHPMailer\PHPMailer\PHPMailer(); to $mail = new PHPMailer; because it was throwing an error for line 39. Also, I went into my gmail settings and changed the security setting to "less secure apps on." This worked well. I uncommented the $mail->addAttachment ($file_name); however it is not sending the file attachment. Thank you for all your help, I really appreciate it. – Chackras Smith EDS Mar 11 '18 at 19:43
  • Okay just wanted to throw out an update. I got the file to send by using this code. $file_to_attach = $_FILES['file']['tmp_name']; $filename=$_FILES['file']['name']; $mail->addAttachment( $file_to_attach , $filename ); I really appreciate the the help thanks again. – Chackras Smith EDS Mar 11 '18 at 20:06