-3

I want to send php mail with template below is my code

    <?php 
    // sending email
$SendFrom = "MyWebsite <no-reply@mywebsite.com>";
$SendTo = "$user_email";
$SubjectLine = "Welcome Email Activation";

// Build Message Body from Web Form Input
$MsgBody='Hello, <br/> <br/> Your are most welcome to Mywebsite.com<br/> <br/> Before you continue kindly activate your email by clicking on the below link or copy and paste it in your browser and hit enter for your email activation to take place.<br/> <br/> <a href="'.$base_url.'activation/index.php?code='.$activation.'">'.$base_url.'activation/index.php?code='.$activation.'</a>';
$MsgBody = htmlspecialchars($MsgBody); //make content safe

// Send E-Mail and Direct Browser to Confirmation Page

mail($SendTo, $SubjectLine, $MsgBody, "From: " . $SendFrom);
    ?>

Notice that i using a simple php code and not PDO as it is working for me Thanks, any help will be appreciated.

  • 3
    PDO? There are no database calls in your example :/ – Sverri M. Olsen Oct 02 '15 at 13:24
  • What PHP Data Objects has to do with anything here? And what is your question, exactly? – al'ein Oct 02 '15 at 13:26
  • What is your question? Is the code not working? – NaijaProgrammer Oct 02 '15 at 13:26
  • if you want send an email with html, format the header properly if does not them message will send plain text – rray Oct 02 '15 at 13:34
  • And do not use deprecated functions! – rray Oct 02 '15 at 13:34
  • at:Sverri i have already database calls in my full php at:Alan and @NaijaProgrammer i just want to know how to send an email template with php, just as i described in my question as you can see how i am sending the message so i want to add an email template to the $MsgBody= – Uchechi Odunze Oct 02 '15 at 13:43
  • You just have to build an HTML page and load it inside a variable. – al'ein Oct 02 '15 at 13:51
  • you didn't tell us what problem you're having. Given the comments and the answer below, they're pretty much correct about this. – Funk Forty Niner Oct 02 '15 at 13:58
  • plus, since you're trying to use `
    `'s for line breaks, those won't work without an HTML headers declaration. If you want to use your existing code without sending as HTML, use `\n` and not `
    `.
    – Funk Forty Niner Oct 02 '15 at 14:00
  • @Fred-ii- alright got ya on that one, yet how do i go about the HTML email template. – Uchechi Odunze Oct 02 '15 at 14:20
  • 1
    show us your template – Funk Forty Niner Oct 02 '15 at 14:21
  • @Fred-ii- i got no template design i thought there is a way of designing it in the php and send it do i have to design it in another file and include it? if so how that work? – Uchechi Odunze Oct 02 '15 at 14:43
  • you can easily include a file to a preset template and assigning variables to it. I can't see how that would not work, I use one myself but that template was built for a client, so I can't share it. – Funk Forty Niner Oct 02 '15 at 14:45
  • Here, check these out http://stackoverflow.com/q/1739188/ and http://stackoverflow.com/q/3706855/ they'll give you a few ideas; best I can do. Good luck - Edit: most particularly this answer http://stackoverflow.com/a/1739207/ – Funk Forty Niner Oct 02 '15 at 14:51
  • @Fred-ii- @rray @alan-machado alright guys i have figure out a sending template to use but i got a problem here while i can't seems to figure out how to put the `$user_email` on the `$to` i did this `$to = '$user_email';` when it sends it fails because it is sending to `$user_email@server1.mywebsite.com` instead of `registered_user@gmail.com` – Uchechi Odunze Oct 02 '15 at 20:46

2 Answers2

1

For display html an email, you need define this info in the header, Content-type: text/html; charset=utf-8

$headers = "From: no-reply@mywebsite.com" . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";

mail($SendTo, $SubjectLine, $MsgBody, $headers);

mail() function is kind painfull, there are other better options like, PHPMailer or swiftmailer this libs are easy to use and you don't need config headers bullshit.

rray
  • 2,518
  • 1
  • 28
  • 38
0
    <?php
    function sendEmail(**$SendTo, $SubjectLine, $MsgBody,$SendFrom**)
    {

        $mail = null;
        $mail = new PHPMailer(true);
        $mail->IsSMTP();
        try
        {
            $mail->Host = "Host Name Enter"; // SMTP server
            $mail->SMTPDebug = 0;                     // enables SMTP debug information (for testing)
            $mail->SMTPAuth = true;                  // enable SMTP authentication
            // sets the SMTP server
            $mail->Port = 2525;                    // set the SMTP port for the GMAIL server
            $mail->Username = "Enter SMTP Username"; // SMTP account username
            $mail->Password = "Enter Password";        // SMTP account password
            $mail->AddAddress($SendTo);

            $mail->SetFrom($SendFrom, 'Subject To'**);
            $mail->Subject = $subject;
    //                $mail->AddEmbeddedImage('uploads/'.$res['user_img'], 'prf');
    //                                <img height='100' width='100' style='border 5px solid gray;' `enter code here`src='cid:prf' alt='' />
            $mail->MsgHTML($MsgBody);

            return $mail->Send();
        } catch (Exception enter code hereon $e)
        {
            echo $e;
        }
        $mail->ClearAllRecipients();
    }

    ?>
-----------
OR
-----------
$SendFrom = "To: $msgTo\r\n";
$SendFrom .= "From: no-reply@email.com\r\n";
$SendFrom .= "Bcc: $bcc\r\n";
$SendFrom .= "X-Mailer: PHP".phpversion();
Patel Yatin
  • 149
  • 1
  • 3