I want to send an email from localhost but I don't know how to install mail server, so I do this way:

<?php
ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Gui mail su dung PHPmailer and gmail</title>
</head>

<body>
<?php 
require_once('PHPMailer/class.phpmailer.php'); 
?>
<?php
define('GUSER', 'tranlinh.nmt@gmail.com'); // I wonder here: is this my gmail account?
define('GPWD', '123456'); // is this password for this email that will be sent
function smtpmailer($to, $from, $from_name, $subject, $body) { 
    global $error;
    $mail = new PHPMailer();  // tạo một đối tượng mới từ class PHPMailer
    $mail->IsSMTP(); // bật chức năng SMTP
    $mail->SMTPDebug = 0;  // kiểm tra lỗi : 1 là  hiển thị lỗi và thông báo cho ta biết, 2 = chỉ thông báo lỗi
    $mail->SMTPAuth = true;  // bật chức năng đăng nhập vào SMTP này
    $mail->SMTPSecure = 'ssl'; // sử dụng giao thức SSL vì gmail bắt buộc dùng cái này
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 465; 
    $mail->Username = GUSER;  
    $mail->Password = GPWD;           
    $mail->SetFrom($from, $from_name);
    $mail->Subject = $subject;
    $mail->Body = $body;
    $mail->AddAddress($to);
    if(!$mail->Send()) {
        $error = 'sending error: '.$mail->ErrorInfo; 
        return false;
    } else {
        $error = 'Done';
        return true;
    }
}  
smtpmailer('tranlinh.fit@gmail.com', '', 'tranlinh.nmt@mail.com', 'tranlinh', 'subject', 'content'); 
if (smtpmailer) {
    echo "sending email DONE!";
}
if (!empty($error)) echo $error;  
?>
</body>
</html>

But it raises error.I don't know why
Please help me clarify this problem! Thanks a lot!

Member Avatar for rajarajan2017

Just give

$mail->Username = 'rajarajan06';
$mail->Password = 'test';

remove the define statements
My gmail id is look like: rajarajan06@gmail.com

No need to give your gmail address as a user name, bcoz you already gave the gmail smtp as smtp.gmail.com. so user name is enough instead of mail

Member Avatar for rajarajan2017

Also please post the error message shown

I have just tried to do like your suggestion but it also does not work properly.
Here is the error message: Invalid address: SMTP Error: Could not authenticate. sending email DONE!sending error: SMTP Error: Could not authenticate.
Should I need to change the smtp in the php.ini file because I see in this file the smtp port is 25 but in the PHPMailer it is 456.

Member Avatar for rajarajan2017

Please change the user name with your hosting name. like rajarajan06@gmail.com

Member Avatar for rajarajan2017

Finally try this one, It will work

$mail->Host ="ssl://smtp.gmail.com";
$mail->Port = 465;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.