Enviar Email via PHP

Asked

Viewed 876 times

-3

Hello, I’m trying to use this code:

    <?php
    $nome = $_POST['nome'];
    $arquivo = $_FILES["arquivo"];
    $assunto = $_POST['assunto'];
    // Para quem vai ser enviado o email
    $para = $_POST['email'];
    $boundary = "XYZ-".date("dmYis")."-ZYX";
    $fp = fopen($arquivo["tmp_name"], "rb"); // abre o arquivo enviado
    $anexo = fread($fp, filesize($arquivo["tmp_name"])); // calcula o tamanho
    $anexo = base64_encode($anexo); // codifica o anexo em base 64
    fclose($fp); // fecha o arquivo
    // cabeçalho do email
    $headers  = "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/mixed; ";
    $headers .= "boundary='$boundary'\r\n";
    $headers .= "$boundary\n";
    // email
    $mensagem  = "--$boundary\n";
    $mensagem .= "Content-Type: text/html; charset='utf-8'\n";
    $mensagem .= "<strong>Nome: </strong> $nome \r\n";
    $mensagem .= "--$boundary \n";
    // anexo
    $mensagem .= "Content-Transfer-Encoding: base64 \n"; 
    $mensagem .= "Content-Disposition: attachment; filename=\"".$arquivo['name']."\" \r\n"; 
    $mensagem .= "$anexo \n"; 
    $mensagem .= "--$boundary \n"; 
    // enviar o email 
    mail($para, $assunto, $mensagem, $headers); 
    ?>

And I’m having the following mistake:

"Warning: mail(): Failed to connect to mailserver at "localhost" port 25, Verify your "SMTP" and "smtp_port" Setting in php.ini or use ini_set() in C: wamp www email.php on line 28"

Someone could help me, thank you.

  • See the Phpmailer library, it is easy to understand. https://github.com/PHPMailer/PHPMailer

1 answer

0

See the Phpmailer library, it is easy to understand, just follow the installation steps via Poser. Phpmailer -> https://github.com/PHPMailer/PHPMailer

// Exemplo Gmail
    <?php

    require_once("vendor/autoload.php");

    //Create a new PHPMailer instance
    $mail = new PHPMailer;


    //Tell PHPMailer to use SMTP

    //Enable SMTP debugging
    // 0 = off (for production use)
    // 1 = client messages
    // 2 = client and server messages
    $mail->SMTPDebug = 2;

    //Ask for HTML-friendly debug output
    $mail->Debugoutput = 'html';

    //Set the hostname of the mail server
    $mail->Host = 'smtp.gmail.com';
    // use
    // $mail->Host = gethostbyname('smtp.gmail.com');
    // if your network does not support SMTP over IPv6

    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
    $mail->Port = 587;

    //Set the encryption system to use - ssl (deprecated) or tls
    $mail->SMTPSecure = 'tls';

    //Whether to use SMTP authentication
    $mail->SMTPAuth = true;

    //Username to use for SMTP authentication - use full email address for gmail
    $mail->Username = "";

    //Password to use for SMTP authentication
    $mail->Password = "";

    //Set who the message is to be sent from
    $mail->SetFrom('', 'Curso PHP 7');

    //Set an alternative reply-to address
    $mail->addReplyTo('', 'First Last');

    //Set who the message is to be sent to
    $mail->addAddress('suporte@ezzxzr', 'Suporte oi');

    //Set the subject line
    $mail->Subject = 'Testando a classe PHPMailer com Gmail';

    //Read an HTML message body from an external file, convert referenced images to embedded,
    //convert HTML into a basic plain-text alternative body
    $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));

    //Replace the plain text body with one created manually
    $mail->AltBody = 'This is a plain-text message body';

    //Attach an image file
    // $mail->addAttachment('images/phpmailer_mini.png');

    //send the message, check for errors
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }

    ?>

Links that can help: https://www.devmedia.com.br/php-envio-de-e-mail-autenticado-utilizando-o-phpmailer/38380 https://www.secnet.com.br/blog/emails-pelo-phpmailer https://www.youtube.com/watch?v=uL87-eU3Mp0

Browser other questions tagged

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