Failed to read a file. php (lib Phpmailer)

Asked

Viewed 105 times

-1

Hello! I have the following form:

<form role="form" method="post" action="send_email.php" id="formContato">
            <div class="left">
                <fieldset class="mail"><input placeholder="Endereço e-mail" type="text" name="email" id="email"></fieldset>
                <fieldset class="name"><input placeholder="Primeiro nome" type="text" name="primeiro_nome" id="primeiro_nome"></fieldset>
                <fieldset class="name"><input placeholder="Segundo nome" type="text" name="ultimo_nome" id="ultimo_nome"></fieldset>
                <fieldset class="name"><input placeholder="Telefone" type="text" name="telefone" id="telefone"></fieldset>
                <fieldset class="subject">
                    <select name="assunto" id="assunto">
                  aias,      <option>Dúvida</option>
                        <option>Reclamações</option>
                        <option>Outros</option>
                    </select>
                </fieldset>
            </div>
            <div class="right">
                <fieldset class="question">
                    <textarea  name="mensagem" id="mensagem" placeholder="Digite sua mensagem..."></textarea></fieldset>
            </div>
            <div class="btn-holder">
                <button class="btn blue" type="submit" value="enviar" name="enviar" id="enviar">Enviar</button>
            </div>
</form>

and the send_email.php file:

<?php

require 'PHPMailerAutoload.php';
require 'vendor/autoload.php';
require 'PHPMailer';

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

// Fetching data that is entered by the user
    $email = $_POST['email'];
    $primeiro_nome = $_POST['primeiro_nome'];
    $ultimo_nome = $_POST['ultimo_nome'];
    $mensagem = $_POST['mensagem'];
    $assunto = $_POST['assunto'];
    $telefone = $_PPOST['telefone'];
    $to_id = '[email protected]';

// Configuring SMTP server settings
    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;
    $mail->SMTPSecure = 'tls';
    $mail->SMTPAuth = true;
    $mail->Username = $to_id;
    $mail->Password = 'senha';

// Email Sending Details
    $mail->addAddress($to_id);
    $mail->Subject = $assunto;
    $mail->msgHTML($email . "\r\n" .$mensagem);

// Success or Failure
    if (!$mail->send()) {
        $error = "Mailer Error: " . $mail->ErrorInfo;
        echo '<p id="para">'.$error.'</p>';
    }
    else {
        echo '<p id="para">Message sent!</p>';
    }
}
else{
        echo '<p id="para">Please enter valid data</p>';
    }
?>

The problem is that when the form is completed and the fields are submitted via post to the action send_email.php, nothing happens in this URL. in case, the URL would be: localhost/site/index.html, where the form is filled and then, when submitted, goes to localhost/site/send_email.php (however, this page is empty). As I am developing this app in sublime text, I ended up not being able to debug the logical part well. (I tried to implement the package Xdebug in sublime but I had difficulties in use). I’ve already deleted all that code from send_email.php, leaving only the phpinfo() to test whether the php is working (and is!).

Funny thing is, even if I $email = $_POST['email']; shortly after the <?php and then print a echo $email, yet nothing appears

1 answer

0

In view of your script is correct.

To debug the PHPMailer, add:

$mail->SMTPDebug = 2;

or

$mail->SMTPDebug = SMTP::DEBUG_SERVER;

Options:

  • SMTP :: DEBUG_OFF (0): Disables debugging (you can also leave this completely, 0 is the standard).
  • SMTP :: DEBUG_CLIENT (1): Output messages sent by the customer.
  • SMTP :: DEBUG_SERVER (2): as 1, more replies received from server (this is the most useful configuration).
  • SMTP :: DEBUG_CONNECTION (3): as 2, more information on the initial connection - this level can help diagnose faults in the STARTTLS.
  • SMTP :: DEBUG_LOWLEVEL (4): as 3, more level information more low, very detailed, do not use for SMTP debugging, only low-level problems.

Documentation

I just discovered that there is something wrong with require. - Mikhael Araujo

You need to hit where to get the PHPMailerAutoload.php in his send_email.php

Examples:

For Phpmailer (5.2-stable) (Link):

(rename the folder to Phpmailer)

In the same directory:

require_once 'PHPMailer/PHPMailerAutoload.php';

Previous directory:

require_once '../PHPMailer/PHPMailerAutoload.php';

If in the latest version (6.0.3):

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
  • I just found out there’s something wrong with require.

  • Try it like this require_once 'PHPMailer/PHPMailerAutoload.php';... Probably the folder path is incorrect. If you have the folder PHPMailer in the same directory as send_email.php then that’s how it is. If not, post how your structure is.

  • require_once 'vendor/Composer/autoload_real.php'; require_once 'vendor/autoload.php'; require_once 'vendor/phpmailer/phpmailer/src/Phpmailer.php'; in the same folder as the send_email.php file, there is a vendor folder with this directory..

  • I still can’t instantiate a Phpmailer class object.

  • You need to get to PHPMailerAutoload.php... So you have to go into the folders... ex.: require_once 'vendor/PHPMailer/PHPMailerAutoload.php';.. the default Phpmailer folder, already has the PHPMailerAutoload.php at the root. If yours is different, look for the autoload.php, and make your way to him.

  • I added it in the current version (6.0.3). https://github.com/PHPMailer/PHPMailer

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.