PHP Mail Function and Send Mail With Attachment

PHP Mail Function | PHP Send Mail With Attachment

PHP Mail Function | PHP Send Mail With Attachment

The mail sending process has become a commonly used process for any website, it may be used for the contact form or newsletter or a shopping purpose mail generated and sent from the website or system it may be through a crone job also. So PHP Mail Function provides us the functionality to send mail in a properly organized 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 the PHP mail function

There are 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. It’s 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 an LF (\n). Lines should not exceed 70 characters.
  • 4. headers: The headers are Optional. It specifies additional headers, like From, Cc, and Bcc. etc The additional headers should be separated with a CRLF (\r\n).
  • 5. parameters: The parameters are also Optional. Specifies an additional parameter. ie, this can be used to set the envelope sender address when using send a mail with the -f send mail option

Example of PHP Normal Mail Sending


<?php
$to = "[email protected], [email protected]";
$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: <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";

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

 

Example of PHP HTML Mail Sending

<?php
$to = "[email protected], [email protected]";
$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: <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";

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

 

PHP Send Mail With Attachment

The easiest way to send a mail with attachment is to use a 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 a mail with attachment using PHP

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

$email = new PHPMailer();
$email->SetFrom('[email protected]', 'Your Name'); //Name is optional
$email->Subject = 'Message Subject';
$email->Body = $bodytext;
$email->AddAddress( '[email protected]' );

$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 the PHP mail function. Please provide your valuable comments to make the site better.

Thanks for Reading

Share The Post On -