I'm trying to get an email class based on PHP's PEAR library to work, but I constantly stumble across this message: Fatal error: Call to a member function send() on a non-object in /PATH/email.php on line 42. I've tried a few different things and they haven't resolved the problem in the following code:
<?php
require_once 'Mail.php';
require_once './config/config.php';
class Email
{
var $smtp_host;
var $smtp_port;
var $smtp_user;
var $smtp_pass;
var $from;
var $smtp_conn;
function Email($init_smtp_host=SMTP_HOST, $init_smtp_port=SMTP_PORT, $init_smtp_user=SMTP_USER, $init_smtp_pass=SMTP_PASS, $init_from_name='admin', $init_from_email=SMTP_USER)
{
$smtp_host = $init_smtp_host;
$smtp_port = $init_smtp_port;
$smtp_user = $init_smtp_user;
$smtp_pass = $init_smtp_pass;
$from = $init_from_name . ' <' . $init_from_email . '>';
$smtp_conn =& Mail::factory('smtp',
array( 'host' => $this->smtp_host,
'port' => $this->smtp_port,
'auth' => TRUE,
'username' => $this->smtp_user,
'password' => $this->smtp_pass));
}
function send($to_name, $to_email, $subject, $message)
{
$to = $to_name . ' <' . $to_email . '>';
$header = array('From' => $this->from, 'To' => $to, 'Subject' => $subject);
$mail = $this->smtp_conn->send($to, $header, $message);
if (PEAR::isError($mail))
return $mail->getMessage();
else
return 0;
}
}
Am I doing something wrong on $mail = $this->smtp_conn->send($to, $header, $message); (where the error is being displayed)?
Thanks
What's the type of the $smtp_conn? If the factory fails, it'd return something other than a Mail object, like perhaps a PEAR::Error.
Related
I am trying to get an email form working but it keeps erroring out.
My contact.php code looks like this:
<?php
ini_set("include_path", '/home4/hipsiguy/php:' . ini_get("include_path") );
require_once "Mail.php";
$username = 'my#emailaddress.ca';
$password = 'myemailpassword';
$smtpHost = 'mail.myemailserver.ca';
$smtpPort = '465';
$to = 'my#emailaddress.ca';
$from = 'my#emailaddress.ca';
$subject = 'Contact Form';
$successMessage = 'Message successfully sent!';
$replyTo = $_POST['your-email'];
$name = $_POST['your-name'];
$body = $_POST['your-message'];
$headers = array(
'From' => $name . " <" . $from . ">",
'Reply-To' => $name . " <" . $replyTo . ">",
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => $smtpHost,
'port' => $smtpPort,
'auth' => true,
'username' => $username,
'password' => $password
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo($mail->getMessage());
} else {
echo($successMessage);
}
?>
I host with HostGator (if that matters) and have installed the Mail and Net_SMTP extensions through CPanel.
I have tried a couple different methods, all of which error out.
The errors below seem to be the most common.
[16-Sep-2021 08:27:00 America/Chicago] PHP Warning: require_once(Mail.php): failed to open stream: No such file or directory in /home4/hipsiguy/public_html/TEST/contact.php on line 30
[16-Sep-2021 08:27:00 America/Chicago] PHP Fatal error: require_once(): Failed opening required 'Mail.php' (include_path='.:/opt/cpanel/ea-php74/root/usr/share/pear:/usr/lib/pear') in /home4/hipsiguy/public_html/TEST/contact.php on line 30
What am I missing?
I have PHP Send Email using SMTP.
$from = "Info <donotreply#test.com>";
$subject = "Calibration will be expiring!";
$body = 'Hello';
$to = "david#test.com, dono#test.com";
$cc = "rena#test.com";
$bcc = ""; //sometimes BCC can be empty
$host = 'smtp.test.com';
$port = '587';
$username = 'donotreply#test.com';
$password = 'dontreply?';
$headers = array(
'Port' => $port,
'From' => $from,
'To' => $to,
'Subject' => $subject,
'Content-Type' => 'text/html; charset=UTF-8',
'Cc' => $cc
);
$recipients = $to.", ".$bcc.", ".$cc;
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($recipients, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
I tried to run above code and only working if $bcc is not empty.
Error when $bcc is empty
Failed to add recipient: #localhost [SMTP: Invalid response code received from server (code: 501, response: 5.1.3 Invalid address [HKXPR02MB0695.apcprd02.prod.outlook.com])]
Is there any way how to fix it or need some modification there?
If $bcc variable is empty then your concatenated string contains ,, which is syntactically incorrect. You need to add some logic there to make ensure this is not happening.
Also I doubt you set BCC correctly anyway. It should not be just another recipient like you have now.
I've got the solution below:
$bcc = $dEmailListBCC['EMAIL_ADDRESS'];
if($bcc == "")
{
$a = "";
}
else
{
$a = ",".$bcc;
}
First, check the $bcc variable is empty or not.
and then the recipients to be like this:
$recipients = $to." ".$a.", ".$cc;
Good day! I have tried and search everything I can find.. but to no avail, code is still not working, and I'm not even getting any error message. I have no problem with my variables, it is in the "mail" part i think. Please help.
require "Mail.php";
$from = 'angelsofmaryacademy#gmail.com';
$to = $hidden1;
$subject = $hidden2;
$body = $_POST['replytomsg'];
$host = "smtp.gmail.com";
$port = "465";
$user = "angelsofmaryacademy#gmail.com";
$pass = "Ang3lsofmaryacademy";
$headers = array ("From" => $from, "To" => $to, "Subject" => $subject);
$smtp = #Mail::factory("smtp",array(
"host" => $host,
"port" => $port,
"auth" =>true,
"username" => $user,
"password" => $pass));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo ('<p>'. $mail->getMessage() . '</p>');
}
else {
echo ('<p>Message successfully sent! </p>');
}
}
I have a script I run to send emails with attachments.
On my old server it worked perfectly, But since moving a few days back. The script no longer works. As soon as it is supposed to send a mail, it bombs out.
I did find this error in the error log file,How can I locate the problem?
[08-Jul-2015 12:39:17 UTC] PHP Fatal error: Call to undefined function mime_content_type() in /home/username/public_html/editor/cronjob.php on line 79
My code more or less looks like the below:
function sendmailwithattachment($from,$to,$subject,$body,$host,$username,$password,$body_id,$port) {
$query = "SELECT * FROM `email_attachments` where `email_id`='$body_id';";
if($result = mysqli_query($this->link, $query)){
if($result->num_rows!=0){
$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);
$crlf = "\n";
$mime = new Mail_mime($crlf);
$mime->setHTMLBody($body);
while ($row = mysqli_fetch_array($result,1)) {
$attachment=$row['attachment'];
$mime->addAttachment($attachment, mime_content_type($attachment));
}
$body = $mime->get();
$headers = $mime->headers($headers);
} else {
$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject,'Content-type' => 'text/html;charset=iso-8859-1');
$body = $body;
}
}
$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>Message not sent:".$mail->getMessage()."</p>";
return $mail->getMessage();
} else {
//echo "<p>Message successfully sent!</p>";
return 'sent';
}
}
It looks like mime_content_type() is deprecated anyway, try replacing it with Fileinfo functions as they mention in the docs. It might solve your problem in the process (though hard to say, because we don't know what your code looks like).
I have a PHP function to send emails,
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, $cc);
}
}
when i call the function, sometimes there is no $cc variable so i get a warning saying that Missing argument 6 for sendemail(),
whats the best way to stop the warning if $cc is not valid?
If you wrote that function, you can make the 6th parameter optional:
function sendemail($email_to, $email_from, $email_subject, $email_body, $email_replyto, $cc = null) {
if ($cc !== null) {
// add cc headers, e.g.
// $headers['Cc'] = $cc;
}
}
You will then have the option to omit this parameter:
sendemail("to#example.com", "from#example.com", "subject", "body", "replyto#example.com");
sendemail("to#example.com", "from#example.com", "subject", "body", "replyto#example.com", "cc#example.com");
Use this
function sendemail($email_to,$email_from,$email_subject,$email_body,$email_replyto,$cc = "")
Try this,
function sendemail($email_to,$email_from,$email_subject,$email_body,$email_replyto,$cc=NULL)
put $cc = NULL.
So you will not get warning if there is no $cc .
If you are able to change the send email function:
function sendemail ($email_to, $email_from, $email_subject, $email_body, $email_replyto, $cc=null) { }
Just make sure that the function body itself will not have problem with a null $cc.