PHP - Send form by email [duplicate]

0

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

    
asked by anonymous 24.06.2016 / 03:01

3 answers

2

Well I'll show you the way I did using gmail, PHPMailer and WampServer.

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

2º 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 ; from the front. The extensions are thus in php.ini ;extension=php_openssl.dll , ;extension=php_sockets.dll .

3rd Download PHPMailer in the GitHub , unzip it and take the following classes:

4ºCode.

require_once('class.phpmailer.php');//chamaaclassedeondevocêacolocou.$mail=newPHPMailer();//instanciaaclassePHPMailer$mail->IsSMTP();//configuraçãodogmail$mail->Port='465';//portausadapelogmail.$mail->Host='smtp.gmail.com';$mail->IsHTML(true);$mail->Mailer='smtp';$mail->SMTPSecure='ssl';//configuraçãodousuáriodogmail$mail->SMTPAuth=true;$mail->Username='[email protected]';//usuariogmail.$mail->Password='suasenhadogmail';//senhadoemail.$mail->SingleTo=true;//configuraçãodoemailaverenviado.$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 rounded the code above I returned the following error: SMTP Error: Could not authenticate .

To solve it I went in my email and found the following message from gmail.

Inotherwords,gmailblockedmyconnectionattemptfromlocalhost.

Toavoidthiserror,goto security settings from gmail and went to the

Iaccessedthesettingsandactivatedasintheimagebelow

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

AndnowI'vesentittoanotheraccountofmine.

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

OBS:

I'm using WampServer, I believe it works on any other server, it's just knowing where the server puts the file httpd apache and php.ini , and enable modules and extensions.

OBS 2:

PHPMailer classes go in your project.

My response was based on this tutorial .

    
24.06.2016 / 03:28
0

Speak Murilo!

Take a look at this stackoverflow post even though it seems similar to your problem, right?

How to send localhost email using the PHP mail function?

Embrace

    
24.06.2016 / 03:05
0

You can even send using localhost, however, this email will hardly be received by any ISP 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 safest and most complete PHP to send email in PHP is PHPMailer.

You can download PHPMailer from Github or by this one direct link .

Follow the example source code. You need to change the SMTP address, login, password, and so on. according to its 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">';//Enviaoe-mail$enviado=$mail->Send();//Exibeumamensagemderesultadoif($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: link

    
24.06.2016 / 04:31