PHP – Send mail from localhost using SMTP

When you setup apache in local system it won't by default setup SMTP server. Hence, if you want to send emails from local system, you will have to either set up SMTP server on your local or use external SMTP server. There are many service providers available who can give you SMTP, username and password to send email using their SMTP server. But it isn't very easy and straight forward to use SMTP in PHP. While searching I found PHPMailer script doing that very nicely and efficiently. You can download PHPMailer from their official site and read further to get more instructions on how to use gmail (or any other) SMTP.

I have removed some of the files from PHPMailer package and here is the zip file containing minimum files you will require to do the testing. I have removed other files just to eliminate any confusion and make it simpler. You can find "smtp.php" file in "examples” folder. Open it and make sure that proper values are given for required variables.

Now, there is a question from where I can find SMTP servers for free. Well, if you are using gmail then you got lucky. Gmail lets us use their SMTP provided that you will have to authenticate it using your own username and password. To use gmail SMTP the values will be like below:

//Set the hostname of the mail server
$mail->Host = "smtp.gmail.com";

//enable this if you are using gmail smtp, for mandrill app it is not required
//$mail->SMTPSecure = 'tls';

//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 587;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "YOUR.ID@GMAIL.COM";
//Password to use for SMTP authentication
$mail->Password = "YOUR_PASSWORD";
?>

Or, if you don't want to use your gmail account then I would suggest to create one account on https://mandrill.com and get your SMTP host, username and password from there. I have tested both gmail and mandrill and they are working pretty good.

//Set the hostname of the mail server
$mail->Host = "smtp.mandrillapp.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 587;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "YOUR_REGISTER_EMAIL@MANDRILL.COM";
//Password to use for SMTP authentication
$mail->Password = "YOUR_PASSWORD";
?>

So, in case if you want to send emails from localhost or from server where you have restrictions on SMTP this method will be helpful.

Checkout my blog about how to send emails from local WordPress setup using SMTP.

Comments

i am using xamp, can i send the data using this same port number, because my port number is 25

how can i sort it out...my configuration is same as given above and i replaced my gmail id and pass....plzzz help me..

SERVER -> CLIENT: 220 smtp.gmail.com ESMTP bn1sm9868933pad.17 - gsmtp
CLIENT -> SERVER: EHLO localhost
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [39.57.119.1]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250 SMTPUTF8
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
CLIENT -> SERVER: QUIT
SERVER -> CLIENT:
SMTP ERROR: QUIT command failed:
SMTP connect() failed.
Mailer Error: SMTP connect() failed.

Getting below error while sending mail through gmail smtp server.
Please help

SERVER -> CLIENT: 220 smtp.gmail.com ESMTP tn5sm18396956pac.32 - gsmtp
CLIENT -> SERVER: EHLO localhost
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [103.48.59.32]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 2.0.0 Ready to start TLS

Warning: stream_socket_enable_crypto(): this stream does not support SSL/crypto in C:\xampp\htdocs\POC\phpmailer\only_req_files\class.smtp.php on line 299
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: F
SMTP ERROR: QUIT command failed: F
SMTP connect() failed.
Mailer Error: SMTP connect() failed.

Hello Rahul,
You can try the solution given at http://stackoverflow.com/questions/23326934/phpmailer-smtp-connect-failed . It should fix your issue. Let me know if you still face the issue after applying suggested changes.

Add new comment