Send email using Phpmailer

Asked

Viewed 529 times

0

Thank you for your attention! Well I don’t have much familiarity with programming just with design. I tried to use the Phpmailer class to create a simple contact form on my site, I looked here for some tips but I was not very successful, because everything I saw was a little early and I could not learn the basics...

I even installed the Composer and install the phpmailer for it but I locked there and I could not follow alone, I ask for help here if someone points out to me what I am doing wrong and how to fix would thank a lot.

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 took it exactly as it was in the documentation and I just filled in my data) <? php // Import Phpmailer classes into the global namespace

    $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

  • The attachments are part of the email, if this file does not exist it will not give correct preventing the email.

  • Have you checked the site yourself? It has several similar answers, it may help you: this one: https://answall.com/questions/153820/phpmailer-erro-ao-enviar , and this one for example: https://answall.com/questions/229703/envio-de-anexo-com-phpmailer

  • Ricardo I researched yes, but as I have no notion of programming, any term in the code that is different from my already find some difficulty to understand...

2 answers

3

If you do not need to attach files to the 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, fix the paths, the error you have is caused by these files not existing.

3

Motive:

Translated error: "Unable to access file" file.tar.gz in /var/tmp/, i.e., it did not find the file and/or the folder.


Resolution:

As you probably picked up from an example, these lines show how to "attach" files (more common, used for a "digital signature"), so as it does not have these files to be attached, comment or delete the example rows of attachments:

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

Official documentation Phpmailer

  • Really, I didn’t realize this was pure lack of attention my, I looked at these lines several times I did not care that they could be a Feature that I would not use in my form.

Browser other questions tagged

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