when I try to send mail with php I get this error:
Fatal error: Class 'Mail' not found in C:\Program Files (x86)
\Apache Software Foundation\Apache2.2\htdocs\pn\mailtest.php
on line 10
Here is my PHP code:
<?php
$from="sjoseph#seisystems.com";
$to="dianahelene#gmail.com";
$subject="testmail";
$host="Sei-mail01";
$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => false));
$mail = $smtp->send($to, $headers, $message);
?>
Why is
You need to load the Mail class with require_once after the <?php tags:
require_once 'Mail.php';
Related
I am trying to send Mail through PHP Pear Mail package, I have 2 scenarios here , In first scenario mail is sending properly without any error but in second scenario I am getting error message .
Scenario 1 :
File Name : testmail.php
<?php
require_once "Mail.php";
$from = "erp#askspidy.com";
$to = "support#askspidy.com";
$subject = "Landing Page Enquiry From -";
$body = "Hello just testing";
$host = "ssl://mail.askspidy.com";
$port = "465";
$username = "support#askspidy.com";
$password = "askspidy#1234";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
'MIME-Version' => 1,
'Content-type' => 'text/html;charset=iso-8859-1'
);
$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>");
}
?>
If I directly run file in url then mail is sent without any errors .
Scenario 2 :
File Name : sendmail.php
<?php
function send_mail($subject,$body)
{
require_once "Mail.php";
$from = "erp#askspidy.com";
$to = "support#askspidy.com";
$host = "ssl://mail.askspidy.com";
$port = "465";
$username = "support#askspidy.com";
$password = "askspidy#1234";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
'MIME-Version' => 1,
'Content-type' => 'text/html;charset=iso-8859-1'
);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
}
?>
Now i am calling this send_mail function from different file like this
File Name : service/process.php
<?php
require_once("../sendmail.php");
$subject="Landing page enquiry";
$email_body="Hello Just Testing! ";
send_mail($subject,$email_body);
?>
When this file is executed into browser , i am getting error message on line
send_mail($subject,$email_body);
Error :
Warning: include_once(Net/SMTP.php): failed to open stream: No such file or directory in /usr/local/lib/php/Mail/smtp.php on line 348
Warning: include_once(): Failed opening 'Net/SMTP.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /usr/local/lib/php/Mail/smtp.php on line 348
Fatal error: Class 'Net_SMTP' not found in /usr/local/lib/php/Mail/smtp.php on line 349
In Scenario 1 everything is working fine then why in scenario 2 I am getting this error, i guess there is problem with path but i am not sure what should be the path and where should i include that.
Folder Structure :
Include code in file Mail/smtp.php
require_once "Net/SMTP.php";
Note: I have manually installed PEAR Package in Cpanel account and done no settings in php.ini file
Added dirname(FILE) to the path name everywhere in code and it worked
require_once dirname(__FILE__)."/Net/SMTP.php";
You can use __DIR__ for the directory path for current file
require_once __DIR__ . "/Net/SMTP.php";
I need my php script to email me some variables which is parsed to it.
I receive the email properly when any text is passed:
mypage.php?url=hello
But I do not receive the email if an url is passed:
mypage.php?url=http://www.google.com
I searched for hours but didn't found any way to do it.
This is the php script:
<?php
// Pear Mail Library
require_once "Mail.php";
$name = $_POST['name'];
$url = $_POST['url'];
$from = '<noreply#website.com>';
$to = '<me#gmail.com>';
$subject = 'Hi!';
$body = '
Name: '.$name.'
The url is: '.$url.'
';
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'mail#gmail.com',
'password' => 'password'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo('<p>' . $mail->getMessage() . '</p>');
} else {
echo('<h1>Message successfully sent!</h1>');
}
?>
When an actual url is parsed, I don't recive the email but also no error is shown.
As #Xorifelse said, I used addslashes function and it worked. I got the email properly with the url in it.
Below is the working script:
<?php
// Pear Mail Library
require_once "Mail.php";
$name = $_POST['name'];
$url = $_POST['url'];
$from = '<noreply#website.com>';
$to = '<me#gmail.com>';
$subject = 'Hi!';
$body = '
Name: '.addslashes($name).'
The url is: '.addslashes($url).'
';
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'mail#gmail.com',
'password' => 'password'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo('<p>' . $mail->getMessage() . '</p>');
} else {
echo('<h1>Message successfully sent!</h1>');
}
?>
I have 2 pages:
automate.php
checkemail.php
on automate.php is:
<?php include 'scripts/checkemail.php'; ?>
and:
<?php
if(!function_exists("sendemail"))
{
function sendemail($email_to,$email_from,$email_subject,$email_body,$email_replyto,$cc='')
{
if(filter_var($email_to, FILTER_VALIDATE_EMAIL))
{
require_once "/usr/local/lib/php/Mail.php";
$from = $email_from;
$to = $email_to;
$subject = $email_subject;
$body = $email_body;
$host = "mail.domain.co.uk";
$username = "sending#domain.co.uk";
$password = "*********";
$headers = array ('From' => $from,
'To' => $to,
'Cc' => $cc,
'Subject' => $subject,
'Content-type' => 'text/html');
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$rec = $to.', '.$cc;
$mail = $smtp->send($rec, $headers, $body);
}
}
}
?>
then on checkemail.php i am calling this function:
sendemail("techsupport#domain.co.uk","Support <no-reply#domain.co.uk>","Contact Not Found",$contact_not_found_email,"no-reply#domain.co.uk");
but i am getting this error:
Fatal error: Call to undefined function sendemail() in /home/integra/public_html/automate/scripts/checkemail.php on line 180
The problem is sendemail() is still not defined when you run include.
This is because the if(!function_exists("sendemail")) block will only execute AFTER the include, and thus, the function will not exist.
I am using default PHP mail() function to send emails as below . I have two email servers. How can I set my code to use these two mail server? I am running my PHP code on Linux.
mail($currEmail, $HOT_EMAIL_SUBJECT, $body, 'From: '.$pollsConfig_senderEmail);
Use PEAR::Mail, you can specify a SMTP server with it.
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$obj = Mail::factory ('smtp',
array ('host' => $host,
'port' => $port));
$obj->send ($to, $headers, $body);
I am trying to edit PHP code that sends an email using PEAR. The below code has worked on the company server, but doesn't seem to work on my computer when I am using localhost.
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
//Send the Actual Mail
$smtp = Mail::factory('smtp',
array ( 'host' => $host,
'auth' => false,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail))
{
echo($mail->getMessage());
}
else
{
echo("<p>Message successfully sent!<p>");
}
The items in $headers are defined earlier, don't worry.
The error returned by echo($mail->getMessage()); is as follows:
Failed to connect to localhost:25 [SMTP: Failed to connect socket: Connection refused (code: -1, response: )]
I'm on a machine running Ubuntu. Not sure if any other information is needed.