PHP - Sending form by e-mail

Asked

Viewed 868 times

0

I wanted to know how to do in php for an html form to be sent to me by email, I searched tutorials on youtube, but they all only work with a host, I tried to emulate a server and could not run the upload action, I would like to know how to work, even using localhost, for example

3 answers

2


Well I’ll show you the way I did using gmail, Phpmailer and Wampserver.

1st enable the ssl_module in apache. To Enable open the file httpd.conf apache and look for the following line in the file #LoadModule ssl_module modules/mod_ssl.so, remove the sign # to enable.

2º Enable the following extensions in php.ini php_openssl, php_sockets and php_smtp(if you do), in my case you do not. To enable extensions look for them in php.ini and remove the ; front. Extensions are like this in php.ini ;extension=php_openssl.dll, ;extension=php_sockets.dll.

3rd Download Phpmailer on Github, unpack it and take the following classes:

inserir a descrição da imagem aqui

4º Encode.

require_once('class.phpmailer.php'); //chama a classe de onde você a colocou.

$mail = new PHPMailer(); // instancia a classe PHPMailer

$mail->IsSMTP();

//configuração do gmail
$mail->Port = '465'; //porta usada pelo gmail.
$mail->Host = 'smtp.gmail.com'; 
$mail->IsHTML(true); 
$mail->Mailer = 'smtp'; 
$mail->SMTPSecure = 'ssl';

//configuração do usuário do gmail
$mail->SMTPAuth = true; 
$mail->Username = '[email protected]'; // usuario gmail.   
$mail->Password = 'suasenhadogmail'; // senha do email.

$mail->SingleTo = true; 

// configuração do email a ver enviado.
$mail->From = "Mensagem de email, pode vim por uma variavel."; 
$mail->FromName = "Nome do remetente."; 

$mail->addAddress("[email protected]"); // email do destinatario.

$mail->Subject = "Aqui vai o assunto do email, pode vim atraves de variavel."; 
$mail->Body = "Aqui vai a mensagem, que tambem pode vim por variavel.";

if(!$mail->Send())
    echo "Erro ao enviar Email:" . $mail->ErrorInfo;

The first time I ran the above code returned me the following error: SMTP Error: Could not authenticate.

To resolve it I went in my email and found the following gmail message. inserir a descrição da imagem aqui

i.e., gmail blocked my connection attempt from localhost.

to avoid this mistake security settings of gmail and went on the

inserir a descrição da imagem aqui

I checked the settings and activated as in the image below

inserir a descrição da imagem aqui

and tried to resend the email from localhost again, sent it to myself.

inserir a descrição da imagem aqui

and now I’ve sent it to another account of mine.

inserir a descrição da imagem aqui

That was the way I did to send email through localhost.

OBS:

I’m using Wampserver, I think it works on any other server, just know where the server puts the file httpd of the apache and the php.ini, and enable modules and extensions.

OBS 2:

Phpmailer classes go in your project.

My answer was based on this tutorial.

  • worked, thanks! D

  • good, for nothing

0

0

You can even send using localhost, however, this email will hardly be received by any provider due to anti-spam filters and SPF and DKIM settings.

The basic function for sending emails in PHP is this:

mail ( "[email protected]", "assunto","corpo do email","From: [email protected]" );

The most secure and complete lib for sending PHP emails is Phpmailer.

You can download Phpmailer from Github or else for this direct link.

Follow the example source code. You need to change in it the SMTP address, login, password, etc. according to your hosting provider.

<?php
 
// Inclui o arquivo class.phpmailer.php localizado na mesma pasta do arquivo php
include "PHPMailer-master/PHPMailerAutoload.php";
 
// Inicia a classe PHPMailer
$mail = new PHPMailer();
 
// Método de envio
$mail->IsSMTP();
$mail->Host = "localhost"; 
$mail->Port = 25; 
 
$mail->SMTPAuth = true; 
$mail->Username = '[email protected]'; 
$mail->Password = 'senha-do-email'; 
 
$mail->SMTPOptions = array(
 'ssl' => array(
 'verify_peer' => false,
 'verify_peer_name' => false,
 'allow_self_signed' => true
 )
);

// $mail->SMTPDebug = 2; 
 
// Define o remetente
$mail->From = "[email protected]"; 
$mail->FromName = "Francisco"; 
 
// Define o(s) destinatário(s)
$mail->AddAddress('[email protected]', 'Maria');
//$mail->AddAddress('[email protected]');
 
        
$mail->IsHTML(true);
 
$mail->CharSet = 'UTF-8';
 
$mail->Subject = "Assunto da mensagem"; 
 
$mail->Body = 'Corpo do email em html.<br><br><font color=blue>Teste de cores</font><br><br><img src="http://meusitemodelo.com/imagem.jpg">';
 
     
// Envia o e-mail
$enviado = $mail->Send();
 
 
// Exibe uma mensagem de resultado
if ($enviado) {
     echo "Seu email foi enviado com sucesso!";
} else {
     echo "Houve um erro enviando o email: ".$mail->ErrorInfo;
}
 
?>

You can see more detailed information about each parameter in this article: https://www.homehost.com.br/blog/enviar-email-php-com-phpmailer-smtp/

Browser other questions tagged

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