2014-09-06 76 views
1

我在PHP新手。我試圖使用PHP發送電子郵件,但我不知道我的代碼中有什麼問題。我GOOGLE了很多,但沒有任何工作。這是我的PHP代碼。我使用class.phpmailer.php。無法發送電子郵件使用核心php

<?php 

require("phpmailer-master/class.phpmailer.php"); 
$mail = new PHPMailer(); 
$mail->IsSMTP(); // send via SMTP 
IsSMTP(); // send via SMTP 
$mail->SMTPAuth = true; // turn on SMTP authentication 
$mail->Username = "[email protected]"; // SMTP username 
$mail->Password = "mypassword"; // SMTP password 
$webmaster_email = "[email protected]"; //Reply to this email ID 
$email="[email protected]"; // Recipients email ID 
$name="myname"; // Recipient's name 
$mail->From = $webmaster_email; 
$mail->FromName = "Webmaster"; 
$mail->AddAddress($email,$name); 
$mail->AddReplyTo($webmaster_email,"Webmaster"); 
$mail->WordWrap = 50; // set word wrap 
$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment 
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment 
$mail->IsHTML(true); // send as HTML 
$mail->Subject = "This is the subject"; 
$mail->Body = "Hi, 
This is the HTML BODY "; //HTML Body 
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body 
if(!$mail->Send()) 
{ 
echo "Mailer Error: " . $mail->ErrorInfo; 
} 
else 
{ 
echo "Message has been sent"; 
} 
?> 
echo "Mailer Error: " . $mail->ErrorInfo; 
} 
else 
{ 
echo "Message has been sent"; 
} 
?> 
+0

你得到了什麼錯誤@Pooja? – 2014-09-06 06:59:56

+0

什麼都沒有。只是一個空白頁面,我甚至看到了控制檯。 – 2014-09-06 07:01:28

+0

上面的代碼是否正確,或者是剪切和粘貼時最後幾行是否重複。第6行IsSMTP(); //通過SMTP發送,應該被刪除 – bumperbox 2014-09-06 07:02:39

回答

1

我可以使用PHP最後發送郵件。這裏是代碼:

<?php 

require_once('class.phpmailer.php'); 
include("class.smtp.php"); 

$mail = new PHPMailer(); 
$mail->IsSMTP(); 
$mail->CharSet="UTF-8"; 
$mail->SMTPSecure = 'tls'; 
$mail->Host = 'smtp.gmail.com'; 
$mail->Port = 587; 
$mail->Username = '[email protected]'; 
$mail->Password = 'sender_password'; 
$mail->SMTPAuth = true; 

$mail->From = '[email protected]'; 
$mail->FromName = 'sender'; 
$mail->AddAddress("[email protected]"); 
$mail->AddReplyTo("[email protected]", 'Information'); 

$mail->IsHTML(true); 
$mail->Subject = "Sample exmple to check proper working of mail function"; 
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; 
$mail->Body = "Hello "; 
$path = $_POST['upload']; 
$mail->AddAttachment($path); // attachment 
if(!$mail->Send()) 
{ 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} 
else 
{ 
    echo "Message sent!"; 
} 
?> 
+0

不錯的解決方案!爲我工作:) – 2015-05-27 17:39:49

0
<?php 

$mail = new PHPMailer(); 
$mail->IsSMTP(); // send via SMTP 

// Comment out this line here it is wrong 
// IsSMTP(); // send via SMTP 

$mail->SMTPAuth = true; // turn on SMTP authentication 
$mail->Username = "[email protected]"; // SMTP username 
$mail->Password = "password"; // SMTP password 
$webmaster_email = "[email protected]"; //Reply to this email ID 
$email = "[email protected]"; // Recipients email ID 
$name = "name"; // Recipient's name 
$mail->From = $webmaster_email; 
$mail->FromName = "Webmaster"; 
$mail->AddAddress($email, $name); 
$mail->AddReplyTo($webmaster_email, "Webmaster"); 
$mail->WordWrap = 50; // set word wrap 


// i would also comment out these lines, get it working without attachments first 
// then add then back in after (if you want attachments) 
// $mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment 
// $mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment 


$mail->IsHTML(true); // send as HTML 
$mail->Subject = "This is the subject"; 
$mail->Body = "Hi, 
This is the HTML BODY "; //HTML Body 
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body 
if (!$mail->Send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
    echo "Message has been sent"; 
} 

// at the end of the pasted code above, you have these lines (below here) doubled up. 
// remove them 

echo "Mailer Error: " . $mail->ErrorInfo; 
} 
else 
{ 
echo "Message has been sent"; 
} 
?> 
+0

什麼都應填寫在下面的字段? $ mail->用戶名=「[email protected]」; // SMTP用戶名 $ mail-> Password =「password」; // SMTP密碼 $ webmaster_email =「[email protected]」; //回覆此電子郵件ID $ email =「[email protected]」; //收件人電子郵件ID 意味着我應該在哪裏指定發件人的用戶名和收件人的用戶名?先生,我有點困惑。 – 2014-09-06 07:26:52

+0

他們應該設置爲您的電子郵件服務器的用戶名和密碼。 – bumperbox 2014-09-06 07:44:43

+0

先生您的電子郵件服務器是什麼意思? – 2014-09-06 08:46:47