2014-09-06 136 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