Jump to content

PHP Mail, no errors, it just won't send.


kratsg

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 <[email protected]>";
                $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 <[email protected]>
);

 

I'm displaying it exactly as it was shown in the textarea (except for filtering out my email address). I've tried error_reporting(E_ALL) and interestingly enough, there are no errors with the class, and the return value for the send_email is array(true).

Link to comment
https://forums.phpfreaks.com/topic/123872-php-mail-no-errors-it-just-wont-send/
Share on other sites

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

 

$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

 

Replacing the $to with my email of course... and it didn't work, any ideas?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.