Peanut
Peanut

Reputation: 3295

Mail function not sending in HTML format

HUGE EDIT!!!!!!!!! Ok, so it's not the HTML or whatever. When I paste it raw in the PHP message variable, it sends perfectly formatted. It's something with the textarea box paste part... that's making it not work properly. Is it the newline thing?.... Hmmmmm

Ok so this has been a frustrating day for me. I'm trying to send HTML emails where I can paste it in the message form textarea. Well I stripped it down to the barebones and it will NOT let anything above the body and after the body be put into HTML.

It prints this:

<html> <head> <title>HalfOffDeals - Thank you!</title> </head> <body>

Then it spits out the body... although unformatted with no style.

Then this afterwards:

</body> </html>

I tried putting the inline CSS how you're suppose to with the style tag and it just omits that completely. I'm using CodeIgniter and I don't know if it strips it automatically which it shouldn't.. I have a form that says (TO, SUBJECT, and MESSAGE). And in the message part, you paste your HTML email template and it's suppose to send to the to email.

And another question I had was how to add '' to the headers:

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

I'm just confused on why my email isn't accepting HTML. This is how I have it setup:

public function send_email() {
    $to = $this->input->post('to');
    $subject = $this->input->post('subject');
    $message .= $this->input->post('message');

    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

    // Additional headers
    $headers .= 'To: Customer <'.$to . "\r\n";
    $headers .= 'From: [email protected] <[email protected]>' . "\r\n";

    // Mail it
    mail($to, $subject, $message, $headers);
}

Any ideas?

HERE IS THE EMAIL TEMPLATE: http://pastebin.com/D04cLXe4

Upvotes: 0

Views: 4264

Answers (4)

Ronald Ara&#250;jo
Ronald Ara&#250;jo

Reputation: 1479

Back when I used the PHP mail function, I also ended up with some problems. Then I met the class phpmailer and my problems were over. Take a good look at it. It's worth it.

Upvotes: 0

mccakici
mccakici

Reputation: 550

 $headers .= 'To: Customer <'.$to . "\r\n";

change to

 $headers .= 'To: Customer <'. $to .'>'."\r\n";

and try:

$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

change to 

$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";

your server is linux, newline is "\n" if windows "\r\n"

Upvotes: 4

joni jones
joni jones

Reputation: 2995

To send html in email you can use default CI email settings;

    $this->load->library('email');
    $config = array(
        'mailtype' => 'html',
        'newline' => '\r\n',
        'charset' => 'utf-8' //default charset
    );
    $this->email->initialize($config);
    $this->email->from($sender);
    $this->email->to($receiver);
    $this->email->subject($subject);
    $this->email->message($message);
    $this->email->send();

For more details see CI email class. Also try to use $this->email->print_debugger() to see email errors/

Upvotes: 0

Elias Dorneles
Elias Dorneles

Reputation: 23796

I see that you are indeed using CodeIgniter -- so, I recommend using CodeIgniter's Email class.

Try changing your function to this:

public function send_email() {
    $to = $this->input->post('to');
    $subject = $this->input->post('subject');
    $message .= $this->input->post('message');

    $this->load->library('email');

    // To send HTML mail, set the appropriate mailtype
    $this->email->set_mailtype('html');

    // setup the message
    $this->email->to($to);
    $this->email->from('[email protected]');
    $this->email->subject('Put the subject here');
    $this->email->message($message);

    // Mail it
    $this->email->send();
}

This will do the right thing to handle HTML emails even if they have attachments. Save your code to what matters, you don't need to do all this work. ;)

Upvotes: 1

Related Questions