简体   繁体   中英

Message sent, not receiving mail - SMTP

I have a function that is supposed to send a coupon through SMTP from outlook365 and my normal PEAR mail.

I'm currently doing testing to send it to my own e-mail. When I click submit I get the success message "Message successfully sent!" however when I check my email inbox I receive nothing.

Is there something I am not including or doing wrong?

<?
ini_set("include_path", '/home/busaweb/php:' . ini_get("include_path") );
require_once "Mail.php";
include('Mail/mime.php');
function send_notification_email($coupon, $subject){
$email_from = "coupons@*****.com";
$email_to = "jason@*****.com";
$email_subject = $subject;

// Email Message
// This is an HTML formatted message
$email_html_msg = file_get_contents('email_templates/notification.html', true);
$email_html_msg = str_replace("*|HEADER|*", $subject, $email_html_msg);
$email_html_msg = str_replace("*|COUPON-ID|*", $coupon->id, $email_html_msg);
$email_html_msg = str_replace("*|TYPE|*", $coupon->type, $email_html_msg);
$email_html_msg = str_replace("*|PRODUCT|*", $coupon->product, $email_html_msg);
$email_html_msg = str_replace("*|DESC|*", $coupon->desc, $email_html_msg);
$email_html_msg = str_replace("*|FIRST-NAME|*", $coupon->first_name, $email_html_msg);
$email_html_msg = str_replace("*|LAST-NAME|*", $coupon->last_name, $email_html_msg);
$email_html_msg = str_replace("*|EMAIL|*", $coupon->email, $email_html_msg);
$email_html_msg = str_replace("*|ZIPCODE|*", $coupon->zip, $email_html_msg);
$email_html_msg = str_replace("*|PRODUCTS|*", stripslashes($coupon->products), $email_html_msg);
$email_html_msg = str_replace("*|NEWSLETTER|*", $coupon->newsletter, $email_html_msg);


// start setting up the email header
$headers = "From: ".$email_from;

// create boundary string
// boundary string must be unique using MD5 to generate a pseudo random hash
$random_hash = md5(date('r', time())); 
$mime_boundary = "==Multipart_Boundary_x{$random_hash}x";

// set email header as a multipart/mixed message
// this allows the sending of an attachment combined with the HTML message
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

// multipart boundary for the HTML message
$email_message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=UTF-8\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_html_msg . "\n\n";

// end the multipart message
$email_message .= "--{$mime_boundary}--\n";

$host = "ssl://smtp.office365.com";
$port = "587";
$username = "coupons@*****.com";
$password = "*****";
$headers = array ('From' => $email_from,   'To' => $email_to,   'Subject' => $headers);
$smtp = Mail::factory('smtp', array('host' => $host, 'port' => $port,  'auth' => true, 'username' => $username, 'password' => $password));
$sendit = $smtp->send($email_to, $headers, $email_message);  
$sendit->SMTPDebug  = 1;
if (PEAR::isError($mail)) {   
echo("<p>" . $mail->getMessage() . "</p>");  
 } 
else {   
echo("<p>Message successfully sent!</p>"); 
 }
 }

Your mail might have been transmitted to the server but then got blocked somewhere. This might be because of strange content, like

$headers = "From: ".$email_from;
...
$headers .= "\nMIME-Version: 1.0\n" .
  "Content-Type: multipart/mixed;\n" .
  " boundary=\"{$mime_boundary}\"";
...
$headers = array ('From' => $email_from,   'To' => $email_to,   'Subject' => $headers);

Thus effectively you construct a header containing From and some MIME setup but then you use this header as the subject of the mail which is really strange. I don't know what the result will be but it might simply be some invalid mail which therefore gets eaten or blocked by some server in between.

Apart from that: why are you trying to construct a multipart/mixed mail, where the multipart consists of a single part? It's not really wrong, only useless.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM