0

I want a copy of each email sent from my HTML online form to create a log file in my server root filepath, which each phpmail() sent, adding to the log file.

I am using the current php post file. Is there any method to do it?

<?php
$webmaster_email = ("myemail@domain.co.uk");

$email_address = $_REQUEST['email'] ;
$comments = $_REQUEST['comment'] ;
$telephone = $_REQUEST['telephone'] ;
$first_name = $_REQUEST['name'] ;
$msg = 
"Name: " . $first_name . "\r\n" . 
"Email: " . $email_address . "\r\n" . "\r\n" .
"Phone No: " . $telephone . "\r\n" . "\r\n" .     
"Comments: " . $comments ;
    mail( "$webmaster_email", "Contact Form Results", $msg );
?>

So basically all the Request statements and current time/date into a log file, is this even possible with php?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Does [this](https://serverfault.com/questions/154212/get-log-of-mail-sent-with-mail-from-php-sendmail) and [this](https://stackoverflow.com/questions/14850756/how-to-log-all-calls-to-a-function-in-php-mail-function) help? – nice_dev Nov 26 '19 at 15:03
  • 1
    Of course its possible. Just takes a bit of work. Just collect the info you want and write it to a file. ___Of course a database would be a better idea as pretty soon the text file will get big an cumbersome___ – RiggsFolly Nov 26 '19 at 15:03
  • I guess this is WAY beyond my coding skills, I take a look into it Many thanks x –  Nov 26 '19 at 15:04
  • 1
    Is there a special reason you want to store data in a file rather than using a database? Files can be a lot of work. – Funk Forty Niner Nov 26 '19 at 15:05
  • Because in my head, I didn't even know a database can be done. From my javascript experience, I will use CreateTextFile to create a text file from HTML. But PHP is beyond my abilties, well time to google more haha –  Nov 26 '19 at 15:08
  • @FunkFortyNiner I think the most simple way of doing this, is to create a throwaway gmail account, and BCC all emails there. I think I found the best solution! –  Nov 26 '19 at 15:10
  • @UrbanOzzy if your script sent lots of email in a shorten time, your server may be subjected to email blacklist, so I suggest to you , if it is your case, to use a file as log instead. – user2342558 Nov 26 '19 at 15:22

2 Answers2

1

You can log the mail result and other info directly in a file used as log:

if(mail( "$webmaster_email", "Contact Form Results", $msg ))
{
    // email sent
    logMail("$webmaster_email", "Contact Form Results", 1);
}
else
{
    // email failed
    logMail("$webmaster_email", "Contact Form Results", 0);
}

function logMail($email, $subject, $success=1)
{
    $pathLog = 'logMail.txt';
    $logContent = file_get_contents($pathLog);
    $logContent .= date('Y-m-d H:i:s');
    $logContent .= "\t";
    $logContent .= "Email ".($success ? 'sent' : 'FAILED!')."\n";
    $logContent .= "To: $email\n";
    $logContent .= "Subject: $subject\n";
    $logContent .= "\n\n";
    file_put_contents($pathLog, $logContent);
}
user2342558
  • 5,567
  • 5
  • 33
  • 54
-2

Have you tried to configure mail.log (Available since PHP 5.3.0) in php.ini?

https://www.php.net/manual/en/mail.configuration.php

mail.log string

The path to a log file that will log all mail() calls. Log entries include the full path of the script, line number, To address and headers.

AnFi
  • 10,493
  • 3
  • 23
  • 47