pear fatal Error: Class 'Mail' not found .wamp - php

I am having some trouble with using the send mail functionality of pear with wamp. I went through with the steps in this link : (http://pear.php.net/manual/en/installation.checking.php) to check if my pear was installed correctly, and it seems like I have it right.
<?php
require_once 'System.php';
var_dump(class_exists('System', false));
?>
The code above returns bool(true). So I am assuming that my paths are set right.
But for the code below I am getting an error.
<?php
include 'Mail.php';
include 'Mail/mime.php' ;
$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = 'test.xls';
$crlf = "\n";
$hdrs = array(
'From' => 'myemail#gmail.com',
'Subject' => 'Test mime message'
);
$mime = new Mail_mime(array('eol' => $crlf));
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain');
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail =& Mail::factory('mail');
$mail->send('myemail2#gmail.com', $hdrs, $body);
?>
Error is on this line : $mail =& Mail::factory('mail'); . Fatal error: Class 'Mail' not found
Also, I installed pear Mail with this command : pear install Mail Mail_mime
I would appreciate any help.
Thanks,

This one works for me, try this way
function sendEmail($subject,$from,$to,$bodymsg,$cc=null)
{
require_once "Mail.php";
require_once "Mail/mime.php";
$crlf = "\n";
$headers = array('From' => $from,
'To' => $to,
'Subject' => $subject);
//$host = "smtp.gmail.com";
$host = "ssl://smtp.gmail.com"; // try this one to use ssl
$port = 465;
$username = "myusername"; //<> give errors
$password = "mypass";
//$mime = new Mail_mime($crlf);
$mime = new Mail_mime(array('eol' => $crlf)); //based on pear doc
$mime->setHTMLBody($bodymsg);
//$body = $mime->get();
$body = $mime->getMessageBody(); //based on pear doc above
$headers = $mime->headers($headers);
$smtp = Mail::factory("smtp",array("host" => $host,
"port" => $port,
"auth" => true,
"username" => $username,
"password" => $password), '-f bounce#domain.com');
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo $mail->getMessage();
} else {
echo "Message sent successfully!";
}
echo "\n
}

You need to either specify the full path to the PEAR Mail package that you've installed (instead of include 'Mail.php';), or include that path in php.ini's include_path.
Also update php.ini with your mail server's address and port... Since you are using Mail's send driver "mail", which is PHP's mail() function. Though you can specify it to use sendmail or an SMTP server instead.

Related

Error in send email by smtp use PEAR Mail package in php

I am using this code to send message by SMTP and have below error:
What is the problem?
My IMAP is on and username and password are correct and apps access to gmail is on
<?php
include 'Mail.php';
include 'Mail/mime.php' ;
$from = "Robert Davis <mymail#gmail.com>";
$to = "Sam Hill <receiver>";
$subject = 'Test mime message with an attachment';
$headers = array ('From' => $from,'To' => $to, 'Subject' => $subject);
$text = 'Text version of email';// text and html versions of email.
$html = '<html><body>HTML version of email. <strong>This should be bold</strong></body> </html>';
$file = './sample.txt'; // attachment
$crlf = "\n";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain');
//do not ever try to call these lines in reverse order
$body = $mime->get();
$headers = $mime->headers($headers);
$host = "smtp.gmail.com";
$username = "mymail";
$password = "mypass";
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true 'username' => $username,'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
}
else {
echo("<p>Message successfully sent!</p>");
}
?>
This is error that i see:
authentication failure [SMTP: Invalid response code received from server (code: 534, response: 5.7.14 Please log in via your web browser and 5.7.14 then try again. 5.7.14 Learn more at 5.7.14 https://support.google.com/mail/answer/78754 x67sm2291331iod.22 - gsmtp)]

The CC email will not send using PEAR

I have read the similar posts about this problem but can't quite get my head around what I am doing wrong here. The 'To' email is received ok.
However, I can not get the 'CC' email to send when using the following script.
Any help would be much appreciated.
// Require Pear Mail Packages
require_once ("Mail.php");
require_once ("Mail/mime.php");
require_once ("Mail/mail.php");
$crlf = "\n";
$from = "Company <admin#example.com>";
$to = $purchasersName . "<" . $purchasersEmail . ">";
$cc = "Company <admin#example.com>";
$subject = "Company Order Confirmation";
$host = "localhost";
$port = "25";
$username = "the username";
$password = "the password";
$hdrs = array ('From' => $from,
'To' => $to,
'Cc' => $cc,
'Subject' => $subject);
$mime = new Mail_mime(array('eol' => $crlf));
$mime->setHTMLBody($mailBody);
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$smtp =& Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $hdrs, $body);
This comment is in the pear docs on this page
http://pear.php.net/manual/en/package.mail.mail.send.php
(If you want other options, you could try http://swiftmailer.org, I have used that on lots of projects now)
Note by: arminfrey#gmail.com 2007-07-08 05:13 UTC In order to send
e-mail to cc or bcc with smtp you have to list the cc e-mail address
both as a recipient (which decides where the e-mail is sent) and in
the cc header, which tells the mail client how to display it.
Here is the code I use:
$to = 'to#example.com'; $cc = 'cc#example.com';
$recipients = $to.", ".$cc;
$headers['From'] = 'from#example.com'; $headers['To'] = $to;
$headers['Subject'] = 'Test message'; $headers['Cc'] =
'cc#example.com'; $headers['Reply-To'] = 'from#example.com';
$send = $mail->send($recipients, $headers, $body);

Missing attachement in mail sent through Mime / SMTP Gmail Server (PHP, PEAR)

I am sending an Email from my server through SMTP Gmail using Pear's Mail Mime. However when I add an attachement it simply does not show up.
$smtpinfo["host"] = "ssl://smtp.gmail.com";
$smtpinfo["port"] = "465";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "xxx";
$smtpinfo["password"] = "xxx";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => utf8_decode($subject)
);
$mime = new Mail_mime();
$mime->setHTMLBody($html);
$mime->addAttachment("http://ww.url.of.a.file.that.exists.100percent.jpg", "image/jpeg");
$body = $mime->get(array('html_charset' => 'utf-8','charset' => 'utf-8'));
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp', $smtpinfo);
$mail = $smtp->send($to, $headers, $body);
Everything works fine just the attachement is entirely missing..
I've been googling for hours.. I appreciate any hints..
My first thoughts would be to check the [boolean] response of the addAttachment method to see if it returns 'not found' or other type of indication(s)
$fileAttached = $mime->addAttachment("http://ww.url.of.a.file.that.exists.100percent.jpg", "image/jpeg");
echo ( !empty($fileAttached) ) ? "Attached successfully!" : "Uh, Houston?";
My initial thought is that it's expecting that 'file' to be 'loca' to your system and not accessed via http, etc. And, even if it DOES allow for HTTP access, you might also want to check your allow_url_fopen in the .ini file to insure it's set to 'enabled' {"On" if looking at your phpinfo() result.
Additional information on the 'file' -
http://pear.php.net/manual/en/package.mail.mail-mime.addattachment.php

How to send an email using php script?

<?php
require_once "Mail.php";
$from = "<niko#gmail.com>";
$to = "<niko#hotmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "<niko#gmail.com>";
$password = "somepassworrd";
$headers = array ('From' => $from,'To' => $to,'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' => $host,'port' => $port,'auth' =>true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail))
echo("<p>" . $mail->getMessage() . "</p>");
else
echo("<p>Message successfully sent!</p>");
?>
When I try to execute these php script I get these error
FATAL ERROR Class Mail NOT found ON number line 18
I know the above question is possible duplicate of these
Send email using the GMail SMTP server from a PHP page
But its not working.My PHP version is 5.3.4 Xampp 1.7.4 version.
mail('caffeinated#example.com', 'My Subject', $message); \\ I tried these
But it shows a warning saying that missing headers.
How do I send an email Using the php script?
Can we send an email without using authentication in PHP ? Because vb.net uses server authentication but most of the code i found on google is without authentication for php. so I got a doubt
Finally please help me with these trying from an 2 hours or so!
The PHP mail() function is for sending mail via Sendmail. If you want to use some SMTP server, you can use Zend_Mail which makes this thing very easy:
http://framework.zend.com/manual/en/zend.mail.html
Using Zend_Mail you only need to write something like this:
$config = array('auth' => 'login',
'username' => 'myusername',
'password' => 'password');
$transport = new Zend_Mail_Transport_Smtp('mail.server.com', $config);
$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('sender#test.com', 'Some Sender');
$mail->addTo('recipient#test.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send($transport);
The above handles authentication for you and sends a mail.

How to use pear mail mime

How can you use pear mail mime with google. I found this which lets you use pear mail with google, but not mail mime: http://globalconstant.scnay.com/2009/11/06/sending-email-through-gmail-using-php/
require_once "Mail.php";
require_once "Mail/mime.php";
$from = "Sender <*******#googlemail.com>";
$to = "Receiver <*******#googlemail.com>";
$subject = "Welcome to SITENAME!";
$crlf = "\n";
$html = "<h1> This is HTML </h1>";
$headers = array('From' => $from,
'To' => $to,
'Subject' => $subject);
$host = "smtp.gmail.com";
$port = 465;
$username = "********#googlemail.com";
$password = "********";
$mime = new Mail_mime($crlf);
$mime->setHTMLBody($html);
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory("smtp",array("host" => $host,
"port" => $port,
"auth" => true,
"username" => $username,
"password" => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo $mail->getMessage();
} else {
echo "Message sent successfully!";
}
echo "\n";
I keep getting
Failed to add recipient: #localhost
[SMTP: Invalid response code received
from server (code: 555, response:
5.5.2 Syntax error. f52sm5542930wes.35)]
Edit:
The email is now received, however it turns out like this:
This is a message I sent from PHP using=
the PEAR Mail package and SMTP through Gmail. Enjoy!
It looks like you have an issue with the email header. I updated your code based on the pear mail doc (http://pear.php.net/manual/en/package.mail.mail-mime.example.php):
require_once "Mail.php";
require_once "Mail/mime.php";
$from = "Sender <*******#googlemail.com>";
$to = "Receiver <*******#googlemail.com>";
$subject = "Welcome to SITENAME!";
$crlf = "\n";
$html = "<h1> This is HTML </h1>";
$headers = array('From' => $from,
'To' => $to,
'Subject' => $subject);
//$host = "smtp.gmail.com";
$host = "ssl://smtp.gmail.com"; // try this one to use ssl
$port = 465;
$username = "********#googlemail.com";
$password = "********";
//$mime = new Mail_mime($crlf);
$mime = new Mail_mime(array('eol' => $crlf)); //based on pear doc
$mime->setHTMLBody($html);
//$body = $mime->get();
$body = $mime->getMessageBody(); //based on pear doc above
$headers = $mime->headers($headers);
$smtp = Mail::factory("smtp",array("host" => $host,
"port" => $port,
"auth" => true,
"username" => $username,
"password" => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo $mail->getMessage();
} else {
echo "Message sent successfully!";
}
echo "\n";
It works for me so I hope it will work for you!
Cheers,
Erez
#john: Using the code from the link you posted, modify it like so --
<?php
require_once('Mail.php');
require_once('Mail/mime.php');
$from = 'Sender <sender#gmail.com>';
$to = 'Receiver <receiver#something.com>';
$subject = 'Sent from PHP on my machine';
$text = 'This is a message I sent from PHP '
. 'using the PEAR Mail package and SMTP through Gmail. Enjoy!';
$message = new Mail_mime();
$message->setTXTBody(strip_tags($text)); // for plain-text
$message->setHTMLBody($text);
$body = $message->get();
$host = 'smtp.gmail.com';
$port = 587; //According to Google you need to use 465 or 587
$username = 'sender';
$password = 'your_password';
$headers = array('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array(
'host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo $mail->getMessage();
} else {
echo "Message sent successfully!";
}
echo "\n";
?>
Update:
Edit:
The email is now received, however it turns out like this:
This is a message I sent from PHP using=
the PEAR Mail package and SMTP through Gmail. Enjoy!
#john: Update
$body = $mime->get();
to
$body = $mime->get(array('text_charset' => 'utf-8'));
and try again.
$body = $mime->get(array('text_charset' => 'utf-8'));
In addition to the above you need html_charset for html emails.
$crlf = "\n";
$body = $mime->get(array('html_charset' => 'utf-8', 'text_charset' => 'utf-8', 'eol' => $crlf));
This will fix the abberations like  in the e-mails.
Couldn't comment on StealthyNinja's answer so I posted my own, sorry about that.
The question is also a bit old but I maybe this could be useful to others.
To get rid of all that HTML tags and weird characters you have to prepare your header so the e-mail client can read the e-mail right. Try this AFTER setting your $headers array:
$headers = $message->headers($headers);
It should work ok after that.
I have used this code to remove 3D after = sign.
$hdrs = array( 'From' => $from,
'To' => $to,
'Subject' => $subject );
$mime =& new Mail_mime();
$mime->setTXTBody($message);
if($htmlMessage==""){
$htmlMessage=$message;
}
$mime->setHTMLBody($htmlMessage);
if($attachmentIsFile){
if($attachment!=null)
$mime->addAttachment($attachment,'application/octet-stream',$attachmentName.extractExtension($attachment));
}else{
if($attachment!="")
$mime->addAttachment($attachment,'application/octet-stream',$attachmentName,false);
}
$body = $mime->get(array('text_encoding' => '8bit','html_encoding' => '8bit'));
$hdrs = $mime->headers($hdrs);

Categories