Search code examples
phpsendmail

PHP : How to use dynamic variables


I'm trying to send HTML email from my Custom Controller.

Here's code.

public function execute()
{
    $to = "[email protected]";
    $subject = "HTML email";

    $message = "
            <html>
            <head>
            <title>HTML email</title>
            </head>
            <body>
            <p>This email contains HTML Tags!</p>
            <table>
            <tr>
            <th>Firstname</th>
            <th>Lastname</th>
            </tr>
            <tr>
            <td>John</td>
            <td>'.$_POST["company_name"].'</td>
            </tr>
            </table>
            </body>
            </html>
            ";


    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

    // More headers
    $headers .= 'From: <[email protected]>';

    mail($to,$subject,$message,$headers);

How Can I use dynamic variables based on POST request so that Email Send perfectly with Dynamic form.

I've tried as I paste above code but still getting syntax erros in PHP Strom.

Any Help would be appreciated.


Solution

  • Instead of:

    <td>'.$_POST["company_name"].'</td>
    

    use:

    <td>".$_POST['company_name']."</td>
    

    Your HTML is quoted with double quotation so to return to PHP you also need to use double one.