missing arguments in PHP Function - how to stop the warning - php

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.

Related

PHP Send Email Using SMTP If BCC Empty Email Can't Send

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;

PEAR: Mail not working

This seems to not work. My webhost claims to have PEAR installed. Does anybody spot any mistakes? I do have every var/field correct. But yet, when this runs, it stops the loading of the rest of the php html page.
<?php
// Report all PHP errors
error_reporting(-1);
if (isset($_POST['email'])) {
require_once "Mail.php";
$from = "No Reply <redacted#redacted.redacted>";
$to = "redacted";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "redacted.redacted.redacted";
$username = "redacted#redacted.redacted";
$password = "---";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$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>");
}
}
?>

Email Cc not working

I'm trying to send email via PHP script. I can send successfully but no email arriving to the recipient under Cc field.. Is there something wrong with the headers?
function send_email($to,$subject,$body,$cc = ''){
require_once "Mail.php";
$from = "<removed>";
$host = "smtp.domain.com";
$username = "<removed>";
$password = "<removed>";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
'Cc' => $cc);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
$response["success"] = 0;
$response["message"] = $mail->getMessage();
echo json_encode($response);
}else {
$response["success"] = 1;
$response["message"] = "Email sent to " . $to;
echo json_encode($response);
}
}
If you use PHPMailer (from your comment), add the addresses like so:
$mail->addCC('somebodyelse#bla.com', 'somebody else');
$mail->addCC('otherguy#blabla.com', 'other somebody');
Or call the addCC method in a loop for your convenience.
I moved to PHPMailer and It is now working..
This is the working code
<?php
// array for JSON response
$response = array();
$to = $_POST['rcpt'];
$subject = $_POST['subject'];
$body = $_POST['msgBody'];
$cc = $_POST['cc'];
send_email($to,$subject,$body,$cc);
function send_email($to,$subject,$body,$cc = ''){
require_once "Mail.php";
$from = "monitor<monitor#m2techtronix.com>";
$host = "smtp.domain.com";
$username = "monitor#m2techtronix.com";
$password = "gUZfE6SVLV";
// $headers = array ('From' => $from,
// 'To' => $to,
// 'Subject' => $subject,
// 'Cc' => $cc);
$headers['From'] = $from;
$headers['To'] = $to.", ".$cc;
$headers['Subject'] = $subject;
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
$response["success"] = 0;
$response["message"] = $mail->getMessage();
echo json_encode($response);
}else {
$response["success"] = 1;
$response["message"] = "Email sent to " . $to;
echo json_encode($response);
}
}
?>
Required classes can be found here - https://github.com/romelemperado/PHPMailer
This is how I send a mail using PEAR's Mail package.
Note you also need to install the Mail/Mime package - which is trivial to do.
<?php
require_once 'Mail.php';
require_once 'Mail/mime.php';
$from = "from#example.com";
$msg = "this is a test";
$to = 'to#example.com';
$cc = 'cc#example.com';
$sbj = "Testing";
$message = new Mail_mime();
$message->setTXTBody($msg);
$message->setHTMLBody("<html><body>$msg</body></html>");
$body = $message->get();
$extraheaders = [
"From" => $from,
"Cc" => $cc,
"To" => $to,
"Subject" => $sbj
];
$mail = Mail::factory("mail");
$headers = $message->headers($extraheaders);
// In case you have more than one address in the $to variable.
$addresses = implode(",", [$to]);
if ($mail->send($addresses, $headers, $body)) {
echo "Successfully Sent";
} else {
echo "Message Failed";
}

Attaching file in the php mail code

This code is working fine in my unix shared hosting account but how do I attach file with it?
I've removed the hostname, login, passwd etc.
<?php
require_once "Mail.php";
$from = "contactus#site.com";
$to = "to email id";
$subject = "this is the message from your domain";
$body = "give your message body here";
$host = "mail.site.com";
$username = "user";
$password = "pass123";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) { echo("" . $mail->getMessage() . "");
} else {
echo("Message Sent successfully ");
}
?>
You have to include both PEAR::Mail and PEAR::Mail_Mime, as it takes both classes to get the full email sent. Example...
include_once('Mail.php');
include_once('Mail_Mime/mime.php');
// include_once('Mail/mime.php');
// The code below composes and sends the email
$message = new Mail_mime();
$message->setTXTBody($text);
$message->addAttachment($path_of_attachment_file);
$body = $message->get();
$extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email);
$headers = $message->headers($extraheaders);
$mail = Mail::factory("mail");
$mail->send($to, $headers, $body);

Error with PEAR based email class

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.

Categories