无法使用core php发送电子邮件


Unable to send email using core php

我是php新手。我正在尝试使用php发送电子邮件,但我不知道我的代码中出了什么问题。我在谷歌上搜索了很多,但都没用。这是我的php代码。我使用的是class.phpmailer.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 = "myemail@googlemail.com"; // SMTP username
$mail->Password = "mypassword"; // SMTP password
$webmaster_email = "recipient@googlemail.com"; //Reply to this email ID
$email="username@domain.com"; // 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";
}
?>

我终于可以使用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 = 'sender@mail.com';
$mail->Password = 'sender_password';
$mail->SMTPAuth = true;
$mail->From = 'sender@mail.com';
$mail->FromName = 'sender';
$mail->AddAddress("sender@mail.com");
$mail->AddReplyTo("sender@mail.com", '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!";
}
?>
<?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 = "username@gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "username@doamin.com"; //Reply to this email ID
$email = "username@domain.com"; // 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";
}
?>