Sunday, 3 September 2023

PHP Email with Attachment by php mail

 <?php 

 

// Recipient 

$to = 'receiver email address'; 

 

// Sender 

$from = 'sender email address'; 

$fromName = 'sender name'; 

 

// Email subject 

$subject = 'PHP Email with Attachment by php mail';  

 

// Attachment file 

$file = "common-600.png"; // attachment file name

 

// Email body content 

$htmlContent = ' 

    <h3>PHP Email with Attachment by php mail</h3> 

    <p>This email is sent from the PHP script with attachment.</p> 

'; 

 

// Header for sender info 

$headers = "From: $fromName"." <".$from.">"; 

 

// Boundary  

$semi_rand = md5(time());  

$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";  

 

// Headers for attachment  

$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

 

// Multipart boundary  

$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" . 

"Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n";  

 

// Preparing attachment 

if(!empty($file) > 0){ 

    if(is_file($file)){ 

        $message .= "--{$mime_boundary}\n"; 

        $fp =    @fopen($file,"rb"); 

        $data =  @fread($fp,filesize($file)); 

 

        @fclose($fp); 

        $data = chunk_split(base64_encode($data)); 

        $message .= "Content-Type: application/octet-stream; name=\"".basename($file)."\"\n" .  

        "Content-Description: ".basename($file)."\n" . 

        "Content-Disposition: attachment;\n" . " filename=\"".basename($file)."\"; size=".filesize($file).";\n" .  

        "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 

    } 

$message .= "--{$mime_boundary}--"; 

$returnpath = "-f" . $from; 

 

// Send email 

$mail = @mail($to, $subject, $message, $headers, $returnpath);  

 

// Email sending status 

echo $mail?"<h1>Email Sent Successfully!</h1>":"<h1>Email sending failed.</h1>"; 

 

?>

send image as content in php mail

 // (A) MAIL SETTINGS

$mailTo = "receiver_email_address@domain_name";

$mailSubject = "इच्छापूर्ति के लिए मन्त्र सिद्ध माला ";


// (B) MAIL MESSAGE

// HOST THE IMAGE ON YOUR OWN SERVER!

// ALSO REMEMBER TO PROVIDE THE DIRECT LINK JUST-IN-CASE

$img = "https://email.aksharmty.in/common-600.png";

$mailBody = "<a href='https://aksharmty.in/mantra-siddh-mala.php'><img src='$img'></a><br>";

$mailBody .= "अधिक जानकारी के लिए वेबसाइट पर जाएं : https://aksharmty.in/mantra-siddh-mala.php<br>";


// (C) HEADER - HTML MAIL

$from = 'sender@domain_name'; 

$fromName = 'sender name'; 

$mailHead = implode("\r\n", [

  "MIME-Version: 1.0",

  "Content-type: text/html; charset=utf-8",

  "From: $fromName"." <".$from.">"

]);


// (D) SEND

echo mail($mailTo, $mailSubject, $mailBody, $mailHead)

  ? "OK" : "ERROR" ;

?>