How to send email using PHP via “sendmail” from localhost (XAMPP)

Xampp has sendmail included in its package. You can download the whole XAMPP package from this link. After You have installed the XAMPP into your windows. Sendmail itself cannot send emails. It needs some mail servers like gmail. Follow following steps :

Step 1:

Go to the XAMPP folder (wherever you have installed it). In my case, it is “C:/Xampp”. Inside that folder there must be a folder called “sendmail” if you have selected it to be installed while installing xampp. Go inside that folder and open the file “sendmail.ini” in a text editor.

sendmail_ini

Step 2:

Find the phrase “smtp_server” on the file. It must be around the line number 14. Set its value to “smtp.gmail.com” without quotes.

sempt_server

Step 3:

Find the phrase “smtp_port”. It must be right below “smtp_server” (if you discount all the comments). Set its value to “465” without quotes. For gmail, the port number is 465.

smtp_port

Step 4:

Find “default_domain” and set its value to “localhost” without quotes.

default_domain

Step 5:

Find “auth_username” and set its value to you gmail address. Find “auth_password” and set its value to your gmail password.

user_pass

Step 6:

Find “hostname” and set its value to “localhost” without quotes.

hostname

Now there’s an additional step

You have do a very slight modification in another file.

Go back to root xampp folder. Go to folder “php”. Open “php.ini” file on a text editor.

Step 7:

Find the phrase “sendmail_path”. It should be around line number 1137 under “mail function” section.

There, you will see a line that says sendmail_path=”C:\\xampp\\mailtodisk\\mailtodisk.exe”. You have to comment that line. Just put “;” at the beginning of the line.

Just above/below that line, you will see another line that says sendmail_path = “\\”C:\\xampp\\sendmail\\sendmail.exe\\” -t”. You will have to make that line active. You can do it by removing “;” from the beginning of that line.

Remember the path I have on my php.ini file and your might be different depending on the location where you installed you xampp. I trust you can handle that.

It should look like this

sendmail_path

Now you can just use the native php mail function to send the mail

mail($to, $subject, $body, $headers);

Here,

$to is the email address you want to send the email to

$subject is the subject of the email

$body is the actual message

$header is the email header like “from : …….”, etc.

Eg :

<?php
    $headers = 'MIME-Version: 1.0\\r\\n';
    $headers .= 'From: somebody@gmail.com' . '\\r\\n' . 'X-Mailer: PHP/' . phpversion();

    if(mail('someone@somewhere.com', 'Demo', 'hey this is demo email message', $headers)){
        echo 'Email sent.';
    } else {
        echo 'Email not sent.';
    }
?>

Congrats you can now send email from your localhost.