我试图在继续后发送电子邮件,但它无法正常工作

时间:2018-04-06 06:58:14

标签: php

我正在尝试在继续订购后发送邮件。我正在使用以下代码,但它不起作用。在继续之后它没有发送任何电子邮件。

$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);

2 个答案:

答案 0 :(得分:0)

你在使用本地机器吗?如果是这样,将不会从本地计算机发送电子邮件。或者,如果您要在共享主机上发送电子邮件,有时共享主机阻止简单的发送电子邮件。在任何一种情况下,您都必须使用 SMTP 发送电子邮件。

答案 1 :(得分:0)

您可以尝试以下代码:

$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";
}

如果您仍然遇到任何问题,请告诉我。