0

anyone who can help me to add the code on how to send an HTML Structured Email with Attachment from Project folder using mail function in PHP.

Please review below code I constructed.

$mail_to = 'receiver@gmail.com';
$mail_subject = 'Sample Document Name';
$mail_headers = [
   'MIME-Version'  => '1.0', 
   'Content-Type'  => 'text/html;charset=UTF-8', 
   'From'          => 'PROJECT', 
   'Reply-To'      => 'reply@gmail.com'
];

$mail_receiver_name = 'Sample Receiver Name';
$mail_document_type_name = 'Sample Document Type';
$mail_document_subject = 'Sample Subject';
$mail_document_number = 'Sample Document Number';
$mail_document_remarks = 'Sample Remarks';

ob_start();
require_once('mail_template.php');
$mail_message = ob_get_contents();
ob_end_clean();

$send = mail($mail_to, $mail_subject, $mail_message, $mail_headers);

$output = $send ? 'success' : 'success but email was not sent';

echo $output;
Mr.Jepoyyy
  • 17
  • 6
  • Is this of some help? https://stackoverflow.com/questions/12301358/send-attachments-with-php-mail – Gowire Oct 28 '22 at 06:58
  • @KenLee I usually like your suggestions. Do I really need `PHPMailer` these days to send an email? What's wrong with the built-in `mail()` – Misunderstood Oct 28 '22 at 07:43
  • Hi @Misunderstood, actually the PHP mail() works, but PHPMailer is another alternative. (I just answered the OP's question, using mail() only) – Ken Lee Oct 28 '22 at 08:03

1 Answers1

0
  1. Please add your email address in the From part of the headers , For example :
$mail_headers[] =  'From: Project <realaddress@gmail.com>'; 
  1. Please implode your headers array by using implode("\r\n", $mail_headers) when you use mail()

So use:

mail($mail_to, $mail_subject, $mail_message, implode("\r\n", $mail_headers));

So please use the following (tested - it works)

PHP

<?php
$mail_to = 'realreceiver@gmail.com';

$mail_subject = 'Sample Document Name';

$mail_headers[] =  'MIME-Version: 1.0';
$mail_headers[] =  'Content-Type: text/html;charset=UTF-8'; 
$mail_headers[] =  'From: Project <realaddress@gmail.com>'; 
$mail_headers[] =  'Reply-To :realaddress@gmail.com'; 



$mail_receiver_name = 'Sample Receiver Name';
$mail_document_type_name = 'Sample Document Type';
$mail_document_subject = 'Sample Subject';
$mail_document_number = 'Sample Document Number';
$mail_document_remarks = 'Sample Remarks';

ob_start();
require_once('mail_template.php');
$mail_message = ob_get_contents();
ob_end_clean();



$send = mail($mail_to, $mail_subject, $mail_message, implode("\r\n", $mail_headers));

$output = $send ? 'success' : 'success but email was not sent';

echo $output;

?>

mail_template.php (can be something like the following:)


Hello Ken 

<br>
<?php echo $mail_receiver_name; ?>
<br>
<?php echo $mail_document_type_name; ?>
<br>
<?php echo $mail_document_subject; ?>
<br>
<?php echo $mail_document_number; ?>
<br>
<?php echo $mail_document_remarks; ?>


** Please use your own email addresses to replace the above "realaddress@gmail.com" and "realreceiver@gmail.com" email accounts.

Ken Lee
  • 6,985
  • 3
  • 10
  • 29