Problems with mail on Unix server.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
josamoto
Forum Commoner
Posts: 41
Joined: Fri Aug 24, 2007 6:57 am
Location: South Africa
Contact:

Problems with mail on Unix server.

Post by josamoto »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi all

I've got a script that sends mail. It works fine on my Windows XP machine, but when uploading to the Unix server, my HTML messages are sent as plain text, and reply-to is not set. So I guess somehow my headers are going missing. Here's the script:

Code: Select all

<?php
class Email {
    // Member variables
    private $to = '';
    private $subject = '';
    private $headers = '';
    private $message = '';
    
    /**
     * Constructor
     * @return Email
     */
    function Email() {
        
    }
    
    /**
     * Sends an email according to $_POST variables set.
     */
    public function send(){
        require_once 'config.php';
        require_once 'parser.php';
        
        // Check parameters first
        if(!$this->checkParams()){
            echo 'Unable to send email! Some required parameters are missing.';
            return false;
        }
        
        // Setup recipients
        $this->to  = $config['email'];
        
        // Setup email subject
        $this->subject = $_POST['subject'];
        
        // Setup and parse message
        $parser = new Parser();
        $data = array(
            'name' => $_POST['name'],
            'email' => $_POST['email'],
            'subject' => $_POST['subject'],
            'message' => $_POST['message']
        );
        $this->message = $parser->parse($config['email_template'], $data);
        
        // Set Content-Type header for HTML email
        $this->headers  = 'MIME-Version: 1.0' . "\r\n";
        $this->headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        
        // Setup additional headers
        $this->headers .= 'From: ' . $_POST['name'] . '<' . $_POST['email'] . '>' . "\r\n";
        
        // Send the email, returning its result
        if(mail($this->to, $this->subject, $this->message, $this->headers))
            echo 'Email successfully sent!';
        else
            echo 'Unable to send email!';
    }
    
    /**
     * Checks that the required $_POST parameters are set.
     * @return true - $_POSTs are valid. false - Some posts are invalid or have not been set.
     */
    private function checkParams(){
        if(!isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['subject'])|| !isset($_POST['message']))
            return false;
        
        return true;
    }
}
?>
Could it be newlines and stuff giving problems? Please help...


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Problems with mail on Unix server.

Post by pickle »

Does your email program let you check the MIME headers that are sent? I'm not too sure what the problem is, but looking at headers is always a start.

Also, you should really be sanitizing your $_POST variables rather than blindly using them.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply