Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    15
    Rep Power
    0

    Mail not recieved using mail()


    Hi everyone,
    I am trying to send a mail using the PHP inbuilt mail() function.
    The code is as follows:
    PHP Code:
    <?php
     $to 
    "xyz@gmail.com";
     
    $subject "Test mail";
     
    $message "Hello! This is a simple email message.";
     
    $from "abc@gmail.com";
     
    $headers "From:" $from;
     
    mail($to,$subject,$message,$headers);
     echo 
    "Mail Sent.";
     
    ?>
    It shows the message Mail Sent in the end, but when I check the inbox, no mail is recieved.
    Also, I am using xampp for PHP learning. Please let me know the reason and what I should do?
    Thanks in advance...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,868
    Rep Power
    368
    should you not do if (mail....);

    btw php mail function states that it is not a guarantee that mail has been accepted just that it is sent.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    15
    Rep Power
    0
    Something strange happened..
    I recieved the mail (abc@gmail.com) but the recipient (xyz@gmail.com) still has not got it.
    Any reasons why this happened?
    And rephrasing my question
    "How do i make sure that mail has been accepted after being sent via mail() function?"
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    How do i make sure that mail has been accepted after being sent via mail() function?
    If mail() returns true it means the mail was accepted for delivery. It does not mean it was actually delivered to the recipient. There is no way to know whether or not it was actually delivered to the final recipient.

    It is better to use a library like PHPMailer or Swift Mailer than to use the mail function directory. These libraries take care of many small details when crafting E-Mail messages that help increase the chances of your mail being delivered successfully.

    If you're testing the mail function on Windows, you also need to have a mail server, since Windows does not come with one by default. Alternatively, if you're using a library for sending mail instead it is quite easy to configure them to use a third party SMTP server like gmail.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Hyderabad
    Posts
    10
    Rep Power
    0

    Simple mistake


    change few lines in your code and check it..
    $suc=@mail($to,$subject,$message,$headers);
    if($suc) echo 'success'; else echo 'fail';
    nad if still any problem
    check ur cpanel
    whether mail system is
    enabled or not...
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    15
    Rep Power
    0
    I made the told changes. The mail is still not going to recipient's inbox but is coming to the sender's inbox.
    Sorry i might sound lame, but how do i check whether mail system is enabled or not in cpanel.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    15
    Rep Power
    0
    And it displays "success" when i run the php file.
  14. #8
  15. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Sep 2003
    Location
    Germany
    Posts
    1,355
    Rep Power
    322
    It says "success" because the mail has been sent successfully. Which doesn't guarantee the mail to be received by the recipient.

    To validate that your mail has been delivered to the recipient you'd probably need to check your mail server for error mails which contain the error that occured (recipient's mailbox unavailable, mail account unavailabe etc)
    IkoTikashi - ikotikashi.de
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Smile Solution to PHP Email Issue


    Actually the PHP email function mail() does not physically send the email to the user's email id. Instead it hands over the mail content to the mail server for delivery. The mail server at hosting environment is responsible for sending the emails.

    The mail() function simply returns true on success and not on delivery of email otherwise it returns false on failure.

    On some hosting environment, the PHP email function experiences some problem in contacting with the email server and sending the emails.

    To resolve this issue the fifth additional parameter of mail() function can be used to ensure that the mail() function does its job successfully. Here is the syntax -

    mail ( $to , $subject , $message $additional_headers, $additional_parameters );

    The additional_parameters parameter can be used to pass additional flags as command line options to the program configured to be used when sending mail, as defined by the sendmail_path configuration setting. For example, this can be used to set the envelope sender address when using sendmail with the -f sendmail option.

    I think if you use the fifth -f parameter and give its an email id like this

    $alternate_email_id = "test@test.com";
    $add_param = "-f" . $alternate_email_id;

    then the mail will certainly be sent to the desired destination.

    Thanks.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    15
    Rep Power
    0
    Okay, just tell me the simplest way to send mails via PHP. I am using XAMPP Control Panel for MySQL, Apache. And I do want the mail to be sent to the recipients inbox SURELY...
    Just tell me the way how you would send an email using PHP?
    A sample code would be highly appreciated.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Smile


    Originally Posted by gahuja91
    Okay, just tell me the simplest way to send mails via PHP. I am using XAMPP Control Panel for MySQL, Apache. And I do want the mail to be sent to the recipients inbox SURELY...
    Just tell me the way how you would send an email using PHP?
    A sample code would be highly appreciated.
    Here is the code:
    Code:
    <?php
    $to = "email id";
    $from = "email id";
    $subject = "your subject";
    $message = "your message";
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-Type: text/html;charset=utf-8". "\r\n";
    $headers .= 'From: ' . $from . "\r\n";
    $headers .= 'Reply-To: ' . $to;
    $ffrom = '-f' . $from;
    @mail($to, $subject, $message, $headers, $ffrom);
    ?>
  22. #12
  23. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,920
    Rep Power
    1045
    Hi,

    no idea why people still fumble with mail headers. There are libraries for this:

    phpmailer
    swiftmailer

    ... and probably many more.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    15
    Rep Power
    0

    @links123


    I tried to send the mail using the code told by you.
    Still no success. The mail comes to the sender's inbox but does not go the recipient's inbox.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    15
    Rep Power
    0

    @Jacques1


    I've downloaded PHPMailer but don't know how to integrate it with the PHP.
    Do I need to make changes to some configuration files? If yes, please let me know what changes do i need to make and where?
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Solution to PHP Email Issue


    Originally Posted by gahuja91
    I tried to send the mail using the code told by you.
    Still no success. The mail comes to the sender's inbox but does not go the recipient's inbox.
    You are entering wrong in "To" part of the mail() please put email address to whom you want to send the mail.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo