Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Php email

  • 26-01-2009 8:57pm
    #1
    Registered Users, Registered Users 2 Posts: 3,875 ✭✭✭


    I go the following example from a website on using php to send emails
    is there any way of being able to include mysql recordset values within the body of the text however?
    when I try it it causes some sort of error and the screen just goes blank and does not show my page
    thanks for any help or pointers.


    <?php
    // multiple recipients
    $to = 'aidan@example.com' . ', '; // note the comma
    $to .= 'wez@example.com';

    // subject
    $subject = 'Birthday Reminders for August';

    // message
    $message = '
    <html>
    <head>
    <title>Birthday Reminders for August</title>
    </head>
    <body>
    <p>Here are the birthdays upcoming in August!</p>
    <table>
    <tr>
    <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
    </tr>
    <tr>
    <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
    </tr>
    <tr>
    <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
    </tr>
    </table>
    </body>
    </html>
    ';

    // 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: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
    $headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
    $headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
    $headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

    // Mail it
    mail($to, $subject, $message, $headers);
    ?>


Comments

  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    of course, you simply place the variables in much like you would when just outputting to a page. how were you trying to do it?


  • Registered Users, Registered Users 2 Posts: 8,488 ✭✭✭Goodshape


    Have you an example of what you tried to do?


  • Registered Users, Registered Users 2 Posts: 3,875 ✭✭✭ShoulderChip


    <?php echo $_POST; ?>

    My problem is putting in a recordset like that
    when i just put it in it thinks it is all text
    so do i just put in
    or how do i go about putting it in, somewhere in the text, sorry if this is a stupid question.


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    You don't "echo".

    echo sends output to the browser, and you want the output to go to the email.

    You are essentially creating a string of text - $message='EVERYTHING UP TO THE END OF THE QUOTATION'

    So I'd do 3 things:

    1) Firstly, wrap the message in double-quotes instead of single quotes

    $message = "
    <html>
    :
    </html>
    ";

    2) Set a variable to the field value BEFORE the $message line

    e.g. $firstName = $_POST;

    ** BTW, $_POST isn't a MySQL recordset **

    3) Then, wherever you need the name output, just put $firstName - no <?php .... ?>, no echo and no quotes - just the variable name; By having the variable name (from 2 above) PHP will do an auto-substitution and avoid the need for you to open and close quotes for each variable.


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    the problem is you're opening your php brackets again, once you have brackets (<?php) open already, you don't need to open them again. after that, you dont need to use an echo statement, as the variable doesn't need to be output right there and then, just included within the body of the email, so you concactenate the variable from the recordset as such:

    [php]
    $message = '
    <html>
    <head>
    <title>Birthday Reminders for August</title>
    </head>
    <body>
    <p>Here are the birthdays upcoming in August!</p>
    <table>
    <tr>
    <th>'.{$_POST}.'</th><th></th>'.{$_POST}.'<th>'.{$_POST}.'</th><th>'.{$_POST}.'</th>
    </tr>
    [/php]

    edit: and as liam said, $_POST is not a recordset from a query, it's values from a form that has been submitted with the post method. also, both methods (his and mine) are doing the same thing, just in different ways. his use of the double quotes ".." allow you to avoid the concatenation, you can simple write in the variable name as demonstrated. i prefer to use the concactenation method simply because it's easier to see your variables if you're using an editor that supports syntax highlighting.


  • Advertisement
Advertisement