0

I am trying send email using php mail() function. It sends email but only HTML Part and not including the dynamic values through php. Any help will be appreciated.

I am trying to make it work but it is not working. I have tried multiple variations but still it only sends HTML part.

    $to = $vendor_email;
        $subject = "";
        $mime_boundary = '<<<--==+X['.md5(time()).']';

        $headers[] = 'MIME-Version: 1.0';
        $headers[] = 'Content-Type: multipart/mixed;';
        $headers[] = ' boundary="'.$mime_boundary.'"';
        $message = '<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="utf-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/home.css">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<title>BuyBusTours</title>
<style>
body{
            font-family: Calibri,Candara,Segoe,"Segoe UI",Optima,Arial,sans-serif;


        }

h4{
    text-align: center;
}
#wrapper{

width: 60%;
}
hr{
    border-color: black;
    border-width: 1px;
}
label{
        display: inline-block;
        width: 250px;
    }

</style>
</head>
<body>
    <div class="container" id="wrapper">

                <h3 style="text-align: center; background-color: orange; height: 60px; padding-top: 18px;">Booking Confirmation for Order Number: { <?php echo $order_id;?> }</h3>

                    <h4>Dear <?php echo $vendor_name ?></h4><br>
                    <h4>Thank you for Choosing BuyBusTours</h4>
                    <hr>


                    <p style="color:red;">This is the confirmation email that we have received your order and it is processing. Please do not confuse it with your e-ticket.</p>
                        <p>We have received your order and we are working on it. As soon as your order is confirmed we will send your E-Ticket. Complete information about your tour and other important details will be mentioned on your E-Ticket. If in case your tour is sold out or it is not operating we shall refund your complete payment.
                    </p>
                    <p>This is <b>NOT A CONFIRMATION</b> that you have reserved a seat on the bus. We are working on your order and as soon as it is confirmed we will send your e-ticket.</p>
                    <hr>

                    <h3 style="color:#F60">Order Details</h3>
                    <label>Tour Name:</label><?php echo $tour_name;?><br>
                    <label>Departure Date</label> <?php echo $dept_date;?> <br>

                    <label>Departure Location:</label> <?php echo $dept_loc;?> <br>
                    <label>Tour Code:</label> <?php echo $tourcode;?> <br>
                    <label>Vendor Tour Code:</label> <?php echo $vendorcode;?> <br>
                    <label>Number of People:</label> <?php echo $no_of_adult + $no_of_child;?> <br>

                    <?php if (!empty($no_of_rooms)) { ?>

                        <?php for ($i = 1 ; $i <= $no_of_rooms ; $i++) {?>

                        <?php if ($i == 1) {?>
                            <label style="color:#F33">Room <?php echo $i;?></label> <br>
                            <?php if (!empty($room1traveler1)) {?>
                                <label>Traveler 1: </label>
                                <?php echo $room1traveler1;?><br>
                            <?php } ?>
                            <?php if (!empty($room1traveler2)) {?>
                                <label>Traveler 2: </label>
                                <?php echo $room1traveler2;?><br>
                            <?php } ?>
                            <?php if (!empty($room1traveler3)) {?>
                                <label>Traveler 3: </label>
                                <?php echo $room1traveler3;?><br>
                            <?php } ?>
                        <?php } ?>

                        <?php if ($i == 2) {?>
                            <label style="color:#F33">Room <?php echo $i;?></label><br>
                            <?php if (!empty($room2traveler1)) {?>
                                <label>Traveler 1: </label>
                                <?php echo $room2traveler1;?><br>
                            <?php } ?>
                            <?php if (!empty($room2traveler2)) {?>
                                <label>Traveler 2: </label>
                                <?php echo $room2traveler2;?><br>
                            <?php } ?>
                            <?php if (!empty($room2traveler3)) {?>
                                <label>Traveler 3: </label>
                                <?php echo $room2traveler3;?><br>
                            <?php } ?>
                        <?php } ?>

                        <?php if ($i == 3) {?>
                            <label style="color:#F33">Room <?php echo $i;?></label><br>
                            <?php if (!empty($room3traveler1)) {?>
                                <label>Traveler 1: </label>
                                <?php echo $room3traveler1;?><br>
                            <?php } ?>
                            <?php if (!empty($room3traveler2)) {?>
                                <label>Traveler 2: </label>
                                <?php echo $room3traveler2;?><br>
                            <?php } ?>
                            <?php if (!empty($room3traveler3)) {?>
                                <label>Traveler 3: </label>
                                <?php  $row["room3_traveler3"];?><br>
                            <?php } ?>
                        <?php } ?>

                        <?php if ($i == 4) {?>
                            <label style="color:#F33">Room <?php echo $i;?></label><br>
                            <?php if (!empty($room4traveler1)) {?>
                                <label>Traveler 1: </label>
                                <?php echo $room4traveler1;?><br>
                            <?php } ?>
                            <?php if (!empty($room4traveler2)) {?>
                                <label>Traveler 2: </label>
                                <?php echo $room4traveler2;?><br>
                            <?php } ?>
                            <?php if (!empty($room4traveler3)) {?>
                                <label>Traveler 3: </label>
                                <?php echo $room4traveler3;?><br>
                            <?php } ?>
                        <?php } ?>

                    <?php } }?>

                    <hr>
                    <h3 style="color:red; text-align:center">Total Amount Charged: <?php echo $total_price;?></h3>


                <h3 style="text-align: center; background-color: orange; height: 60px; padding-top: 18px;">Contact us at: 0345-1272211</h3>

        </div>

</body>
</html>';
        $message = html_entity_decode($message);
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        mail($to, $subject, $message, $headers);
  • Because `$message` is all one variable `$message = '<` you need to put the php variables in slightly differently. You break the inverted commas and concatenate using "dot" e.g ' . $tourcode . '
    and then you are back into text html until the next variable. You don't need the `echo`. A larger example http://stackoverflow.com/questions/33965765/get-variable-in-while-loop-based-on-what-user-chooses/33966051#33966051 You also need From: and Reply-To: values in your header and the email must be from a valid address on your server.
    – Steve Dec 13 '15 at 18:40

2 Answers2

2
<h4>Dear <?php echo $vendor_name ?></h4><br>

Should be

<h4>Dear '.$vendor_name.'</h4><br>

The reason being is that in the mail body you are already in <?php ?> tags, and so there is no needs to repeat them.

What's happening here is you are ending $message='.......... with Dear '

and then concatenating with the . $variable . (concatenate again) '</h4> Resume writing out $message.

Repeat this for all of the times you use a variable within a string.

Anything in there such as an if() statement or a for() loop, you should calculate these before you start defining any of the email strings, and assign each of their results an individual $var and assign just like I mention above. This will also make your code less messy, easier to navigate and thus adjust if and when you need to.

Adam Copley
  • 1,495
  • 1
  • 13
  • 31
1

Your email message is being sent inside single quotes, ', so no PHP variables will be interpolated inside it. That will fix quite a few of the problems immediately, but a bigger problem is that you appear to have PHP statements inside there too, such as loops and conditionals – you can't put those inside strings.

What you need to do is pre-calculate your text, i.e. moving all those loops and conditionals out of your HTML message text, so that whatever they calculate end up just being variables. You should then switch to using double-quoted strings (or, in this case, heredoc is probably easier) to put those values into your message.

TwoStraws
  • 12,862
  • 3
  • 57
  • 71