PHP - Form Submission by email [duplicate]
I wanted to know how I do in php so that an html form is sent to me by email, I looked for tutorials on youtube, but they all only work with a host, I tried to emulate a server and I could not run the send action, I would like to know how I do it to work, even]}
3 answers
Good I'll show you the way I did using gmail, PHPMailer and WampServer.
1st enable or ssl_module
in apache. To enable open Apache file httpd.conf
and look for the following line in the file #LoadModule ssl_module modules/mod_ssl.so
, remove the symbol #
to enable.
2nd enable the following extensions in php.ini php_openssl
, php_sockets
and php_smtp
(if it has), in my case it does not. To enable extensions look for them in php.ini and remove the ;
from the front. Extensions are like this in php.ini ;extension=php_openssl.dll
, ;extension=php_sockets.dll
.
3rd download PHPMailer on GitHub, unzip it and take the following classes:
4th 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 solve it I went in my email and found the following gmail message.
I.e. gmail blocked my connection attempt from localhost.
To avoid this error I went to settings security from gmail and went in part
I went to the settings and enabled as in the image below
And I tried to resend the email from localhost again, I sent it to myself.
And now I sent to another account of mine.
This was the way I did to send email through localhost.
OBS:
I'm using WampServer, I think it works on any other server, it's just knowing where the server puts the Apache httpd
file and the php.ini
, and enabling the modules and extensions.
OBS 2:
PHPMailer classes go into your project.
My answer was based on this tutorial .
This Is Murilo!
Take a look at this post here from stackoverflow even seems similar to your problem, right ?
How to send email from localhost using PHP mail function?
Hug
You can even send using localhost, however, this email will hardly be received by any provider due to 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 to send emails in PHP is PHPMailer.
You can download PHPMailer from Github or from this direct link.
Follows 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 /