Skip to main content

Code for Mail in PHP

PHP mail function and mail configuration in XAMPP and sending mail is done from sendmail through localhost. I hope it will help you.
  1. mail() function
  1. <?php
  2. $to = 'dubeynitish22@hotmail.com';
  3. $subject = 'Test';
  4. $message = 'Hello';
  5. $headers = 'From: webmaster@example.com' . "\r\n" .
  6. 'Reply-To: webmaster@example.com' . "\r\n" .
  7. 'X-Mailer: PHP/' . phpversion();
  8.  
  9. if(!mail($to, $subject, $message, $headers)){
  10. echo "Error !!";
  11. }else{
  12. echo "Email Sent !!";
  13. }
  14. ?>
2. php.ini configuration (For SEND-MAIL)
  1. [mail function]
  2. ; For Win32 only.
  3. ; http://php.net/smtp
  4. ;SMTP=localhost
  5. ; http://php.net/smtp-port
  6. ;smtp_port=25
  7.  
  8. ; For Win32 only.
  9. ; http://php.net/sendmail-from
  10. ;sendmail_from = me@example.com
  11.  
  12. ; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
  13. ; http://php.net/sendmail-path
  14. sendmail_path ="C:\xampp\sendmail\sendmail.exe -t"
3. mail.ini (localhost)


  1. smtp_server=smtp.gmail.com
  2. auth_username=abc@gmail.com
  3. auth_password=abcdefg
  4. hostname=localhost

Comments

  1. I receive an error so what's might be the issue

    ReplyDelete
    Replies
    1. smtp_server=smtp.gmail.com
      smtp_port=465

      Change the port and restart Apache, Try it.

      Delete
  2. it really did help to me. thanks a lot

    ReplyDelete
  3. what should the sendmail.ini file look like?

    Thanks

    ReplyDelete
  4. what is the path for mail.ini path in localhost

    ReplyDelete
    Replies
    1. It is written above , if not working let me know again.

      Delete
  5. I write same to same code in my php.ini ,sendmail.ini and mail.php file but email can't send . why?

    ReplyDelete
    Replies
    1. Hi Imdad,

      Kindly share your code on geekanswer22@gmail.com, I will check and will revert back And try it too...

      smtp_server=smtp.gmail.com
      smtp_port=465

      Change the port and restart Apache, Try it.

      Delete
  6. Tried Same Code..But its not working

    ReplyDelete
  7. it's not working please send me a solution ?

    ReplyDelete

Post a Comment

Popular posts from this blog

PHP code for finding distinct elements common to all rows of a matrix in O(n) time complexity

<?php class Matrix{ /** •@param 2D array $matrix • •Prints distinct elements common to all rows of the matrix */ public static function getDistinctElementsCommonToAllRows($matrix){ // A hash map to store count of elements $hashmap = array(); $selectedHash = array(); $rows = count($matrix); $cols = count($matrix[0]); for ($i = 0; $i < $rows; $i++) { // Increment the count of first // element of the row if(array_key_exists($matrix[$i][0],$hashmap)){ $hashmap[$matrix[$i][0]] = $i+1; } // Starting from the second element // of the current row for ($j = 1; $j < $cols; $j++) { // If current ele

PHP code for Implementing LRU cache.

<?php interface LRUCache{ /** •@param string $key •@param string $value •@return bool $result • •Stores value against the key in the cache */ public function insertIntoCache($key,$value); /** •@param string $key •@return string $value •Gets the value of a key from the cache */ public function getFromCache($key); /** Purge the entire cache */ public function purgeCache(); /** •@return int $count •Gets the number of successful cache hits so far */ public function allCacheHits(); /** •@return int $count •Gets the number of unsuccessful cache hits so far **/ public function allCacheMissed(); } class Cache implements LRUCache{ // int the max number of elements the cache supports private $capacity; // Array representing a naive hashmap (TODO needs to pass t