Я хочу отправить HTML-страницу по электронной почте с рисунками и стилями CSS, используя PHP. Как я могу добавить свои изображения и мою таблицу стилей CSS без загрузки изображений на сервер?
Ниже приведен код PHP для отправки по электронной почте:
<?php
$to = "[email protected]";
// subject
$subject = "Test mail";
// message
$message = file_get_contents("index.html"); // index.html contains images and css stylesheet which are not displayin in the email
// from
$from = "[email protected]";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= "From:" . $from;
// Mail it
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
Я был бы очень признателен за вашу помощь. Заранее спасибо.
Вы можете добавить файл send html следующим образом:
$file_name = basename($file); // Get file name
$data = file_get_contents($file); // Read file contents
$file_contents = chunk_split(base64_encode($data)); // Encode file data into base64
$uid = md5(time()); // Create unique boundary from timestamps
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "From: {$from_name}<{$mail_from}>";
$headers[] = "Reply-To: {$mail_from}";
$headers[] = "Content-Type: multipart/mixed; boundary=\"{$uid}\"";
$headers[] = "This is a multi-part message in MIME format.";
$headers[] = "--{$uid}";
$headers[] = "Content-type:text/plain; charset=iso-8859-1"; // Set message content type
$headers[] = "Content-Transfer-Encoding: 7bit";
$headers[] = $message; // Dump message
$headers[] = "--{$uid}";
$headers[] = "Content-Type: application/octet-stream; name=\"{$file_name}\""; // Set content type and file name
$headers[] = "Content-Transfer-Encoding: base64"; // Set file encoding base
$headers[] = "Content-Disposition: attachment; filename=\"{$file_name}\""; // Set file Disposition
$headers[] = $file_contents; // Dump file
// Send mail with header information
if (mail($mail_to, $subject, '', implode("\r\n", $headers) ))
return true;
Для сложной электронной почты вы должны использовать класс Mailer, как замечательный класс PHPMailer.
С помощью этого класса очень легко отправлять сложные письма (например, письма HTML). Вы найдете примеры в папке примеров PHPMailer.