Tue Jun 16 2020

E-mail with Attachment

PHP Scripting953 views

File Name: PHP-email-attachment.php

<html>
	<head>
		<title>Send Mail with Attachment</title>
	</head>
	<body>
		<?php
			if(isset($_POST['mail_to'])) {

				/* Send from */
				$from_add = "info@geekboots.com";
				
				$semi_rand = md5(time()); 
				$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
				
				/* Email headers */
				
				/* Email contain type and version */
				$headers  = 'MIME-Version: 1.0' . "\n";
				$headers .= "Content-Type: multipart/mixed;\n";
				$headers .=  "boundary=\"{$mime_boundary}\"";
				
				/* Email send from and subject */
				$headers .= 'From: Geekboots <'.$from_add.'>' . "\n";
				$headers .= "Subject: ".$_POST['subject']."\n";
				
				/* Reply path of the mail */
				$headers .= "Reply-To: ".$from_add."\n";
				$headers .= "Return-Path: ".$from_add."\n";
				
				/* Organisation of the mail sender */
				$headers .= "Organization: Geekboots\n";
				
				/* Unique message id */
				$headers .= "Message-ID: <notif-".time()."@geekboots.com>\n";
				
				/* Mailer info */
				$headers .= "X-Mailer: Geekboots, PHP v".phpversion()."\n";
				
				/* Add message */
				$message = "This is a multi-part message in MIME format.\n\n"; 
				$message .= "--{$mime_boundary}\n"; 
				$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
				$message .= "Content-Transfer-Encoding: 7bit\n\n";
				$message .= $_POST['message'] . "\n\n"; 
				$message .= "--{$mime_boundary}\n";
        		
				/* Attached file */
				$file = fopen($_FILES['attached']['tmp_name'],"rb");
				$data = fread($file,filesize($_FILES['attached']['tmp_name']));
				fclose($file);
				$filename = $_FILES['attached']['name'];
				$data = chunk_split(base64_encode($data));
				$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$file\"\n"; 
				$message .= "Content-Disposition: attachment;\n" . " filename=\"$filename\"\n"; 
				$message .= "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
				$message .= "--{$mime_boundary}\n";
				
				/* PHP mail() function to send mail */
				if(mail($_POST['mail_to'], $_POST['subject'], $message, $headers))
					echo "Mail sent successfully!";
				else
					echo "Send fail!";
				
			}
		?>
		<form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>">
			<input type="email" name="mail_to" required placeholder="To" /><br />
			<input type="text" name="subject" required placeholder="Subject" /><br />
			<textarea name="message" placeholder="Message"></textarea><br />
			Attachment: <input type="file" name="attached" /><br />
			<input type="submit" value="Send" />
		</form>
	</body>
</html>



/* Output */
To: yourmail@gmail.com
Subject: Testing
Message: Testing PHP Mail
Attachment: choose file

Mail sent successfully!
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.