How to send email from localhost using PHP mail function?

I want to send emails from localhost .

<html>
<head>
<meta charset="utf-8">
</head>

<body>

<h1> Olá mundo!</h1>

<?php

$enviar = mail( "[email protected]", "nada não", "teste somente");

?>

</body>
</html>

I went on php.ini and put it like this:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.gmail.com
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = [email protected]
Author: Woss, 2014-11-18

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:

insert the description of the image here

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. insert the description of the image here

I.e. gmail blocked my connection attempt from localhost.

To avoid this error I went to settings security from gmail and went in part

insert the description of the image here

I went to the settings and enabled as in the image below

insert the description of the image here

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

insert the description of the image here

And now I sent to another account of mine.

insert the description of the image here

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 .

 26
Author: MeuChapeu, 2014-11-22 14:23:02

The steps were taken and adapted from the original comment in PHP.net:

1-Download the sendmail file.zip of http://glob.com.au/sendmail / . if you use XAMPP, it is already included (skip Step 2).

2-unzip to some Windows folder, but preferably with short names (such as C:\sendmail).

3-Edit your PHP.ini (in WAMP/XAMPP, access from the own menu), and configure as below (note that we will use settings of the Sendmail us Unix): [mail function] ; For Win32 only. ; SMTP =

; for Win32 only. ; sendmail_from =

; for Unix only. You may supply arguments as well (default: "sendmail-t-i"). sendmail_path = ' C:\sendmail\sendmail.exe-t '

; Force the addition of the specified parameters to be passed as extra parameters ; to the sendmail binary. These parameters will always replace the value of ; the 5th parameter to mail (), even in safe mode. ;email.force_extra_paramaters =

4-Save your PHP changes.ini.

5-now open the SENDMAIL file.ini and edit the following fields: smtp_server: the address of the SMTP server; smtp_port: the SMTP server port; default_domain: your email domain; pop3_server: the address of the POP3 server; pop3_username: your email; pop3_password: your password. Get this information with your email provider. In the case of Gmail, take the information from the following Help page: Gmail: Configuring Outlook Express.

[sendmail] 

; you must change mail.mydomain.com to your smtp server, 
; or to IIS's "pickup" directory.  (generally C:\Inetpub\mailroot\Pickup) 
; emails delivered via IIS's pickup directory cause sendmail to 
; run quicker, but you won't get error messages back to the calling 
; application. 

smtp_server=smtp.yourdomain.com 

; smtp port (normally 25) 
; ATENÇÃO: PARA SSL É 465 
; ATENÇÃO: PARA GMAIL É 587 (E APENAS COM SSL)! 

smtp_port=25 

; the default domain for this server will be read from the registry 
; this will be appended to email addresses when one isn't provided 
; if you want to override the value in the registry, uncomment and modify 

default_domain=yourdomain.com 

; log smtp errors to error.log (defaults to same directory as sendmail.exe) 
; uncomment to enable logging 
; error_logfile=sendmail_error.log 

; create debug log as debug.log (defaults to same directory as sendmail.exe) 
; uncomment to enable debugging 
; debug_logfile=sendmail_debug.log 

; if your smtp server requires authentication, modify the following two lines 

;auth_username= 
;auth_password= 

; if your smtp server uses pop3 before smtp authentication, modify the 
; following three lines 

pop3_server=pop.yourdomain.com 
[email protected] 
pop3_password=mysecretpassword 

; to force the sender to always be the following email address, uncomment and 
; populate with a valid email address.  this will only affect the "MAIL FROM" 
; command, it won't modify the "From: " header of the message content 

[email protected] 

; sendmail will use your hostname and your default_domain in the ehlo/helo 
; smtp greeting.  you can manually set the ehlo/helo name if required 

hostname=

6-restart Apache

7 - some email services need configuration to allow POP3 access. In Gmail, for example, perform the steps described in "enabling POP".

 8
Author: Otto, 2014-11-18 10:15:06

In Windows 8 you will need Stunnel to make Sendmail work properly with Gmail once it has removed support for v2 as it is used by sendmail, follow the step-by-step below and I believe you will succeed:

You will need:

  • Wamp Server;
  • Sendmail;
  • Stunnel;
  • a Gmail email account (logical).

After installing the Wamp create inside the Wamp main directory a folder named " sendmail ", for example D:\wamp\sendmail, if you have installed WAMP on the Drive "D:".

Put the sendmail files in the New created folder, look for the configuration file sendmail.ini and open it to edit as below.

Change the following values:

smtp_server=localhost
smtp_port=25
smtp_ssl=none
[email protected]
auth_password=sua_senha

Save and close the sendmail file.ini

Configured or Sendmail it's time to set up php.ini by changing the following values:

smtp_port = 465
sendmail_path = "D:\wamp\sendmail\sendmail.exe -t"

Remembering that the way "D:\wamp\sendmail\sendmail.exe " can be different depending on the drive where you installed WAMP.

Save and close the file php.ini and restart the WAMP services.

Now comes the tip to work on Windows 8.

  1. download Stunnel ( https://www.stunnel.org/downloads.html ) and install on your computer;
  2. in the Stunnel folder look for the file stunnel.conf, should be in C:\Arquivos of programs \ stunnel (64bits) or C:\Program Files (x86)\stunnel (32bits);

Tip: to edit this file you will need special permissions, so you will need to copy it to the desktop to only then edit it and after editing overwrite it in the file folder. source.

After copying the file open it and edit the following settings:

cert = stunnel.pem
socket = l:TCP_NODELAY=1
socket = r:TCP_NODELAY=1
key = stunnel.pem

[ssmtp]
accept  = 465
connect = 25

[gmail-smtp]
client = yes
accept = 127.0.0.1:25
connect = smtp.gmail.com:465

// Para verificar os registros, você pode habilitar a opção de debug
// que fica no início do arquivo
debug = 7
output = stunnel.log

Tip: if you enable the debug option you will need to create the stunnel file.log if it does not exist and give the proper permissions of your user.

Save, close the file stunnel.conf and execute a " Stunnel.exe '.

Run your PHP email sending script and make sure it worked!

No link https://github.com/mjpsolucoes/confSendmailStunnelWin8 I made my configuration files available [php.ini (partial), sendmail.ini, stunnel.conf] for you to analyze!

Good luck!!!

 7
Author: Aluisio Martins Junior, 2014-11-22 22:04:27