Creekstone
Creekstone

Reputation: 85

PHPMailer with body contents generated on the fly

So I'm using PHPMailer to send an email to a user database once a week using my web host's scheduled tasks. However, the contents of the email need to be generated at run-time when the scheduled task runs.

In my code, I create the HTML contents, generate the HTML file and save it into a directory on the server, then try to use that newly created HTML file as the email's body contents.

However, when I run the page I get:

Mailer Error ([email protected]) Message body empty

Any ideas how to resolve this? Code below:

$emailMessage .= "HTML code is in here.";

$fileName = date("YmdHis") . "-email.html";
$newEmail = fopen("path/to/file/$fileName", "w") or die("Unable to open file!");
fwrite($newEmail, $emailMessage);
fclose($newEmail);

$body = file_get_contents('path/to/file/$fileName');

$mail = new PHPMailer;
$mail->isSMTP();
$mail->msgHTML($body);

etc etc

If I hard code in the name of the file in the $body line it works, but not when I use the variable name. Anyone know if it's possible or is there a better way to send an email that's generated on the fly?

Upvotes: 0

Views: 47

Answers (2)

Sabato
Sabato

Reputation: 19

Only using double quotes (") PHP will interpret variable names

Upvotes: 1

Mr Glass
Mr Glass

Reputation: 1306

Quotes matter. Change the single quotes in

$body = file_get_contents('path/to/file/$fileName');

To double quotes

$body = file_get_contents("path/to/file/$fileName");

Upvotes: 2

Related Questions