How to Send Email from Local Wamp Server Using PHP

How to send email from localhost WAMP Server to send email Gmail Hotmail or so forth?

Configuring a working email client from localhost is quite a chore, I have spent hours of frustration attempting it. At last I have found this way to send mails (using WAMP, XAMPP, etc.):

Install hMailServer

Configure this hMailServer setting:

  1. Open hMailServer Administrator.
  2. Click the "Add domain ..." button to create a new domain.
  3. Under the domain text field, enter your computer's localhost IP.

    • Example: 127.0.0.1 is your localhost IP.
  4. Click the "Save" button.
  5. Now go to Settings > Protocols > SMTP and select the "Delivery of Email" tab.
  6. Find the localhost field enter "localhost".
  7. Click the Save button.

Configure your Gmail account, perform following modification:

  1. Go to Settings > Protocols > SMTP and select "Delivery of Email" tab.
  2. Enter "smtp.gmail.com" in the Remote Host name field.
  3. Enter "465" as the port number.
  4. Check "Server requires authentication".
  5. Enter your Google Mail address in the Username field.
  6. Enter your Google Mail password in the password field.
  7. Check mark "Use SSL"
  8. Save all changes.

Optional

If you want to send email from another computer you need to allow deliveries from External to External accounts by following steps:

  1. Go to Settings > Advanced > IP Ranges and double click on "My Computer" which
    should have IP address of 127.0.0.1
  2. Check the Allow Deliveries from External to External accounts Checkbox.
  3. Save settings using Save button.

Configure WAMP server to send email

Configuring a working email client from localhost is quite a chore, I have spent hours of frustration attempting it. I'm sure someone more experienced may be able to help, or they may perhaps agree with me.

If you just want to test, here is a great tool for testing mail locally, that requires almost no configuration:

http://www.toolheap.com/test-mail-server-tool/

Sending mail via php script using wamp server

You can use PHPMailer or PHPMimeMail to send mail from your localhost to gmail. To send email to gmail, you should send authenticated mail like SMTP mail. You should configure your mail username,password, and your mail host in your mail configurtion.

sample PHPmailer script for Gmail :

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail = new PHPMailer();

$body = file_get_contents('contents.html');
$body = preg_replace('/[\]/','',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "mail@gmail.com"; // GMAIL username
$mail->Password = "12345"; // GMAIL password

$mail->SetFrom('mail@gmail.com', 'Balaji K');

$mail->AddReplyTo("mail2@gmail.com");

$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";

$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);
$mail->Body($body);
$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

How to configure WAMP (localhost) to send email using Gmail?

Gmail servers use SMTP Authentication under SSL or TLS. I think that there is no way to use the mail() function under that circumstances, so you might want to check these alternatives:

  • PEAR::Mail
  • phpMailer
  • Nette\Mail

They all support SMTP auth under SSL.

You'll need to enable the php_openssl extension in your php.ini.

Additional Resources:

  • How to Send Email from a PHP Script Using SMTP Authentication (using PEAR::Mail)
  • Send email using PHP with Gmail (using phpMailer)
  • Mailing using Nette\Mail

unable to send email using php on wamp server

Here is how I got hMailer to work with my WAMPServer's PHP

First setup your hmailer server.

  1. Create a domain, lets call it testmail.net
  2. Create an account that has no password attached to it, lets say nopwd@testmail.net
  3. Create an account that is set up as a Catch-all lets say postmaster@testmail.net. Using this you dont have to create an account for every user you want to send mail to and then login as that user, all mail comes to this account.

Now add your mail server domain name to the HOSTS file

Edit c:\windows\system32\drivers\etc\hosts and add a line like this

127.0.0.1  testmail.net

If you are running the mail server on another PC use the IP of the other PC instead of 127.0.0.1

Change the php.ini file ( use the wampmanager icon menus to make sure you are editing the correct php.ini )

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = mail.testmail.net
; http://php.net/smtp-port
; assuming you are using the standard port number, change if you used something else.
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = nopwd@testmail.net

Now just make sure that you also use the from address when you send mail as the hmailer will want this or it will probably reject the email.

$to = 'xyz@testmail.net';
$subject = 'Customer_Details Report';
$msg="php mail";
$headers = 'From: nopwd@testmail.net' . "\r\n"

$result=mail($to, $subject, $msg, $headers);


Related Topics



Leave a reply



Submit