1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How can I send emails from PHP from my hosting service?

Discussion in 'PHP' started by HenryCan, Jun 26, 2017.

  1. #1
    I'm using a free hosting service to host my website. I'd like to send emails occasionally to notify myself of problems in my website, for instance unexpected errors of significant severity.
    SEMrush
    I know that the mail() function in PHP is widely used for this purpose but it doesn't seem to work for programs running in my hosting service; I copied an example from a StackExchange discussion, changed the content of the email appropriately, and executed the code but no email showed up in my inbox and there were no error messages (probably because I'm not doing error handling correctly yet).

    UPDATE: I rewrote the code a bit and added some error checking so that it looks like this:
    function Send_Email() {
    
        echo 'Composing email....' . '<br/>';
       
        $to      = 'foo@bar.com';
        $subject = 'Reminder';
        $message = 'The ants in France dance mainly on the plants.';
        $headers = 'From: foo@bar.com' . "\r\n" .
                'Reply-To: foo@bar.com' . "\r\n" .
                'X-Mailer: PHP/' . phpversion();
       
        echo 'Sending email....' . '<br/>';
       
        $rc = mail($to, $subject, $message, $headers);
        if (!$rc) {
            $errorMessage = error_get_last()['message'];
            echo $errorMessage;
        }
        echo 'Return code: ' . $rc . '<br/>';
    }
    PHP:
    When I executed it, I got:

    Composing email....
    Sending email....
    Return code: 1
    Code (markup):
    Notice that I didn't get any error message even though the return code was non-zero. I don't know where to find a table listing the possible return codes from mail(); it's certainly not in the mail() article in the PHP manual.

    I'm guessing that the hosting environment may not send emails as configured and I don't know if I can reconfigure the environment to allow for email. (I'm assuming I can't change or even see php.ini which is apparently the normal place to configure email settings since it is presumably shared by lots of other users.)

    Can anyone suggest an approach for sending emails in this environment?
     
    Last edited: Jun 26, 2017
    HenryCan, Jun 26, 2017 IP
    SEMrush
  2. sarahk

    sarahk iTamer Staff

    Messages:
    26,435
    Likes Received:
    3,956
    Best Answers:
    111
    Trophy Points:
    665
    #2
    I send all my site emails through Mandrill. Costs me the base $20/month for mailchimp but I can then send as many as I want.

    However... since you're on a free host you probably won't want to spend just to get email. How about you move to a paid host where email is actually allowed.

    FWIW free hosts probably turn email off to prevent spammers signing up
     
    sarahk, Jun 26, 2017 IP
  3. HenryCan

    HenryCan Member

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #3
    If I had money to spend, I'd either get something like Mandrill or a paid host that allows email. Unfortunately, our club has virtually nothing in the bank since we don't charge membership fees.

    The good news is that I got mail() to work despite being on a hosting service. I'm limited to sending 50 emails a day but that's fine. I only intend to use the emails to notify myself of major errors and I'm hoping to keep those under 50/day!

    Here's my revised code for the sake of anyone else having this problem down the road. (I'm not sure why the original version didn't work but I'm guessing I didn't have the headers quite right. The return codes from mail() seem to be only 0 and 1; I had assumed there were a bunch of possible return codes and that they were listed somewhere but I seem to have been wrong about that. I'm still get a return code of 1 from this revised code but I'm actually receiving the emails so I figure that's good enough for now. I'll delve into this again down the road when I have the time to get it 100% perfect.

        Notify_Webmaster();
    
    function Notify_Webmaster() {   
       
        define("TIMESTAMP_FORMAT", "Y-m-d-H.i.s.u");
        $currentTimestamp = date(TIMESTAMP_FORMAT);
        $filename = $_SERVER['PHP_SELF'];
        $message = "Connection failure for main  database";
       
       
        Send_Email_About_Logged_Error($currentTimestamp, $filename, $message);
       
    }
    
    function Send_Email_About_Logged_Error($timestamp, $filename, $message) {
       
            echo 'Composing email....' . '<br/>';
       
            $to = 'foo@bar.com'; //the webmaster's email address
            $from = 'php_log@mywebsite.com'; //something that implies the PHP log
            $subject = 'Website - Error Notification';
            $body1 = "Attention Webmaster:\r\n\r\n";
            $body2 = 'An error was detected that requires your attention.' . "\r\n\r\n";
            $body3 = 'Timestamp of error: ' . $timestamp . "\r\n";
            $body4 = 'File encountering error: ' . $filename . "\r\n";
            $body5 = 'Message: ' . $message . "\r\n";
            $body8 = "\r\n\r\n" . 'Thank you for your attention.';
            $body9 = "\r\n\r\n" . 'Your buddy, the  Logging Notification System';
            $message = $body1 . $body2 . $body3 . $body4 . $body5 . $body8 . $body9;
       
            $headers = "From:" . $from;
       
            echo 'Sending email....' . '<br/>';
       
            $rc = mail($to,$subject,$message,$headers);
            echo 'Return code: ' . $rc . '<br/>';
       
            echo "Mail Sent.";
            // You can also use header('Location: thank_you.php'); to redirect to another page.
    }
    PHP:
     
    HenryCan, Jun 26, 2017 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    Personally I never trust mail() unless you know it works. Most hosts disable it to avoid spam. Instead can you send via SMTP? Maybe using a solid library like PHPMailer:

    https://github.com/PHPMailer/PHPMailer

    This would be independent of server settings and you can be sure that you can get them almost all the time. You can even use your Gmail logins to send (or any other email system that allows SMTP) for free.
     
    ThePHPMaster, Jun 26, 2017 IP
    sarahk likes this.
  5. HenryCan

    HenryCan Member

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #5
    I saw that suggestion in other forums that I consulted but I was doubtful about getting PHPMailer to work in my environment of a hosting service. I got the impression PHPMailer was intended for a standalone server environment. Am I wrong?
     
    HenryCan, Jun 27, 2017 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    26,435
    Likes Received:
    3,956
    Best Answers:
    111
    Trophy Points:
    665
    #6
    I've used it on shared hosting, a long time ago, but it was my go-to when I wanted to setup html emails.
     
    sarahk, Jun 27, 2017 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    Just as a note - 0 or 1 isn't a "return code" - mail() returns either true or false (a boolean value), 1(true) if the email was sent, and 0 (false) if the email was not sent, there was an error, or mail() for some reason had a hickup. There isn't really a point in returning the 1 - just check to see if true or false, and if false, throw an error.
     
    PoPSiCLe, Jun 30, 2017 IP