9

I looked all over for this question and all I found on this was to add the following:

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

and

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

I am wanting to send a newsletter type email, so the styling really matters for this. All of the videos I watched were just making html sheets, so I really didn't get that. I want to style the content in my email.

I have this right now:

$to = $newsletter_email;
        $subject = 'Thank you for subscribing';
        $message = '
            <html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title></title>
            <style>
                #email-wrap {
                background: #151515;
                color: #FFF;
                }
            </style>
            </head>
            <body>
                <div id="email-wrap">
                <p>Hi,</p><br>
                <p>Thank you.</p><br>
                <p>Thank you,</p>
                <p>Administration</p>
                </div>
            </body>
            </html>
                ';

                $from = "newsletter@example.com";
                //$Bcc = "example@example.com";

                // 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";

                // Additional headers
                $headers .= 'To: ' .$to. "\r\n";
                $headers .= 'From: ' .$from. "\r\n";
            //  $headers .= 'Bcc: '.$Bcc. "\r\n";

                // Send the email
                mail($to,$subject,$message,$headers);

I have tried taking out the style from this message variable and turning this file into a html styled file, outside of the php:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Newsletter</title>
<style>
#email-wrap {
    background: #151515;
    color: #FFF;
}
</style>
</head>

etc.

The email actually sends, but I cannot figure out how to add style to this. What am I doing wrong??

            $email_from = "newsletter@example.com";

            $full_name = 'Company Name';
            //$from_mail = $full_name.'<'.$email_from.'>';
            $from = $from_mail;

            //$from = "newsletter@example.com";
            //$Bcc = "example@example.com";

            // To send HTML mail, the Content-type header must be set
            $headers .= "From: ".$full_name." <".$email_from.">\r\n";
            and $headers .= "Return-Path: ".$full_name." <".$email_from.">\r\n";
            /*$headers = "" .
                       "Reply-To:" . $from . "\r\n" .
                       "X-Mailer: PHP/" . phpversion();*/
            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

            // Additional headers
            $headers .= 'To: ' .$to. "\r\n";
            $headers .= 'From: ' .$from_email. "\r\n";
        //  $headers .= 'Bcc: '.$Bcc. "\r\n";

            // Send the email
            mail($to,$subject,$message,$headers);

enter image description here

Ralph
  • 307
  • 1
  • 2
  • 14
  • 1
    Email support inline css.use inline css for mail design. – meet Feb 27 '16 at 09:09
  • Be aware that your code (and all the posted answers) is vulnerable to header injection attacks, possibly others, and will not encode email contents correctly. Don't roll your own email code, you'll do it wrong, as all this demonstrates; use a library like [PHPMailer](https://github.com/PHPMailer/PHPMailer) that you tagged this question with. – Synchro Feb 27 '16 at 12:50
  • Your charsets should agree - you're setting UTF-8 in the HTML, but ISO-8859-1 in the message headers; you can't have both at once - it will break as soon as you have non-ascii text – Synchro Feb 27 '16 at 12:52

4 Answers4

7

You need to use inline style to get it works on your email

    $to = $newsletter_email;
    $subject = 'Thank you for subscribing';
    $message = '
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title></title>
        </head>
        <body>
            <div id="email-wrap" style='background: #151515;color: #FFF;'>
            <p>Hi,</p><br>
            <p>Thank you.</p><br>
            <p>Thank you,</p>
            <p>Administration</p>
            </div>
        </body>
        </html>
            ';

            $from = "newsletter@example.com";
            //$Bcc = "example@example.com";

            // 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";

            // Additional headers
            $headers .= 'To: ' .$to. "\r\n";
            $headers .= 'From: ' .$from. "\r\n";
        //  $headers .= 'Bcc: '.$Bcc. "\r\n";

            // Send the email
            mail($to,$subject,$message,$headers);

For the second part of your question you can use something like this

$to = 'test@server.com';
$email_from = "best.buy@yahoo.com";

$full_name = 'Best Buy';
$from_mail = $full_name.'<'.$email_from.'>';
$from = $from_mail;
$headers = "" .
           "Reply-To:" . $from . "\r\n" .
           "X-Mailer: PHP/" . phpversion();
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
$headers .= 'From: ' . $from_email . "\r\n";       
mail($to,$subject,$message,$headers);
Peter Wilson
  • 4,150
  • 3
  • 35
  • 62
  • Do you know why my name is coming up as newsletter when sending this? I assumed it was because of my header. So I tried doing this `$headers .= 'From: Company'"\r\n";` and it breaks the code – Ralph Feb 27 '16 at 09:23
  • @Ralph do you mean it appears as `newsletter `instead of `newsletter@example.com` – Peter Wilson Feb 27 '16 at 09:30
  • No, when you go into gmail or whatever mail client you use, next to the subject line, where it shows who the email is from, mine says `newsletter`. I am wanting it to say my `Company Name`. So if I was Best Buy, I would want it to say `Best Buy`. – Ralph Feb 27 '16 at 09:32
  • I believe the from param in a PHP header is looking for an email address. – symlink Feb 27 '16 at 09:39
  • You need to Change you variable $from to be `$from = "Best Buy";` instead of `$from = "newsletter@example.com";` – Peter Wilson Feb 27 '16 at 09:40
  • @PeterWilson Won't that not use my email address as the recipient then? – Ralph Feb 27 '16 at 09:46
  • @Ralph No, the recipient is $to not $from – Peter Wilson Feb 27 '16 at 09:54
  • @PeterWilson Yea, sorry. I saw that and tried to edit my post as quick as possible. Guess it wasn't quick enough. Doing this : `$from = "Company Name";` shows up as `unknown sender` and doesn't show my email address. – Ralph Feb 27 '16 at 09:54
  • @Ralph Ok I got it please refer to this question http://stackoverflow.com/questions/8365754/change-the-sender-name-php-mail-instead-of-sitenamehostname-com – Peter Wilson Feb 27 '16 at 10:11
  • @PeterWilson Unfortunately, that is still showing the unknown sender. I added my code to my question. – Ralph Feb 27 '16 at 10:23
  • @Ralph try `$headers .= "From: ".$full_name." <".$email_from.">\r\n"; ` and `$headers .= "Return-Path: ".$full_name." <".$email_from.">\r\n"; ` – Peter Wilson Feb 27 '16 at 10:40
  • @PeterWilson That isn't allowing me to send it. OR my code could just be so messed up from trying this in so many variations, I updated my question with current code, – Ralph Feb 28 '16 at 03:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/104767/discussion-between-peter-wilson-and-ralph). – Peter Wilson Feb 28 '16 at 07:42
3

Just try to make it like template.

Here is the little bit example may be helpful.

Create my_template.html file & add following code in that file.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Newsletter</title>
<style>
#email-wrap {
    background: #151515;
    color: #FFF;
}
</style>
</head>
<body>
<p>Hi {{USERNAME}}</p>
<p>Please click on below link </p><br>
{{LINK}}
</body>
</html>

now Write send email function where you want. and follow the step :

1) Read HTML file you recently created.

$html = file_get_contents('my_template.html');

2) Replace Variables

$link = "<a href='LINK YOU WANR TO PLACE'>Click Here </a>";
$html =  str_replace("{{USERNAME}}",$username,$html);
$html =  str_replace("{{LINK}}",$link,$html);

3) Finally send email.

            $from = "newsletter@example.com";
            $headers .= 'To: ' .$to. "\r\n";
            $headers .= 'From: ' .$from. "\r\n";
            $headers .= 'Bcc: '.$Bcc. "\r\n";

            // Send the email
            mail($to,$subject,$html,$headers);
Ankit vadariya
  • 1,253
  • 13
  • 14
0

Indeed, the emails that you send will be opened in different ways, either through an email client installed on the machine that will integrate an HTML rendering engine propriéaitre, or an online e-mail client that will your emails in a rather special roping and withdraw some HTML attributes. The table proposed by Campaign Monitor gives you a very quick overview of the extent of damage and we note that, according to the email clients, rules are completely different.
link : https://www.campaignmonitor.com/css/
However there are tools such as inline-gulp-css that can turn your HTML styles making them "online".
link : https://www.npmjs.com/package/gulp-inline-css

bhrached
  • 127
  • 1
  • 9
-1

TRY

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>


</head>
<body>
                <div style="background:#151515; color: #FFF;">
                <p>Hi,</p><br>
                <p>Thank you.</p><br>
                <p>Thank you,</p>
                <p>Administration</p>
                </div>
</body>
</html>
Omari Victor Omosa
  • 2,814
  • 2
  • 24
  • 46