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 Longest Repeated Substrings (LRS)

<?php Class LRS{ /** •@param array $texts •Prints longest repeated substrings for each text */ public static function getAllLRS($texts){ $stringArr = array(); foreach($texts as $string){ $stringArr[] = self::LongestRepeatedSubstring($string); } return $stringArr; } public function LongestRepeatedSubstring($string){ if ($string == null) return null; $string_length = strlen($string); $substrings = array(); for ($i=0; $i < $string_length; $i++){ $substrings[$i] = substr($string, $i); } sort($substrings); $result = ""; for ($i = 0; $i < $string_length - 1; $i++){ $lcs = self::LongestCommonString($substrings[$i], $substrings[$i ...

What Make Facebook page load faster ?

Facebook has a “ Lazy Loading ” system they call “ BigPipe ” that helps the pages load fast. Facebook breaks each page down into sections they call “ Pagelets” , and using Javascript only load the most important Pagelets first then load the less important ones shortly afterward. And pipeline them through several execution stages inside web servers and browsers, as the modern microprocessor do to serve the various request in an order to increase the productivity. Big Pipe is totally implemented in PHP and javascript. By loading and rendering the main page structure first with minimal info on it, the page appears to load quicker than having to wait for a complete page to download and then render. To exploit the parallelism between web server and browser, BigPipe first breaks web pages into multiple chunks called pagelets. Just as a  pipelining microprocessor  divides an instruction’s life cycle into multiple stages (such as “instruction fetch”, “instruction decode”, ...

Write a function that checks if a given word stored in a doubly linked list is a palindrome.

<?php class Node { public $value; public $next = null; // next node public $prev = null; // previous node public function __construct($value) { $this->value = $value; } } class Palindrome { /** •@param string $word •@return bool */ public static function isPalindrome($head, $tail){ if ($head == null) return true; while ($head != $tail){ if ($head->value != $tail->value) return false; $head = $head->next; $tail = $tail->prev; } return true; } } $head = new Node(1); $firstNode = new Node(2); $secondNode = new Node(3); $tail = new Node(4); $head->next = $firstNode; $firstNode->prev = $head; $firstNode->next = $secondNode;...