Reputation: 36839
I want to send an Newsletter with PHPMAiler. The newsletter does work, but I'm wondering if there is a better option to do this.
What I have is.
Now my code looks as follows
$mail = new PHPMailer();
//Adding the body
$body = file_get_contents('template/index.htm');
$mail->Subject = "PHPMailer Test Subject via mail(), basic";
$mail->AltBody = "To view this message, please use an HTML compatible email viewer!";
$mail->SetFrom('xxxxxxx', 'xxxxxxxxxx');
$address = "[email protected]";
$mail->AddAddress($address, "xxxxxxx");
$mail->AddEmbeddedImage("template/images/bullet_point.gif","1");
$mail->AddEmbeddedImage("template/images/template_1_01.gif","2");
$mail->AddEmbeddedImage("template/images/template_1_03.gif","3");
$mail->MsgHTML($body);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
I use file_get_contents to get the html page, and AddEmbeddedImage to embed images, now is there a way to pass only the HTML page to PHPMailer and that PHP Mailer will embed these images automatically?
Upvotes: 1
Views: 5272
Reputation: 400962
I don't think what you are trying to do is possible (automatically adding required images) with PHPMailer "from scratch".
Maybe you could parse the HTML to get the list of images it links to ? There are at least 2 ways of doing that :
DOMDocument::loadHTML
on the HTML content loaded from your file (the output of file_get_contents
), and work with the methods of DOMDocument
(and, why not, DOMXPath
?)To get the images, I suppose you have <img>
tags in your e-mail, with src
attributes ; the goal is to find the values of those ones ;-)
Once you have the list of images path, you iterate over them, and call $mail->AddEmbeddedImage
on each one of them.
I suppose that would work just fine (haven't tried though, but I don't see why it wouldn't).
As a side note, the other way would be to keep the images on your server for a couple of days/weeks/months, and not include them in the mail ; it would make the mails smaller, which means :
And to make sure the images don't get erased / replaced on the server before a couple of weeks/month, you can create a new directory for each newsletter, containing it's images -- and once in a while, delete the directories older than X
days.
Upvotes: 1