How send image using mail in core php

To send an image using email in Core PHP, you can use the mail() function and attach the image file to the email. Here's an example code:

<?php

// Define the email settings
$to = '[email protected]';
$subject = 'Image Attachment';
$from = '[email protected]';

// Define the image file path and name
$image_path = 'path/to/image.jpg';
$image_name = 'image.jpg';

// Define the email body
$body = 'This is an email with an image attachment.';

// Create a new email message
$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject,
    'MIME-Version' => '1.0',
    'Content-Type' => 'multipart/mixed'
);

// Add the image file to the email
$attachment = chunk_split(base64_encode(file_get_contents($image_path)));
$headers['Content-Type'] = 'image/jpeg';
$headers['Content-Disposition'] = 'attachment; filename="'. $image_name. '"';
$headers['Content-Transfer-Encoding'] = 'base64';

// Add the email body
$headers['Content-Type'] = 'text/plain';
$headers['Content-Transfer-Encoding'] = '7bit';

// Send the email
mail($to, $subject, $body, implode("\r\n", $headers));

?>

In this example, we use the mail() function to send an email with an image attachment. We define the email settings, image file path and name, and email body. We then create a new email message using the headers array, which includes the From, To, Subject, MIME-Version, and Content-Type headers.

We add the image file to the email using the chunk_split() and base64_encode() functions to encode the file contents, and then set the Content-Type and Content-Disposition headers to indicate that the file is an image attachment.

Finally, we add the email body using the text/plain content type and send the email using the mail() function.

Note that this code assumes that the image file is located at the specified path and that the file name is correct. You may need to modify the code to suit your specific requirements.

Also, you can use PHPMailer library which is more powerful and flexible than the built-in mail() function. Here is an example of how to send an email with an image attachment using PHPMailer:

<?php

require_once 'PHPMailer/PHPMailer.php';
require_once 'PHPMailer/SMTP.php';

$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'your_password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress('[email protected]', 'Recipient Name');

$mail->Subject = 'Image Attachment';
$mail->Body = 'This is an email with an image attachment.';

$mail->addAttachment($image_path, $image_name, 'image/jpeg');

if (!$mail->send()) {
    echo 'Error sending email: '. $mail->ErrorInfo;
} else {
    echo 'Email sent successfully!';
}

?>

This code uses the PHPMailer library to send an email with an image attachment. We define the email settings, image file path and name, and email body. We then add the image file to the email using the addAttachment() method and set the Content-Type header to image/jpeg. Finally, we send the email using the send() method.