Jump to content

[SOLVED] mail() function is not working, any ideas?


Recommended Posts

So, I'll show you two codes in a class which handles the loading of the variables in the email, and actually sending the email itself:

 

<?php


        public function load_email($to='',$subject='',$message='',$cc='',$bcc='',$mail_type='') {

                //always be sure everything has been defaulted just in case it somehow cached
                $this->recipient = null;
                $this->subject = null;
                $this->message = null;
                $this->sender = "hanyTV <webmaster@hany.tv>";
                $this->cc = null;
                $this->bcc = null;
                $this->headers = null;
                $this->loaded = false;

                if(empty($to)){
                        return array(false,"missing","recipient");
                } elseif(empty($subject)){
                        return array(false,"missing","subject");
                } elseif(empty($message)){
                        return array(false,"missing","message");
                } elseif(!$this->check_email_address($to)){
                        return array(false,"invalid format","recipient");
                } elseif(!empty($cc) && !is_array($cc)){
                        return array(false,"invalid format","cc");
                } elseif(!empty($bcc) && !is_array($bcc)){
                        return array(false,"invalid format","bcc");
                }

                $this->recipient = $to;
                $this->subject = $subject;
                $this->message = wordwrap($message,70);
                $this->sender = "From: ".$this->sender."\r\n";

                if(!empty($cc)){
                        $this->cc .= "Cc: ";
                        foreach($cc as $add){
                                $this->cc .= "$add, ";
                        }
                        $this->cc .= "\r\n";
                }

                if(!empty($bcc)){
                        $this->bcc .= "Bcc: ";
                        foreach($bcc as $add){
                                $this->bcc .= "$add, ";
                        }
                        $this->bcc .= "\r\n";
                }

                //format the headers correctly with the From,CC,BCC, etc...

                if($mail_type == 'html'){//send html mail by formatting headers FIRST
                        $this->headers  = 'MIME-Version: 1.0' . "\r\n";
                        $this->headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                } else {//we are not sending html mail, so we must strip the message of all html
                        $this->message = strip_tags($this->message);
                }
                $this->headers .= $this->sender.$this->cc.$this->bcc;
                $this->loaded_email = true;
                return array(true);
        }

        public function send_email() {
                if($this->loaded_email){
                        $sent = mail($this->recipient,$this->subject,$this->message,$this->headers);
                        echo "<textarea rows=4 style='width:100%;'>mail(".$this->recipient.",".$this->subject.",".$this->message.",".$this->headers.");</textarea>";
                        return array($sent);
                } else {
                        return array(false,"did not call","load_email()");
                }
        }
?>

 

load_email() is a function, inserting the values as needed, will validate each value, and basically build the headers for the email.

send_email() does what it says, if load_email() was executed successfully, it will send the email. As you see in the code, I had it output a textarea to show exactly what values I was passing to the mail() function:

 


mail(********@aol.com,Hey,I've got some things for you to look at if you have time.,From: hanyTV <webmaster@hany.tv>
);

 

The output tells me that I'm passing the values into the function CORRECTLY, and yet it will not send :-\

 

On another note, looking into php.net's manual, i tried the following code:

 

$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

 

And it didn't work, directly by using php.net's code, and I'm still not getting any errors. Any ideas?

Try sending a simple mail:

 

<?php
// The message
$message = "Line 1\nLine 2\nLine 3";

// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70);

// Send
mail('caffinated@example.com', 'My Subject', $message);
?>

 

If something simple won't work, then the problem is the server, not the code.

Ok, so I've tried a different domain, and it looks like it works with a @gmail.com address.

 

Also, it works with @yahoo.com. It seems like it fails with any @aol.com address (which confuses me). I've checked the spam folder, and the email just never gets sent. Anyone know why AOL would not receive the email?

 

Try sending a simple mail:

 

<?php
// The message
$message = "Line 1\nLine 2\nLine 3";

// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70);

// Send
mail('caffinated@example.com', 'My Subject', $message);
?>

 

If something simple won't work, then the problem is the server, not the code.

 

I tried sending your simple email code, and it does work, just not with an AOL domain (and perhaps some others, but I don't have THAT many different email accounts o_o)

Ok, so I've finally solved it with a VERY weird solution.

 

The additional parameter in the mail() function must be set for AOL mail to even accept it (something about not having "nothing@" for a default email address which is set by your host).

 

So it looks like this:

 

mail("recipient","subject","message","headers","-fYOURNAME@YOURDOMAIN");

 

Where yourname@yourdomain was a web mail account that was set up through cPanel.

Hi,

 

I seem to be having the same problem but I can't get it working. I've tried making a simple example, but it still no good...

 

<?php

$to = "myemailaddress@hotmail.com";
$subject = "Hi!";
$body = "Hi, How are you?";
$headers="From: user@domainofsite.co.uk \r\n" .
							"Reply-To: user@domainofsite.co.uk \r\n" .
							"X-Mailer: PHP/" . phpversion();

if (mail("$to","$subject","$body","$headers")) {
  echo("<p>Message successfully sent!</p>");
} else {
  echo("<p>Message delivery failed...</p>");
}

?>

 

It always says it is sent, but I get nothing. There is another page on the site using the mail() function and it works fine. I just can't see why.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.