0

I am trying to send mail after proceed order. I am using following code but its not working. its not sending any email after proceed.

$to = 'abc@gmail.com';
$subject = "Order Confirmation - Your Order with Mysite.com[order0001] has been successfully placed!";
$from = " admin@mysite.com";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = "From: $from\nMIME-Version: 1.0\nContent-Type: text/html; charset=utf-8\n";
$message = '<html><head></head><body><h1>Hi !</h1><table>';

while($main = mysqli_fetch_array($selector)) {
    $prid = $main['p_id'];
    $qu = $main['quantity'];
    $ototal = $main['order_total'];
    $norder = $main['net_order_amount'];
    $slecp = mysqli_query($db, "SELECT * FROM `product` WHERE `p_id` = '$prid'");
    $selr = mysqli_fetch_array($slecp);

    $prname = $selr['p_name'];
    $primg = $selr['p_image'];
    $image = "http://www.myindiamade.com/images/$primg";

    $message .= "<tr>
                   <td><img src='".$image."'></td>
                 </tr>";
} 
$message .='</table></body></html>';
mail($to,$subject,$message,$headers);
Qirel
  • 25,449
  • 7
  • 45
  • 62
shoeb
  • 1
  • 2
  • Are you working on localhost? Because mail function not working in localhost – Nirali Apr 06 '18 at 07:01
  • Query inside a loop, and without binding your variables is not a good idea. Learn how to use a prepared statements, and use `JOIN` instead of running a query inside a loop. – Qirel Apr 06 '18 at 07:02
  • 1
    Possible duplicate of [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – Qirel Apr 06 '18 at 07:02
  • I would look at using something like `PHPmailer` That is what all the cool kids use. – ArtisticPhoenix Apr 06 '18 at 07:04
  • 1
    I think you are overwriting `$headers = "From: $from\nMIME-Version: 1.0\nContent-Type: text/html; charset=utf-8\n";` Try `$headers .= "From: $from\nMIME-Version: 1.0\nContent-Type: text/html; charset=utf-8\n";` – The fourth bird Apr 06 '18 at 07:04

2 Answers2

0

Are you working on the local machine? if so email will not be sent from the local machine. OR if you are sending the email on shared hosting, sometimes share hosting block the simple sending email. You have to send email using SMTP in either case.

abrar
  • 480
  • 7
  • 10
0

You can try this code:

$to = "youremail@gmail.com";
$from = "Myname <sender@gmail.com>";
$subject = "Test Attachment Email";

$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "document.pdf";


//$pdfdoc is PDF generated by FPDF
$pdfdoc     = "/opt/transmail/2018-03-07_32_11564_invoice.pdf";
$attachment = chunk_split(base64_encode($pdfdoc));

// main header
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

// no more headers after this, we start the body! //

$message = "Thanks";
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;

// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;

// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";

// send message
if (mail($to, $subject, $body, $headers)) {
echo "mail send ... OK";
} else {
echo "mail send ... ERROR";
}

let me know if you still facing any issue..

Sachin Tiwari
  • 434
  • 3
  • 6
  • I have tested the code on my machine.. And, it working fine.This example will help him to pass the correct format of email header if he is missing something. – Sachin Tiwari Apr 06 '18 at 07:26