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

20

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 was in 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]

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

19

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 .

    
22.11.2014 / 15:23
8

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

1 - Download the sendmail.zip 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.

[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 require configuration to allow POP3 access. In Gmail, for example, follow the steps in "Enabling POP".

    
18.11.2014 / 11:15
7

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:

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

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
    
    [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.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