Sending Email with fuelPHP

Sending email with fuelPHP is really straight forward, as are most things with fuel!

In this example i am sending a quote to a client and attaching a PDF:

[code]
$email = \Email::forge();
$email->to($data[‘quote’]->client->email, $data[‘quote’]->client->name);
$email->subject(‘New Quote from ‘ .$data[‘quote’]->user->username);
$email->html_body(\View::forge(’email/quotetoclient’, $data));
$email->from(‘changed-no-spam@stuartsillitoe.co.uk’, ‘Stuart’);
$view = View::forge(‘quotes/print’, $data, false);
$pdf = Pdf::factory(‘dompdf’)->init();
$pdf->set_paper(‘A4’);
$pdf->load_html($view);
$pdf->render();
$out = $pdf->output();
$tmp = $_SERVER[‘DOCUMENT_ROOT’] .’/tmp/tmp.pdf’;
file_put_contents($tmp, $out);
$email->attach($tmp, false, null, null, $data[‘quote’]->ref .’.pdf’);
$email->send();
[/code]

There should be a check if $mail->send() actually worked or not, so this is better:
[code]
try
{
$email->send();
}
catch(\EmailSendingFailedException $e)
{
die(‘Failed to send mail.’);
}
catch(\EmailValidationFailedException $e)
{
die(‘Email address is not valid.’);
}
[/code]




No Comments


No comments yet.



Leave a Reply

Your email address will not be published. Required fields are marked *