This script works fine, but there's a part I'm not sure if there's an easier / better way to do it. The part I'm talking about is commented in the script. Also any general advice on improvements if there are any to be would be cool.

<?php
if (isset($_POST['submit'])) {

    //Variables
    $name  = Trim(stripslashes($_POST['name']));
    $email = Trim(stripslashes($_POST['email']));
    $id    = rand(1000, 9999);

    //Headers
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: email@email.co.uk' . "\r\n";
    $headers .= 'Cc: email@email.co.uk' . "\r\n";

    //Email Information
    $to      = $email;
    $subject = 'Project Information';

    //Email Body
    $message = '<html><body>';
    $message .= '<h1>Message</h1>';
    $message .= '<h1>Quote ID</h1>';
    $message .= '<p>' . $id . '</p>';
    if (!empty($name)) {
        $message .= '<h1>Name</h1>';
        $message .= '<p>' . $name . '</p>';
    }
    $message .= '<h1>Message</h1>';
    $message .= '<a href="http://www.domain.com/clients/' . $name . '.php">Link</a>';
    $message .= '</body></html>';

    //Send Email
    if (mail($to, $subject, $message, $headers)) {
        echo 'Message Sent';
    } else {
        echo 'Message Failed';
    }

    //Unsure about this
    //Here a php page is generated by the main php
    //I've done it like the message above but I'm unsure if this is correct?
    $request = '<?php ';
    $request .= '$headers = "From: email@email.co.uk"."\r\n";';
    $request .= '$to = "email@email.co.uk";';
    $request .= '$subject = "Invoice Request";';
    $request .= '$message = "' . $name . ' is requesting ' . $id . ' quote";';
    $request .= 'mail($to, $subject, $message, $headers);';
    $request .= 'unlink(__FILE__)';
    $request .= ' ?>';
    $request .= '<h1>Invoice Being sent asap</h1>';

    //Write the above php to a file
    $myfile = fopen('clients/' . $name . '.php', 'w');
    fwrite($myfile, $request);
    fclose($myfile);

}

?>

Hi,

you could use the Nowdoc syntax, I prefer it because the code is more readable, but it's your choice, an example:

<?php

$id      = rand(1,1000);
$name    = 'MissMolly';
$email   = 'email@email.tld';
$request = <<<'EOD'
<?php
    $to      = "%1$s";
    $subject = "Invoice Request";
    $message = "%2$s is requesting %3$d quote";
    $headers = "From: %1$s\r\n";

    mail($to, $subject, $message, $headers);
    unlink(__FILE__);
?>
<h1>Invoice Being sent asap</h1>
EOD;

$request = sprintf($request, $email, $name, $id);
$myfile  = fopen('clients/' . $name . '.php', 'w');
fwrite($myfile, $request);
fclose($myfile);

With sprintf() then you replace the placeholders %1$s, %2$s, %3$d with the data you want to inject:

Do you have a specific reason to create such file? The email is already sent by the line 33.

Also, you're using the $name variable to set the filename, but if $name contains special characters or spaces, then it could affect the access to the generated PHP file, you should use a function to replace the spaces, something like this:

I also question the need for making a file for each email sent, especially by name of the sender..

If you have database access, you should probably use it. You can templatize a single php page, and get the values from the database for that user. That way, you also have a paper trail of all the emails you sent out, and you wont be sucking up so much disk space making new files.

Just my 2c

commented: +1 +14

Thanks for the help guys, I'll try that out!

And seeing as people are questioning what I'm doing here, it's bascially a little quote / invoicing system I'm making. Bascially:

I fill out a form with the project and client information (project propasl, timeframes, cost etc...)
It emails that information to them as a quote for them to check
If they agree they have a link to click in the email to request a propper invoice
The link sends them to the php page created saying "invoice is being sent"
That page also sends me an email telling me they want a propper invoice
I can them send them one though paypal

I'm just trying to automate this best I can to cut down on time emailing. And to answer ryans questions, I get the whole database thing maybe useful, I just didn't want to be storing information if i could help it, and the pages that are created are deleted once they've been viewed so it's not wasting any space.

I'll give the sugestions a go and maybe check back with a better version!

Thanks guys (:

Member Avatar for diafol

For invoicing, it can be very useful for the recipient to get a pdf for their own records. So, while sending a dump of templatized data may work for you, it may not be so convenient for the recipient.
Having had to set up a pseudo-invoicing system of my own recently, this is what I do:

1) Have a set of html template files with placeholders. One of which is "extra_info", which can serve to add individual data if required.
2) Have an invoices DB table, with a status, sent_date and extra_info fields. The sent_date is NULL until a "message successfully sent" notification is received and then this is updated with the date (as well as status = success value). On fail, the status field is given an error value.
3) So no completed templates (similar to merged documents in office software) are created - only a handful of DB values updated.
4) During this process a pdf file is created (fpdf, but you can use a plethora of others) an attached to the email. The email body has HTML version of the invoice.

Caveat: Doing this for a large number of recipients in one action would probably incur a timeout issue due to the time it takes to create a single pdf for each recipient. However, there are ways to do this, e.g. with a cron job, to create files in a temp folder.

Here's an example:

templates
    email
        invoice14.html
        invoice7.html
        invoice28.html
    pdf
         invoice14.php
         invoice7.php
         invoice28.php

You can use sprintf as mentioned or even go the full monty and use a templating engine like Twig, Smarty, Blade etc etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.