How to send e-mail from localhost using the PHP mail function?


I want to send emails from localhost.

<meta charset="utf-8">


<h1> Olá mundo!</h1>


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



I was in php.ini and put it like this:

[mail function]
; For Win32 only.
smtp_port = 25

; For Win32 only.
;sendmail_from = [email protected]

Personal, I was able to send using the example that comes within the phpmailer itself.

asked by anonymous 18.11.2014 / 09:43

3 answers


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/ 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:


require_once('class.phpmailer.php');//chamaaclassedeondevocêacolocou.$mail=newPHPMailer();//instanciaaclassePHPMailer$mail->IsSMTP();//configuraçãodogmail$mail->Port='465';//portausadapelogmail.$mail->Host='';$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.";

    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.


Toavoidthiserror,goto security settings from gmail and went to the


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


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


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 .

22.11.2014 / 15:23

The steps were taken and adapted from the original comment in

1 - Download the file from link . 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, go through its own menu), and configure as below (note that we will use Sendmail settings in 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 the extra parameters ; to the sendmail binary. These parameters will always replace the value of ; the 5th parameter to mail (), even in safe mode. ; mail.force_extra_paramaters =

4 - Save your PHP.ini changes.

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


; you must change 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 port (normally 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 

; 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 


; if your smtp server uses pop3 before smtp authentication, modify the 
; following three lines 
[email protected] 

; 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 


6 - Restart Apache

7 - Some email services require configuration to allow POP3 access. In Gmail, for example, follow the steps in "Enabling POP".

18.11.2014 / 11:15

In Windows 8 you will need Stunnel to make Sendmail work correctly with Gmail as it has removed support for v2 as 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 (logical) email account.

After installing WAMP, create a folder with the name " sendmail ", for example D: \ wamp \ sendmail / strong>, if you installed WAMP in the drive " D: ".

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

Change the following values:

[email protected]

Save and close the sendmail.ini

Configuring Sendmail is time to configure php.ini by changing the following values:

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

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

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

Now comes the hint to work on Windows 8.

  • Download Stunnel ( link ) and install it on your computer;
  • In the Stunnel folder look for the stunnel.conf file, it should be in C: \ Program Files \ 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 Desktop to only edit it and then edit it overwrite it in the source folder.

    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
    accept  = 465
    connect = 25
    client = yes
    accept =
    connect =
    // 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.log file if it does not exist and give your user permissions.

    Save, close the stunnel.conf file and run " Stunnel.exe ".

    Run your PHP mailing script and make sure it works!


    In the link link I have made my configuration files available (php.ini (partial), sendmail.ini, stunnel.conf ] for you to review!

    Good luck !!!

    22.11.2014 / 23:04