How to send email from localhost using PHP’s mail function?

Asked

Viewed 51,011 times

22

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 into the php.ini and put 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]
  • 1

    What mistake happens my dear ?

  • No error happens. I refresh the page and only the header that I placed appears.

  • Makes var_dump($enviar); before you close PHP and see what happens.

  • Returns false boolean.

  • one of the problems is that gmail smtp does not work on port 25 where the function will no longer be able to send, always returning bool false

  • I did everything according to the @Otto reply, restarted apache at the end. I use wampstack, put the pasata "sendamil" in C, put inside htdocs, always changing the path in the file "php.ini" and nothing...

  • I did several tests and no error appears on the page.

  • When you tried to send with phpmailer, you remembered to activate the extension php_openssl.dll in the php.ini? Required for authenticated SMTP sending (e.g., gmail on port 587).

  • I remembered, but it was actually bleached when I went to see.

  • And that example of gmail that comes with phpmailer in the folder examples, gives you what mistake?

  • @Thomas I managed to send using the example that comes inside phpmailer itself.

  • And Voce needed to change the port on your server for that?

Show 7 more comments

3 answers

27


Well I’ll show you the way I did using gmail, Phpmailer and Wampserver.

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

2º Enable the following extensions in php.ini php_openssl, php_sockets and php_smtp(if you do), in my case you do not. To enable extensions look for them in php.ini and remove the ; front. Extensions are like this in php.ini ;extension=php_openssl.dll, ;extension=php_sockets.dll.

3rd Download Phpmailer on Github, unpack it and take the following classes:

inserir a descrição da imagem aqui

4º 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 resolve it I went in my email and found the following gmail message. inserir a descrição da imagem aqui

i.e., gmail blocked my connection attempt from localhost.

to avoid this mistake security settings of gmail and went on the

inserir a descrição da imagem aqui

I checked the settings and activated as in the image below

inserir a descrição da imagem aqui

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

inserir a descrição da imagem aqui

and now I’ve sent it to another account of mine.

inserir a descrição da imagem aqui

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

OBS:

I’m using Wampserver, I think it works on any other server, just know where the server puts the file httpd of the apache and the php.ini, and enable modules and extensions.

OBS 2:

Phpmailer classes go in your project.

My answer was based on this tutorial.

  • I was able to send using the example that comes inside the phpmailer itself.

  • 1

    Thanks, I picked up some tips from your reply.

  • @I Wanna Know, I’m glad my answer helped you in some way. =)

  • 1

    Meuchapeu, ball show your tutorial. I had to uncomment the line ;extension_dir = "ext" from php.ini to make it work, otherwise I just had to follow q vc wrote =DDD

  • @Gregoryiyama, =P I’m glad you were useful.

8

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

You will need:

  • WAMP Servidor;
  • Sendmail;
  • Stunnel;
  • A Gmail email account (logical).

After WAMP is installed create a folder with the name inside the WAMP main directory "sendmail", for example D: wamp sendmail, if you installed WAMP on the unit "D:".

Place the sendmail files in the new created folder, search 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 file sendmail.ini

Configured Sendmail is time to configure the 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 unit where you installed WAMP.

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

Now comes the tip to work on Windows 8.

  1. Download the 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: 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 the Desktop only then edit it and after edited 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 the proper permissions of your user.

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

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

On the link https://github.com/mjpsolucoes/confSendmailStunnelWin8 i provided my configuration files [php.ini (partial), sendmail.ini, stunnel.conf] for you to analyze!

Good luck!!!

  • 1

    I was able to send using the example that comes inside the phpmailer itself.

  • Blz! Now I’ve seen your edition...

8

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

1 - Download sendmail.zip file from 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, go to the menu itself), and set it 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 as 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 changes from PHP.ini.

5 - Now open the SENDMAIL.ini file and edit the following fields: smtp_server: the address of the SMTP server; smtp_port: the SMTP server port; default_domain: the domain of your email; pop3_server: the address of the POP3 server; pop3_username: your email; pop3_password: your password. Get this information from 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".

  • I had downloaded sendmail. I attended a class that taught exactly that, but didn’t know exactly where to get the domain information and tals. I’ll look now and answer.

  • I’m just having a question: the email domain is my own email?

  • I tried to put my email and only "gmail.com", but it didn’t work.

  • remembering that gmail the port is 587 only with active SSL .... still advisable to send using a library type phpmailer or swiftmailer

  • to enable SSL uncomment this line "ssl = alway use SSL"?

  • gives a read on this link http://yogeshchaugule.com/blog/2013/configure-sendmail-wamp has information exactly about gmail

  • I was reading, but the guy asks to download the stunnel, the download page has several links, I chose a... It says to change the config file, but it does not specify what. I will continue searching to see if I can get something more understandable. Thank you.

  • 1

    I was able to send using the example that comes inside the phpmailer itself.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.