Send two email ids in CC - php

I am obtaining two email id's from database by a query. Both stored in a single variable. I want to send email to these two addresses by PHPMailer keeping them in cc. Currently only one email is being selected and passed in cc. Can I know where am I going wrong. My code here,
$get_cc_email_id_sql=mysql_query("select * from tbl_name where column_name IN(13,5)");
$user_email_cc='';
while ($get_data_cc=mysql_fetch_array($get_cc_email_id_sql))
{
$user_email_cc=$get_data_cc['email'];
}
$mail = new PHPMailer();
$subject = "Mail";
$content ="XYZ";
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "ssl";
$mail->Debugoutput = 'html';
$mail->Port = 465;
$mail->Username = "xyz#xyz.com"; // Changed username and password from
$mail->Password = "xyz";
$mail->Host = "ssl://smtp.xyz.com";
$mail->Mailer = "smtp";
$mail->SetFrom("xyz#xyz.com", "XYZ");
$mail->AddAddress(abc#abc.com);
$mail->AddCC($user_email_cc);
$mail->Subject = $subject;
$mail->WordWrap = 80;
$mail->MsgHTML($content);
$mail->IsHTML(true);
if(!$mail->Send())
echo "Problem sending mail.";
else
echo "Mail Sent";

use this code
$mail->AddCC('person1#domain.com', 'Person One');
$mail->AddCC('person2#domain.com', 'Person Two');

use $user_email_cc as array then it will store you both email a 0 and 1 position
$user_email_cc=array();
while ($get_data_cc=mysql_fetch_array($get_cc_email_id_sql))
{
$user_email_cc[] =$get_data_cc['email'];
}
New Code
$get_cc_email_id_sql=mysql_query("select * from tbl_name where column_name IN(13,5)");
$user_email_cc=array();
while ($get_data_cc=mysql_fetch_array($get_cc_email_id_sql))
{
$user_email_cc[] =$get_data_cc['email'];
}
$mail = new PHPMailer();
$subject = "Mail";
$content ="XYZ";
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "ssl";
$mail->Debugoutput = 'html';
$mail->Port = 465;
$mail->Username = "xyz#xyz.com"; // Changed username and password from
$mail->Password = "xyz";
$mail->Host = "ssl://smtp.xyz.com";
$mail->Mailer = "smtp";
$mail->SetFrom("xyz#xyz.com", "XYZ");
foreach($user_email_cc as $email_cc){
$mail->AddCC($email_cc);
}
$mail->AddAddress(abc#abc.com);
$mail->Subject = $subject;
$mail->WordWrap = 80;
$mail->MsgHTML($content);
$mail->IsHTML(true);
if(!$mail->Send())
echo "Problem sending mail.";
else
echo "Mail Sent";

Can you call $mail->AddCC(...) multiple times, just like $mail->AddAddress(...)?

Related

Could Not Access File using phpMailer

I'm Using phpmailer to send mail.
Recieving mail fine but the attachment not attached in it.
When I debug it. it gives me the error.
"Could not access file: upload_images/images.jpg"
i have a folder upload_images in which i have an image name images.
here is my code...
if (isset($_POST['btnsubmit'])) {
require "phpmailer/PHPMailerAutoload.php";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "mail.domain.com";
$mail->port = 465;
$mail->SMTPAuth=true;
$mail->SMTPDebug = 4;
$mail->SMTPSecure = 'tls';
$mail->Username = "info#example.com";
$mail->Password = '******';
$file_name = $_FILES["attc"]["name"];
$tmp_name = $_FILES["attc"]["tmp_name"];
$path = '/upload_images/';
//move_uploaded_file($tmp_name, $path.$file_name);
$mail->setFrom('abc#gmail.com','From_name');
$mail->addAddress('xyz#gmail.com','To_name');
$mail->addReplyTo('abc#gmail.com','From_name');
$mail->isHTML(true);
$mail->AddAttachment("upload_images/images.jpg","images.jpg");
$mail->Subject = "Testing well";
$mail->Body = 'This is Body Part';
if ($mail->send()) {
echo "<script>alert('Email Sent Success!')</script>";
}
else{
echo "<script>alert('".$mail->ErrorInfo."')</script>";
}
}
You should use an absolute path. For example, if upload_images is inside the document root, you may use:
$mail->AddAttachment($_SERVER['DOCUMENT_ROOT']."/upload_images/images.jpg","images.jpg");
Use the absolute path.
if (isset($_POST['btnsubmit'])) {
require "phpmailer/PHPMailerAutoload.php";
$my_path ="upload_images/images.jpg";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "mail.domain.com";
$mail->port = 465;
$mail->SMTPAuth=true;
$mail->SMTPDebug = 4;
$mail->SMTPSecure = 'tls';
$mail->Username = "info#example.com";
$mail->Password = '******';
$file_name = $_FILES["attc"]["name"];
$tmp_name = $_FILES["attc"]["tmp_name"];
$path = '/upload_images/';
//move_uploaded_file($tmp_name, $path.$file_name);
$mail->setFrom('abc#gmail.com','From_name');
$mail->addAddress('xyz#gmail.com','To_name');
$mail->addReplyTo('abc#gmail.com','From_name');
$mail->isHTML(true);
$mail->AddAttachment($my_path);
$mail->Subject = "Testing well";
$mail->Body = 'This is Body Part';
if ($mail->send()) {
echo "<script>alert('Email Sent Success!')</script>";
}
else{
echo "<script>alert('".$mail->ErrorInfo."')</script>";
}
}
Use the following and path for file in the variable $my_path
if (isset($_POST['btnsubmit'])) {
//use the path
$my_path = "/upload_images/images.jpg";
require "phpmailer/PHPMailerAutoload.php";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "mail.domain.com";
$mail->port = 465;
$mail->SMTPAuth=true;
$mail->SMTPDebug = 4;
$mail->SMTPSecure = 'tls';
$mail->Username = "info#example.com";
$mail->Password = '******';
$file_name = $_FILES["attc"]["name"];
$tmp_name = $_FILES["attc"]["tmp_name"];
$path = '/upload_images/';
//move_uploaded_file($tmp_name, $path.$file_name);
$mail->setFrom('abc#gmail.com','From_name');
$mail->addAddress('xyz#gmail.com','To_name');
$mail->addReplyTo('abc#gmail.com','From_name');
$mail->isHTML(true);
$mail->AddAttachment($my_path,'images.jpg');
$mail->Subject = "Testing well";
$mail->Body = 'This is Body Part';
if ($mail->send()) {
echo "<script>alert('Email Sent Success!')</script>";
}
else{
echo "<script>alert('".$mail->ErrorInfo."')</script>";
}
}

PHP Mailer Multiple email

I have a form in my website. When the user fills out the form, I want an email to be sent to me with data entered in the form and also a thank you email for the person filling the form.
This is the code I am using:
<?php
function sendEmail($subject, $body) {
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl"; // does tls works with port 25
$mail->Host = 'smtp.zoho.com'; // is this the correct
$mail->Port = 465;
$mail->Username = "noreply#domain.org";
$mail->Password = "mypassword";
$mail->SetFrom("noreply#domain.org");
$mail->Subject = $subject;
$mail->Body = $body;
$mail->IsHTML(true);
$mail->AddAddress('data#domain.org');
if(!$mail->Send()) {
$mail->ErrorInfo;
echo "<script>alert('Error Occured sending the enquiry. Please try again')</script>";
}
else
{
echo "<script>window.location.href='http://domain.com/index.html'</script>";
}
}
?>
<?php
function sendEmail($subject, $body) {
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 3;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl"; // does tls works with port 25
$mail->Host = 'smtp.zoho.com'; // is this the correct
$mail->Port = 465;
$mail->Username = "noreply#domain.org";
$mail->Password = "mypassword";
$mail->SetFrom("noreply#domain.org");
$mail->Subject = $subject;
$mail->Body = $body;
$mail->IsHTML(true);
$mail->AddAddress(''.$_POST['emailAddr'].''); // **
emailAddr is the name for email field in the form and I wish to send email to this email address.
$mail->Subject = 'Thank you for contacting Domain';
$mail->Body = 'Thanks for getting in touch. Your message has been received and will be processed as soon as possible.';
if(!$mail->Send()) {
$mail->ErrorInfo;
echo "<script>alert('Error Occured sending the enquiry. Please try again')</script>";
}
else
{
echo "<script>window.location.href='http://domain.com/index.html'</script>";
}
}
?>

While sending mail getting following error

<?php
require('phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->Username = "akilesh1111#gmail.com";
$mail->Password = "*********";
$mail->Host = "smtp.gmail.com";
$mail->Mailer = "smtp";
$mail->SetFrom($_POST["userEmail"], $_POST["userName"]);
$mail->AddReplyTo($_POST["userEmail"], $_POST["userName"]);
$mail->AddAddress("akilesh1111#gmail.com");
$mail->Subject = $_POST["subject"];
$mail->WordWrap = 80;
$mail->MsgHTML($_POST["content"]);
if(is_array($_FILES)) {
$mail->AddAttachment($_FILES['attachmentFile']['tmp_name'],$_FILES['attachmentFile']['name']);
}
$mail->IsHTML(true);
if(!$mail->Send()) {
echo "<p class='error'>Problem in Sending Mail.</p>";
} else {
echo "<p class='success'>Contact Mail Sent.</p>";
}
?>
This code is working fine in localhost
When uploading to server then it shows below error
SMTP Error: Could not authenticate. Problem in Sending Mail.

Mailer function error

SMTP -> ERROR: DATA not accepted from server: 550 This message was classified as SPAM and may not be delivered
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "mail.cc.com";
$mail->SMTPAuth = true;
$mail->Username = "info#cc.com";
$mail->Password = "cc";
$mail->From = "cc.com"." <info#cc.com>";
$mail->AddReplyTo("cc1#gmail.com");
$mail->FromName = "info#cc.com";
$mail->AddAddress($climail);
$mail->AddCC("cc1#gmail.com");
$mail->Sender="info#cc.com";
$mail->IsHTML(true);
$mail->WordWrap = 70;
$mail->Subject = $sub;
echo $meegate;
$mail->Body=$meegate;
$mail->SMTPDebug = 1;
$mail->Send();
Error occurring while sending mail.. mail not sent..
You need to set SMTP host and also the port I guess which is 465:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "info#gmail.com";
$mail->Password = "cc";
$mail->From = "gmail.com"." <info#gmail.com>";
$mail->AddReplyTo("cc1#gmail.com");
$mail->FromName = "info#gmail.com";
$mail->AddAddress($climail);
$mail->AddCC("cc1#gmail.com");
$mail->Sender="info#gmail.com";
$mail->IsHTML(true);
$mail->WordWrap = 70;
$mail->Subject = $sub;
echo $meegate;
$mail->Body=$meegate;
$mail->SMTPDebug = 1;
$mail->Send();

Username and Password not accepted

I'm trying to send a email using gmail's smtp (see below code) but I'm getting "Username and Password not accepted" error.
I've tried:
this link
allow less secure apps
ssl is enabled
enable imap/pop on gmail's settings
a different login
none of them works.
Here's the PHP code:
function sendEmail($from, $fromName, $msg)
{
$mail = new PHPMailer();
$mail->SMTPDebug = 4; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line
$mail->IsSMTP();
//$mail->Host = 'tls://smtp.gmail.com:587';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; //465
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = 'xxxx#gmail.com';
$mail->Password = 'yyyyyyy';
$mail->From = $from;
$mail->FromName = $fromName;
$mail->AddAddress('foo#gmail.com', ' ');
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Priority = 1;
$mail->Timeout = 60;
$mail->SMTPKeepAlive = true;
$mail->Subject = "subject here";
$mail->Body = $msg;
$mail->AltBody = 'testing..';
$ok = $mail->Send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
return $ok;
}
Update: Here's the full error message (with DebugMode = 4)
After testing I got this to work by changing the host to include the tls prefix.
function sendEmail($from, $fromName, $msg)
{
$mail = new PHPMailer();
$mail->SMTPDebug = 4;
$mail->WordWrap = 900;
$mail->IsSMTP();
//$mail->Host = 'tls://smtp.gmail.com:587';
$mail->Host = "tls://smtp.gmail.com";
$mail->Port = 587; //465
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = 'xxxx#gmail.com';
$mail->Password = 'yyyyyyy';
// Define o remetente
$mail->From = $from;
$mail->FromName = $fromName;
$mail->AddAddress('foo#gmail.com', ' ');
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Priority = 1;
$mail->Timeout = 60;
$mail->SMTPKeepAlive = true;
$mail->Subject = "subject here";
$mail->Body = $msg;
$mail->AltBody = 'testing..';
$ok = $mail->Send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
return $ok;
}

Categories