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 function for checking IMEI

Luhn algorithm for IMEI Check public function __checkIMEI($imei){ if(strlen($imei)==15){ $imeia=($imei[1]*2); if(strlen($imeia)==2){$imeia=str_split($imeia,1); $imeia=$imeia[0]+$imeia[1]; } $imeib=($imei[3]*2); if(strlen($imeib)==2){$imeib=str_split($imeib,1); $imeib=$imeib[0]+$imeib[1]; } $imeic=($imei[5]*2); if(strlen($imeic)==2){$imeic=str_split($imeic,1); $imeic=$imeic[0]+$imeic[1]; } $imeid=($imei[7]*2); if(strlen($imeid)==2){$imeid=str_split($imeid,1); $imeid=$imeid[0]+$imeid[1];} $imeie=($imei[9]*2); if(strlen($imeie)==2){$imeie=str_split($imeie,1); $imeie=$imeie[0]+$imeie[1]; } $imeif=($imei[11]*2); if(strlen($imeif)==2){$imeif=str_split($imeif,1); $imeif=$imeif[0]+$imeif[1]; } $imeig=($imei[13]*2); if(strlen($imeig)==2){$imeig=str_split($imeig,1); $imeig=$imeig[0]+$imeig[1]; } $IMEI= ($ime...

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 $capaci...

Magic Function in PHP (__sleep() and __wakeup() )

There are many magic methods in PHP like  __construct(), __destruct(), __callback(), __get(), __set(), __sleep(), __wake() and many more. But we will be takingon  on  __sleep() and  __wake(). __sleep() : serialize() checks if your class has a function with the magic name __sleep(). If so, that function is executed prior to any serialization. It can clean up the object and is supposed to return an array with the names of all variables of that object that should be serialized. If the method doesn't return anything then NULL is serialized and E_NOTICE is issued. serialize() is used for the representation of the storage class for storing the value. Serializing   an object means converting it to a byte stream representation that can be stored in a file. The use of __sleep()  to commit the pending task. If a bulk data is being inserted then at that time __sleep can be used. it will not release the object unless the work is not completed....