使用localhost和实时服务器发送邮件的区别

时间:2014-07-28 12:46:02

标签: php image email

我想知道使用localhost和实时服务器发送邮件的区别。 这是我的代码,有人帮助纠正这个代码,以便邮件和附件可以成功发送。显示邮件发送但我没有收到任何。我不知道错误或代码丢失的位置发生。帮助我,以便我可以纠正它。 //这是m1.php文件

<?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">

//这是m2.php文件。从m1.php链接重定向到这里,带附件的邮件在这里完成

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

2 个答案:

答案 0 :(得分:0)

如果您的邮件发送成功,

mail()将返回值。 试试这个。

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

答案 1 :(得分:0)

您没有收到电子邮件的原因可能是您没有将本地主机配置为运行SMTP服务器。

要在localhost上测试电子邮件,我使用Antix SMTP冒名顶替者。它位于您的计算机上,显示您的应用已连接到SMTP服务器时发送的电子邮件。这样可以让您看到收到的电子邮件,而无需担心从测试环境发送实时电子邮件。