Zotoaster
Zotoaster

Reputation: 393

Sending emails with PHPMailer

I'm struggling to understand how to send emails with PHPMailer.

I have multiple website clients and I have implemented a contact form for each of them. I can send emails to my own email address but not to theirs.

This is the function I wrote to do it.

function sendSMTP($host, $usr, $pwd, $port, $fromAddr, $fromName, 
$replyAddr, $replyName, $addr, $subject, $body)
{
$mail = new PHPMailer(true);

$mail->SMTPDebug  = 1; 

$mail->isSMTP();
$mail->Host         = $host;
$mail->SMTPAuth     = true;
$mail->Username     = $usr;
$mail->Password     = $pwd;
$mail->SMTPSecure   = 'ssl';
$mail->Port         = $port;

$mail->setFrom($fromAddr, $fromName);
$mail->addReplyTo($replyAddr, $replyName);

$mail->addAddress($addr);

$mail->isHTML(true);

$mail->Subject = $subject;
$mail->Body    = $body;
}

I have a feeling it's not working because of something to do with the first 4 parameters (host, user, password, port). I started this months ago and sat on it and now I'm totally lost on how to fix it.

Do I have to authenticate various sender accounts for each client? I'm stuck. What is the correct way to use this function?

Upvotes: 2

Views: 9120

Answers (1)

Radhika Choudhary
Radhika Choudhary

Reputation: 71

I successfully send email to my or other email address from my localhost using PHPMailer. if using gmail set HOST = smtp.gmail.com, set your email address and password in USER, PASSWORD. I found the PORT number by google 'gmail port number'. Hope it will be Helpful.

<?php

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;
$mail->isSMTP();                            // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';              // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                     // Enable SMTP authentication
$mail->Username = '[email protected]'; // your email id
$mail->Password = 'password'; // your password
$mail->SMTPSecure = 'tls';                  
$mail->Port = 587;     //587 is used for Outgoing Mail (SMTP) Server.
$mail->setFrom('[email protected]', 'Name');
$mail->addAddress('[email protected]');   // Add a recipient
$mail->isHTML(true);  // Set email format to HTML

$bodyContent = '<h1>HeY!,</h1>';
$bodyContent .= '<p>This is a email that Radhika send you From LocalHost using PHPMailer</p>';
$mail->Subject = 'Email from Localhost by Radhika';
$mail->Body    = $bodyContent;
if(!$mail->send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}

?>

Upvotes: 4

Related Questions