I want to send mails using gmail server with my gmail id and password. I am using the php mailer class for sending mails. Please help me to do this. my code is shown below

$mail             = new PHPMailer();
$body             = "Test mail Test mail";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug  = 1; 
$mail->SMTPSecure = 'tls';                    // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "smtp.gmail.com"; // sets the SMTP server
$mail->Port       = 587;                    // set the SMTP port for the GMAIL server
$mail->Username   = "test@gmail.com"; // SMTP account username
$mail->Password   = "password";        // SMTP account password

$mail->SetFrom('test@gmail.com', 'First Last');

$mail->AddReplyTo("test@gmail.com","First Last");

$mail->Subject    = "Test mail";

$mail->AltBody    = "Test mail"; // optional, comment out and test


$address = "myemail@gmail.com@gmail.com";
$mail->AddAddress($address, "Testing");

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";

Recommended Answers

All 6 Replies

yes.. i am getting the error
MTP -> ERROR: Failed to connect to server: Connection refused (111)
SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.

Tried using the port 465 and security ssl

Its already set..

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.