how to send mutiple emails in my code? - php

I need to send the email to the multiple recipients but I am getting error in my code. I need to send the email to multiple recipients.
<?php
require 'phpmailer/PHPMailerAutoload.php';
if(isset($_POST['send']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$to_id = $_POST['toid'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.domain.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $email;
$mail->Password = $password;
$mail->setFrom('info#domain.com', 'name');
$mail->addReplyTo('info#domain.com', 'name');
$mail->addAddress($to_id);
$mail->Subject = $subject;
$mail->msgHTML($message);
if(!$mail->send()) {
$error = "Mailer Error: " . $mail->ErrorInfo;
?>
<script>alert('<?php echo $error ?>');</script>
<?php
}
else {
echo "Message Sent Successfully";
}
}
?>

Try this code.
require 'PHPMailer/PHPMailerAutoload.php';
function SendPHPMail($to, $from, $subject, $htmlContent, $attachments = array())
{
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'emailAddress#gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 25;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => true,
'allow_self_signed' => true
)
);
$mail->From = 'emailAddress#gmail.com'; //sender emailAddress
$mail->FromName = 'name'; //sender name
//Here $to has multiple emailAddress
//$to = array('address1#domain.com','address2#domain.com','address3#domain.com');
if(!empty($to)){
foreach($to as $emailAddress){
$mail->addAddress($emailAddress);
}
} else{
throw new \Exception('No emails found!');
}
if(!empty($attachments)){
foreach($attachments as $attachment){
$mail->addAttachment($attachment);
}
}
//$mail->addCC();
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $htmlContent;
if(!$mail->send()) {
throw new \Exception($mail->ErrorInfo);
}
}

instead of $mail->addAddress($to_id); use something like this:
$mail->addAddress("peter#doe.com , hannes#mail.com , manuel#domain.com", "...");

You can use just like that:
$mail->AddAddress('email1#domain.com', 'First Email');
$mail->AddAddress('email2#domain.com', 'Second Email');
Or if you have emails in an array $_POST['toid'], than you can use AddAddress() in a loop.

Related

PHPMailer: Attempt to assign property of non-object

I'm frustrated to fix this.
One time I made it work. The next day, an error annoys me:
Warning: Attempt to assign property of non-object in C:\XAMPP\htdocs\HAF\includes\sendmail.php on line 356
Help me fix this :(
Here's my code:
sendmail.php
class SendMail {
function notification($recipient, $name, $subject, $message) {
global $email;
$email->Host = "smtp.gmail.com";
$email->SMTPAuth = true;
$email->Username = "******#gmail.com";
$email->Password = "**********";
$email->SMTPSecure = "tls";
$email->Port = 465;
$email->setFrom('admin#gmail.com', 'My WebApp');
$email->addAddress($recipient);
$email->isHTML(true);
$email->Subject = $subject;
$email->Body = $message;
if(!$email->send()) {
return false;
} else {
return true;
}
}
}
index.php
$email = "jaydenjames#gmail.com";
$name = "Jayden James";
$message = "Welcome {$name}!";
$SendMail->notification($email, $name, 'Welcome guest!', $message);
update answer with gmail configuration....
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = 'MyUsername#gmail.com';
$mail->Password = 'valid password';
$mail->SMTPAuth = true;
$mail->From = 'MyUsername#gmail.com';
$mail->FromName = 'Mohammad Masoudian';
$mail->AddAddress('anotherValidGmail#gmail.com');
$mail->AddReplyTo('phoenixd110#gmail.com', 'Information');
$mail->IsHTML(true);
$mail->Subject = "PHPMailer Test Subject via Sendmail, basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->Body = "Hello";
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message sent!";
}

How to send multiple emails by php variable in Addcc

From $list i am getting all the emails that are posted by form after using explode
$list = explode(',', $guest_email);
foreach ($list as $k => $v) {
$mail = new PHPmailer();
$mail->From = $smtp_user;
$mail->FromName = $fromname;
$mail->Mailer = "mail";
$mail->Subject = "";
$mail->IsHTML(true);
//$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = ;
$mail->SMTPSecure = '';
$mail->Host = $smtp;
$mail->Port = $port;
$mail->Password = $sm;
$mail->Username = $sm;
$mail->Body = $bodyContent;
in below line the mail are going to all posted email address, in next line i want to add all the emails in cc, but it is not working
$mail->AddAddress($v);
$mail->Addcc($v)
$mail->AddReplyTo($smtp_user);
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "SUCCESS";
}
}
how to send all email in $mail->Addcc($v)

Internal error for PHPmailer on server

$msg = 'God Bless You';
$subj = 'Ryan D\'souza Photgraphy Invoice';
$to = $_POST['email'];
$from = 'emailaddres';
$name = $_POST['client'];
smtpmailer($to,$from, $name ,$subj, $msg);
function smtpmailer($to, $from, $from_name = 'Ryan D\'souza Photgraphy', $subject, $body)
{
global $error;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.sendgrid.net';
$mail->Port = 465;
$mail->Username = 'username';
$mail->Password = 'password';
$mail->IsHTML(true);
$mail->From="emailaddres";
$mail->FromName="Ryan D'souza Photgraphy";
$mail->Sender=$from; // indicates ReturnPath header
$mail->AddReplyTo($from, $from_name); // indicates ReplyTo headers
$mail->AddCC('cc#site.com.com', 'CC: to site.com');
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAttachment('pdfs/'.$_POST['client'].'.pdf');
$mail->AddAddress($to);
$mail->AddAddress("emailaddres");
if(!$mail->Send())
{
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
}
else
{
$error = 'Message sent!';
return true;
}
}
I'm using PHP mailer to attach a file and send mail. the code works on local system but when I upload it on the server that time it gives me Internal server error. I tried commenting and print each line of the code and found out that i get at $mail->Send().

how to variable pass mail function in php

I am new to php and i want solution for variable pass in mail function
I am using this way to variable pass mail function but getting issue Code:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
function Send_Mail($to,$subject,$body,$cc = "",$bcc = "")
{
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->SMTPDebug = false;
$mail->Username = 'xyz#gmail.com'; // Your Gmail Address
$mail->Password = '123456'; // Your Gmail Password
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->From = 'xyz#gmail.com'; // From Email Address
$mail->FromName = 'gfgg'; // From Name
$mail->addAddress($to);
$mail->addReplyTo('xyz#gmail.com', 'dfg'); // Same as From Email and From Name.
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->send()) {
return array('status' => false, 'message' => $mail->ErrorInfo);
}
return array('status' => true, 'message' => 'Email Sent Successfully');
}
if (isset($_REQUEST['submit']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$contact=$_POST['contact'];
echo $name, $email, $contact;
$result = Send_Mail('$name','$email','$contact');
}
pls give me soltution
?>
The Send_Mail function requires minimum 3 parameters, which are $to, $subject and $bodyin this order.
So first of all you are calling your parameters within strings
$result = Send_Mail('$name','$email','$contact');
and in the wrong order.
You should probably try this
$result = Send_Mail($email, "Subject", "Message");
You will have to define $subject and $message variables and replace them with "Subject" and "Message"
Try with -
echo "$name, $email, $contact";
$result = Send_Mail($name,$email,$contact);

Problem with using PHPMailer for SMTP

I have used PHPMailer for SMTP and there is problem in sending mail with error "Mailer Error: The following From address failed: no-reply#mydomain.org.uk"
My code is as follows:
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "localhost;"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = ""; // SMTP username
$mail->Password = ""; // SMTP password
$mail->From = $email_address;
$mail->FromName = $email_address;
$mail->AddAddress($arrStudent[0]["email"]);
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Subject";
$theData = str_replace("\n", "<BR>", $stuff);
$mail->Body = $theData; // "This is the <b>HTML body</b>";
$mail->AltBody = $stuff;
if (!$mail->Send()) {
$sent = 0;
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
i researched everything and when i debug inside class.smtp.php i found error the function "get_lines()" is returning error value "550 Authentication failed"
The code was working fine previously, i am wondering how this problem came suddenly.
Desperate for some help.
Thanks,
Biplab
public function sendEmail ( $subject, $to, $body, $from = FALSE ) {
require_once('mailer.class.php');
$mailer = new PHPMailer();
//do we use SMTP?
if ( USE_SMTP ) {
$mailer->IsSMTP();
$mailer->SMTPAuth = true;
$mailer->Host = SMTP_HOST;
$mailer->Port = SMTP_PORT;
$mailer->Password = '';
$mailer->Username = '';
if(USE_SSL)
$mailer->SMTPSecure = "ssl";
}
$mailer->SetFrom($from?$from:ADMIN_EMAIL, ADMIN_NAME);
$mailer->AddReplyTo ( ADMIN_EMAIL, ADMIN_NAME );
$mailer->AddAddress($to);
$mailer->Subject = $subject;
//$mailer->WordWrap = 100;
$mailer->IsHTML ( TRUE );
$mailer->MsgHTML($body);
require_once('util.class.php');
$mailer->AltBody = Util::html2text ( $body );
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if ( ! $mailer->Send() ) {
return FALSE;
}
else {
$mailer->ClearAllRecipients ();
$mailer->ClearReplyTos ();
return TRUE;
}
}
I've used like that... SetFrom should be used in place of From... that's your error buddy... :))
try adding belowe line to php.ini
extension=php_openssl.dll
restart and try again
I am using YII's Mailer with PHPMailer, and this works for me:
$mail = Yii::createComponent('application.extensions.mailer.EMailer');
$mail->Username = $this->SMTP_USERNAME; // SMTP username
$mail->Password = $this->SMTP_PASSWORD; // SMTP password
$mail->SMTPAuth = true;
$mail->From = $this->fromAddress;
$mail->Host = $this->SMTP_SERVER_ADDRESS;
$mail->FromName = $this->fromName;
$mail->CharSet = 'UTF-8';
$mail->Subject = Yii::t('mailer', $this->subject);
$mail->Body = $this->message;
$mail->AddReplyTo($this->toAddress);
$mail->AddAddress($this->toAddress);
$mail->IsSMTP(true);
$mail->IsHTML(true);
$mail->Send();
Hope that helps?

Categories