Sideshow
Sideshow

Reputation: 1351

php mail() not sending styles

I am trying to send style information along with an email via PHP mail() function. Unfortunately, even though my mail client is set to accept styled emails it still just renders as plain html text.

$to = $email;
    $subject = "Subject Details";
    $message='<html><body><table width="600" height="840" border="0" cellpadding="5" cellspacing="5">';
    $message.='<tr><td height="110"><img src="https://user.co.uk/images/logo.jpg" alt="SignDox" width="300" height="100" /></td></tr>';
    $message.='<tr><td height="29" bgcolor="#5C1561">&nbsp;</td></tr>';
    $message.='<tr><td height="537" valign="top">';
    $message.="Message Goes Here\n\n";
    $message.="Username: ".$new_agent_id."\n";
    $message.="Password: ".$pass1."\n\n";
    $message.="To sign in to your user panel follow this link: \n";
    $message.="Inside your admin section you will be able to change your username and password.\n";
    $message.='</td></tr>';
    $message.='<tr><td height="140" bgcolor="#5C1561">&nbsp;</td></tr></table></body></html>';
    $from = "[email protected]";
    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);

Upvotes: 0

Views: 2877

Answers (6)

Ben Carey
Ben Carey

Reputation: 16968

You need to define the correct headers in order to send HTML formatted emails. This can be done very simply using:

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

However, I would urge you to use the PHPMailer Class. This class will handle all headers and anything else you may need. You can easily add attachments, embed images, send via SMTP etc...

It is a fantastic class, and not to mention the amount you will learn with objects and classes, expecially if you are a newbie to PHP :-)

See here for the PHPMailer Class

Upvotes: 1

Tapas Pal
Tapas Pal

Reputation: 7207

//your header should be like this
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

Upvotes: 0

Jhonathan H.
Jhonathan H.

Reputation: 2713

function send_mail($from,$fromName,$to,$object,$bodyText,$bodyHtml){
    $site = "mywebsite.ca";
    $from = $fromName." <".$from.">";
    $limite = "_----------=_parties_".md5(uniqid (rand()));

    $header  = "Reply-to: ".$from."\n";
    $header .= "From: ".$from."\n";
    $header .= "X-Sender: <".$site.">\n";
    $header .= "X-Mailer: PHP\n";
    $header .= "X-auth-smtp-user: ".$from." \n";    
    $header .= "X-abuse-contact: ".$from." \n"; 
    $header .= "Date: ".date("D, j M Y G:i:s O")."\n";
    $header .= "MIME-Version: 1.0\n";
    $header .= "Content-Type: multipart/alternative; boundary=\"".$limite."\"";

    $message = "";
    $message .= "--".$limite."\n";
    $message .= "Content-Type: text/plain\n";
    $message .= "charset=\"iso-8859-1\"\n";
    $message .= "Content-Transfer-Encoding: 8bit\n\n";
    $message .= $bodyText;

    $message .= "\n\n--".$limite."\n";
    $message .= "Content-Type: text/html; ";
    $message .= "charset=\"iso-8859-1\"; ";
    $message .= "Content-Transfer-Encoding: 8bit;\n\n";
    $message .= $bodyHtml;

    $message .= "\n--".$limite."--";
    if(mail($to,$object,$message,$header)){
      //echo all
    }
    else{
      //echo mssage not submit
    }
}

hope this function can help you. try to use this function to as your send_mail, then at the buttom try to write a condition to see if your message is submitted.You just needed to give inputs in it

Upvotes: 1

NullPoiиteя
NullPoiиteя

Reputation: 57322

try something like

<?php
$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From:" . $from;
/*
$headers[] = "Bcc: JJ Chong <[email protected]>";
$headers[] = "Subject: {$subject}";  */ optional

mail($to, $subject, $message, implode("\r\n", $headers));

Upvotes: 1

MrCode
MrCode

Reputation: 64536

Set the content type, and charset as desired. You should also terminate your from header with \r\n.

$headers = "From:" . $from . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

Upvotes: 1

GBD
GBD

Reputation: 15981

To send HTML mail, the Content-type header must be set

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

Note:

1) If intending to send HTML or otherwise Complex mails, it is recommended to use the PEAR package » PEAR::Mail_Mime.

2) It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.

Please read more this: PHP Manual

Upvotes: 4

Related Questions