Dear all,

I have done a website that I need to integrate emailing functionality.
I have done this before, and it worked perfectly, and still does.
However, when I attempted to use the same code to test the emailing functionality for the new site, I get an error message as below;

"Mailer Error: The following From address failed: recipient@gmail.com : Called Mail() without being connected"

Here is the code that I am using.

<?php
require("PHPMailer_5.2.4/class.phpmailer.php");
    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->Host = "localhost";
    $mail->From = $_POST['email'];
    $mail->FromName  =  $_POST['sender_name'];
    $email_add = $_POST['email'];
    $phone_number = $_POST['phone_number'];
    $name = $_POST['name'];
    $subject = $_POST['subject'];
    $msg = $_POST['email_message']."\n".$name.".";

    $mail->AddBCC("cc_recepient@gmail.com");
    $mail->AddAddress("recepient@gmail.com");

    $mail->SMTPAuth = "true";
    $mail->Username = "my_gmail@gmail.com";
    $mail->Password =  "my_gmail_password";
    $mail->Port  =  "25";

    $mail->name = $name;
    $mail->phone_number = $phone_number;
    $mail->Subject = $subject;
    $mail->Body = $msg;
    $mail->WordWrap = 50;

    if(!$mail->Send())
    {
echo "<script language='javascript'>
alert('Your email was NOT sent! Please try again.')
</script>
<script>
window.location='contact_us.php'
</script>";
    }
    else
    {
    echo "<script language='javascript'>
alert('Mail Successfully sent! Thank you for contacting us.')
</script>
<script>
window.location='contact_us.php'
</script>";
    }

//  $email = $_REQUEST['email'] ;
//  $message = $_REQUEST['message'] ;

?>

I added the line;

echo 'Mailer Error: ' . $mail->ErrorInfo;

to identify the problem, and it shows me the error mentioned above.
I dunno what I am doing wrong since the same code is working fine elsewhere.

What type of SMTPSecure you add into the email prefix? And the port number would need to be matched with the SMTPSecure type (25 is not really what gmail currently used to authenticate because it could be blocked). The port number for SSL is 465 and TSL is 587. Also, does the email account require 2-step authentications? Are you sure that the password is correct? It looks like the failure is right at authenticating the sender account. You could check this link for further info on how to email on PHP.

Thank you @Taywin.
I am going to try that out and will let you know the results.

Meanwhile, out of curiosity, how come the emailing that I hosted on the testing server still work perfectly, using the above code?

So, I followed the tutorial, and still did not solve my problem, until I decided to check whether the ports are open.
It turns out all the ports have been blocked on the webmail server.
I think I now know what the problem is.

Will submit a report on whatever the results will be after unblocking the ports.

Thanx guyz.

you seem to use GMail SMTP but the code below uses your host machine (which am sure is not google's SMTP servers

$mail->Host = "localhost";

So change it to google SMTP. here is example from their site (simplified)

$mail = new PHPMailer();
$body = "Your Body here!";
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "tls";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 587;                   // set the SMTP port for the GMAIL server
$mail->Username   = "yourusername@gmail.com";  // GMAIL username
$mail->Password   = "yourpassword";            // GMAIL password

$mail->SetFrom('name@yourdomain.com', 'First Last');
$mail->AddReplyTo("name@yourdomain.com","First Last");
$mail->Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");
//optional
$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

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

@Mtangoo, I am using 'localhost', coz that is what is working on the hosted testing site.
It is working perfectly with that as the host.
Meanwhile, after alot of huffing, puffing and digging, I realised that all the ports 25, 463 and 587 are all blocked. This means that PHPMailer can't use any of those ports to send the emails.
So, I learnt that they are using formmail instead.

Thank you all for the input.

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.