Send email using PHPMailer

0

Thank you for your attention! Well I do not have much familiarity with programming with just design. I tried the PHPMailer class to create a simple contact form on my site, I looked for some tips here but I did not have much success, because everything I saw was already a little early and I could not learn the basics ...

I installed the composer and installed phpmailer for it but crashed there and I could not go it alone, I ask for help here if someone points out what I'm doing wrong and how to fix it would thank you very much.

CONTACT.HTML

       <form id="form" action="vendor/sendmail.php" method="post">
        <div class="field">
          <label class="label">Name</label>
          <div class="control">
            <input class="input" type="text" name="nome" placeholder="Name">
          </div>
        </div>

        <div class="field">
          <label class="label">Email</label>
          <div class="control">
            <input class="input" type="email" name="email" placeholder="Email">
          </div>
        </div>

        <div class="field">
          <label class="label">Message</label>
          <div class="control">
            <textarea class="textarea" name="mensagem" placeholder="Message"></textarea>
          </div>
        </div>

        <div class="field is-grouped">
          <div class="control">
            <button class="button is-link">Submit</button>
          </div>
        </div>
      </form>

SENDMAIL.PHP (I got exactly as I was in the documentation and just filled in my data)     

    $nome = $_POST["nome"];
    $email = $_POST["email"];
    $mensagem = $_POST["mensagem"];

    // These must be at the top of your script, not inside a function
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;

    //Load Composer's autoloader
    require 'autoload.php';

    $mail = new PHPMailer(true);                              // Passing 'true' enables exceptions
    try {
        //Server settings
        $mail->SMTPDebug = 2;                                 // Enable verbose debug output
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'mail.eftmkg.com';                      // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = '[email protected]';                  // SMTP username
        $mail->Password = 'xxxxxxxxxxxx';                     // SMTP password
        $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, 'ssl' also accepted
        $mail->Port = 587;                                    // TCP port to connect to

        //Recipients
        $mail->setFrom('[email protected]', 'Mailer');
        $mail->addAddress('[email protected]', 'Kaio Maia');     // Add a recipient
        $mail->addAddress('[email protected]');                       // Name is optional
        $mail->addReplyTo('[email protected]', 'Info');
        //$mail->addCC('[email protected]');
        //$mail->addBCC('[email protected]');

        //Attachments
        $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
        $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

        //Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = 'EFTMKG';
        $mail->Body    = '$mensagem';
        //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

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

When I try to send I get the error message:

Could not access file: /var/tmp/file.tar.gz

Message could not be sent. Mailer Error: Could not access file: /var/tmp/file.tar.gz

link

    
asked by anonymous 03.07.2018 / 10:20

2 answers

3

If you do not need to attach files to email, remove these lines. If you want to keep the code for later use as an example, comment, then use // at the beginning of the line to become a comment.

//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

If you need to attach something to the email, correct the paths, the error you are caused by these files does not exist.

    
03.07.2018 / 12:40
3

Reason:

The translated error: "Could not access the file" file.tar.gz in /var/tmp/ , that is, it could not find the file and / or the folder .

Resolution:

As you probably picked up from an example, these lines show you how to "attach" files (most common, used for a "digital signature"), so as you do not have these files to attach, comment or delete the sample attachment lines :

//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

Official PHPMailer documentation

    
03.07.2018 / 13:34