0

I wish to know the difference between mail sending using localhost and live server. This is my code and someone help to correct this code so that mail along with attachment can be sent successfully.it showed mail sent but I did not receive any.I am not sure where the error or code missing happens.Help me so that I can rectify it. //This is m1.php file

<?php
$max_no_img=1; // Maximum number of images value to be set here

echo "<form method=post action=m2.php enctype='multipart/form-data'>";

echo "<tr><td>Photo: </td><td><input type='file' name='uploadfile' class='bginput'></td></tr>";
?>
<input type="submit" name="Submit" value="Send">

//This is m2.php file.From m1.php link redirects to here and the mail with attachment to be done here

<?php
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->SMTPDebug = true;
$mail->IsSMTP();  // telling the class to use SMTP
$mail->SMTPAuth   = true; // SMTP authentication
$mail->Host       = "ssl://smtp.gmail.com"; // SMTP server
$mail->Port       = 465; // SMTP Port
$mail->Username   = "mymail@gmail.com"; // SMTP account username
$mail->Password   = "password"; 
if($_POST){
$targetFolder   =   "image/".$_FILES['uploadfile']['name']; //My location where images stored & retrieved
copy($_FILES['uploadfile']['tmp_name'],$targetFolder);
$htmlbody = " Your Mail Content Here.... You can use html tags here...";
$to = "mymail@gmail.com"; //Recipient Email Address
$subject = "Test email with attachment"; //Email Subject
$headers = "From: mymail@gmail.com\r\nReply-To: mymail@gmail.com";
$random_hash = md5(date('r', time()));
$headers .= "\r\nContent-Type: multipart/mixed; 
boundary=\"PHP-mixed-".$random_hash."\"";
// Set your file path here
$attachment = chunk_split(base64_encode(file_get_contents($targetFolder))); 

//define the body of the message.
$message = "--PHP-mixed-$random_hash\r\n"."Content-Type: multipart/alternative; 
boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
$message .= "--PHP-alt-$random_hash\r\n"."Content-Type: text/plain; 
charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n";

//Insert the html message.
$message .= $htmlbody;
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";

//include attachment
$message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: application/zip; 
name=\"$targetFolder\"\r\n"."Content-Transfer-Encoding: 
base64\r\n"."Content-Disposition: attachment\r\n\r\n";

$message .= $attachment;
$message .= "/r/n--PHP-mixed-$random_hash--";

//send the email
$mail = mail( $to, $subject , $message, $headers );

echo $mail ? "Mail sent" : "Mail failed";
}
?>
DAK
  • 1
  • 2

2 Answers2

0

mail() will return value if your mail sent successfully. Try this.

$status = mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 
if($status) {
echo 'Mail sent successfully';
} else {
echo 'Mail sending failed';
}
RajivRisi
  • 398
  • 1
  • 12
0

The reason that you are not receiving the email is likely that you have not configured your localhost machine to run an SMTP server.

To test emails on localhost I use Antix SMTP imposter. It sits on your machine and shows emails that would have been sent had your app been connected to an SMTP server. This has the added bonus of allowing you to see the emails as they would be received without having to worry about sending live emails from your test environment.