PHP Mail Function | PHP Send Mail With Attachment

PHP Mail Function | PHP Send Mail With Attachment

PHP Mail Function | PHP Send Mail With Attachment

The mail sending process is become a commonly used process for any website, it may used for the contact form or newsletter or a shopping purpose mail generated and sent from the website or system it maybe through crone job also. So PHP Mail Function provides us the functionality to send mail in a proper organised way. In this post we are going to see How to Send Mail With and without Attachment in PHP.

The function name is mail(); to send mail through php.

Parameters should pass through php mail function

There is 5 numbers parameters should be sent through the php mail function and they are 1. to, 2. subject, 3. message, 4. headers, and 5. parameters among them 3 are required and they are 1. to, 2. subject, 3. message and we general use 4 parameters and that is the 4. header with the 3 required parameters,

  • 1. to : The to is Required. it is the receivers of the email
  • 2. subject : The subject is Required. Its the subject of the email. and cannot contain any newline characters
  • 3. message : The message is also Required. its the message to be sent. Each line should be separated with a LF (\n). Lines should not exceed 70 characters.
  • 4. headers : The headers is Optional. its specifies additional headers, like From, Cc, and Bcc. etc The additional headers should be separated with a CRLF (\r\n).
  • 5. parameters : The parameters is also Optional. Specifies an additional parameter. ie, this can be used to set the envelope sender address when using send mail with the -f send mail option

Example of PHP Normal Mail Sending

<?php
$to = "somebody@example.com, somebodyelse@example.com";
$subject = "Normal email";

$message = "Hi Its a normal mail sent via php";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";

mail($to,$subject,$message,$headers);
?>

 

Example of PHP HTML Mail Sending

<?php
$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";

$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";

mail($to,$subject,$message,$headers);
?>

 

PHP Send Mail With Attachment

The easiest way to send mail with attachment is to use php mailer, to use that just go to the below link and include the file in your code and then use the below code.

The link to download PHP Mailer Classes : http://github.com/PHPMailer/PHPMailer

The code to send mail with attachment using php

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$email = new PHPMailer();
$email->SetFrom('you@example.com', 'Your Name'); //Name is optional
$email->Subject = 'Message Subject';
$email->Body = $bodytext;
$email->AddAddress( 'destinationaddress@example.com' );

$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';

$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );

return $email->Send();
?>

 

There is another way to send mail

For that first you have to get the content of the file and then have to attach the file content through the body or message portion of the mail.

The Example of send mail with attachment using PHP

<?php

$file = $path.$filename;
$content = file_get_contents( $file);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);

// header
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

// message & attachment
$nmessage = "--".$uid."\r\n";
$nmessage .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$nmessage .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$nmessage .= $message."\r\n\r\n";
$nmessage .= "--".$uid."\r\n";
$nmessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$nmessage .= "Content-Transfer-Encoding: base64\r\n";
$nmessage .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$nmessage .= $content."\r\n\r\n";
$nmessage .= "--".$uid."--";

if (mail($mailto, $subject, $nmessage, $header)) {
return true; // Or do something here
} else {
return false;
}

?>

 

I hope you have learned to send mail using php mail function. Please provide your valuable comment to make the site better.

Thanks for Reading

Share The Post On -

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.