views:

35

answers:

1

Hello,

I want to send emails where the data is from a php/mySQL query.

I know that html will render in the email but i don't think the php codes will.

So is there any way where i can send emails with content queried from a mySQL DB ?

I already search here, there is one topic that covers it but the person who advised suggested a pdf export or to use a 3rd party tool which in my case are not applicable.

Thank you for the help guys :)

+2  A: 

Use PHPMailer to generate the email on the server. It makes it very easy to generate multi-part messages (plaintext + html, with attachments and embedded/inline images). Basically:

// set up PHPMailer
$mail = new PHPMailer();
$mail->SetFrom('[email protected]');
$mail->AddReplyTo('[email protected]');
$mail->Subject('Your profile');
$mail->IsHTML(TRUE);

// do your database query
$con = connect_to_database();
$stmt = run_database_query($con, "SELECT ... FROM ...");

$data = fetch_from_database($stmt);


// set the email address
$mail->AddAddress($data['email'], $data['fullname']);


// html content for smart email clients
$html = <<<EOL
<h1>Welcome</h1>

<p>Your username is {$data['username']}.</p>
EOL;

// plain text alternate content
$text = <<<EOL
Welcome

Your username is {$data['username']}.
EOL;

// add the content to the mail
$mail->MsgHTML($html);
// add alternate content 
$mail->AltBody($text);


// send the mail
if ($mail->Send()) {
   // mail sent correctly
} else {
   die("Uhoh, could not send to {$mail['email']}:" . $mail->ErrorInfo);
}
Marc B
trying this out <3 thanks :D but i am getting error on EOL - i tried removing, replacing with quotes...:$
mireille raad
Oops. `$text = <<<EOF;` <--shouldn't have had a semicolon there. Fixed it now.
Marc B