Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Send mail error

Tags:

php

phpmailer

Please help me I am New in PHP and since last 5 Hours i am try to semd mail and now really tired. Thanks.

Here is my code. I am using Gmail account.

include("class.phpmailer.php");
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = $mail->getFile('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP();
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server

$mail->Username   = "[email protected]";  // GMAIL username
$mail->Password   = "xxxxxxxx";            // GMAIL password

$mail->AddReplyTo("[email protected]","First Last");

$mail->From       = "[email protected]";
$mail->FromName   = "First Last";

$mail->Subject    = "PHPMailer Test Subject via gmail";

//$mail->Body       = "Hi,<br>This is the HTML BODY<br>";                      //HTML Body
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap   = 50; // set word wrap

$mail->MsgHTML($body);

$mail->AddAddress("[email protected]", "John Doe");

$mail->AddAttachment("images/phpmailer.gif");             // attachment

$mail->IsHTML(true); // send as HTML

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

When I run my file I get This Error:

Warning: fopen(contents.html) [function.fopen]: failed to open stream: No such file or directory in D:\xampplite\htdocs\WebEng\class.phpmailer.php on line 1870

Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465 (Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?) in D:\xampplite\htdocs\WebEng\class.smtp.php on line 122 Mailer Error: SMTP Error: Could not connect to SMTP host.

like image 762
Talha Bin Shakir Avatar asked Feb 28 '23 20:02

Talha Bin Shakir


1 Answers

First of all when you get error messages then that's great! Because in 90% of the cases you find that others have had them too and therefore you'll find plenty of information on the internet about this error message.

So step 1 when getting an error message you don't know yet is always open google and copy paste it there. But, take out any paths or other things which are uniquely connected to your system.

Then about your errors. Especially xampp light doesn't support SSL. Maybe you try an easier sendmail example first. Like a very small one and then increase it step by step. That's what I always do, when I don't know why something doesn't work. I start with one line and see what it does, then I add another and so forth.

Let's say you start with this and see if it works:

<?php

include("class.phpmailer.php");

$mail             = new PHPMailer();

$mail->IsSMTP();
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "[email protected]";  // GMAIL username
$mail->Password   = "xxxxxxxx";            // GMAIL password

$mail->From       = "[email protected]";
$mail->Subject    = "PHPMailer Test Subject via gmail";
$mail->Body       = "Hi, this is a test";           
$mail->AddAddress("[email protected]", "Hussain");

$mail->send();
?>

oh, and btw. your mail from has a .com too many!

like image 65
markus Avatar answered Mar 09 '23 00:03

markus