Sunday, February 23, 2014

How to configure XAMPP to send mail from localhost?

You can send mail from localhost with sendmail package , sendmail package is inbuild in XAMPP. So if you are using XAMPP then you can easily send mail from localhost.
for example you can configure C:\xampp\php\php.ini and c:\xampp\sendmail\sendmail.ini for gmail to send mail.
in C:\xampp\php\php.ini find extension=php_openssl.dll and remove the semicolon from the beginning of that line to make SSL working for gmail for localhost.
in php.ini file find [mail function] and change
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = my-gmail-id@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
Now Open C:\xampp\sendmail\sendmail.ini. Replace all the existing code in sendmail.ini with following code
[sendmail]

smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=my-gmail-id@gmail.com
auth_password=my-gmail-password
force_sender=my-gmail-id@gmail.com
Now you have done!! create php file with mail function and send mail from localhost.
PS: don't forgot to replace my-gmail-id and my-gmail-password in above code. Also, don't forget to remove duplicate keys if you copied settings from above. For example comment following line if there is another sendmail_path : sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe" in the php.ini file

-------------------------------------------------------------------------

You have to configure SMTP on your server
You can use the googles free SMTP server Pretty easy to setup too.
study about php mailer in this link
<?php

$mail = new PHPMailer(true);

//Send mail using gmail
if($send_using_gmail){
    $mail->IsSMTP(); // telling the class to use SMTP
    $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 = "your-gmail-account@gmail.com"; // GMAIL username
    $mail->Password = "your-gmail-password"; // GMAIL password
}

//Typical mail data
$mail->AddAddress($email, $name);
$mail->SetFrom($email_from, $name_from);
$mail->Subject = "My Subject";
$mail->Body = "Mail contents";

try{
    $mail->Send();
    echo "Success!";
} catch(Exception $e){
    //Something went bad
    echo "Fail :(";
}

?>

-----------------------------------------------------------

1) Open the php.ini“. (XAMPP/php/php.ini)

    2) Search for the attribute called SMTP in the php.ini file.Generally you can find the line SMTP=localhost“. change the localhost to the smtp server name of your ISP. And, there is another attribute called smtp_port which should be set to 25.Ive set the following values in my php.ini file.

        SMTP = smtp.wlink.com.np
        smtp_port = 25

    3) Restart the apache server so that PHP modules and attributes will be reloaded.

    4) Now try to send the mail using the mail() function ,

        mail(“you@yourdomain.com”,”test subject”,”test body”);

    you might get the warning like this,

    Warning: mail() [function.mail]: sendmail_from not set in php.ini or custom From:” header missing in C:\Program Files\xampp\htdocs\testmail.php on line 1

    5) Now specify the following headers and try to send the mail again,

        $headers = MIME-Version: 1.0' . “\r\n”;
        $headers .= ‘Content-type: text/html; charset=iso-8859-1' . \r\n”;
        $headers .= From: sender@sender.com . \r\n”;
        mail(“you@yourdomain.com”,”test subject”,”test body”,$headers);

    Well thats all, the mail is sent to you@yourdomain.com from the localhost.

    Note : Some smtp server verifies the email address of the sender so the email address which is in the place of sender@sender.com should be a valid and existing email address otherwise mail might not be sent to the you@yourdomain.com”.

No comments: