PHP send email BCC & CC using SMTP - php

I have an PHP code to send email to, cc & bcc.
In this case, the cc is not working.
I tested it and saw on my email there is no cc email. (BCC email is worked)
Here is my code:
require_once "Mail.php";
$from = "WEBAPPS <donotreply#test.com>";
$subject = "Calibration will be expiring!";
$host = 'smtp.office365.com';
$port = '587';
$username = 'donotreply#test';
$password = 'w00ew?';
$to = "alwis.david#gmail.com";
$cc = "data.gw#gmail.com";
$bcc = "hidayurie.dave#yahoo.com";
$body = "aa";
$headers = array(
'Port' => $port,
'From' => $from,
'To' => $to,
'Subject' => $subject,
'Content-Type' => 'text/html; charset=UTF-8'
);
$recipients = $to.", ".$cc.", ".$bcc;
$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>");
}
Why CC is not working? and how to solve it?

in the above code, all recepients seems treating as same.
$recipients = $to.", ".$cc.", ".$bcc;
You can use PHPMailer class at https://github.com/PHPMailer/PHPMailer . This is user friendly library.
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail = new PHPMailer(true); //turn on the exception, it will throw exceptions on errors
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
if (!empty($recipientArr)) { //have mutiple recepients
foreach ($recipientArr AS $eachAddress) {
$mail->addAddress($eachAddress);
}
}
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}

The carbon copy is an header.
All recipients are recipients in the same way for the mail server, the difference between carbon copy and blind carbon copy is just a matter of declaring it in the headers.
$headers = array(
'Port' => $port,
'From' => $from,
'To' => $to,
'Subject' => $subject,
'Content-Type' => 'text/html; charset=UTF-8',
'Cc' => $cc
);

Related

PHP code to mail sending

Is it necessary to have a mailid in which site am using the code? It is showing mail sent but it doesn't send anything to the mailid mentioned in the code. and I have a doubt from which emailid, mail will be sent? For that have I to create an id on my domain?
$msg=" ".$a." ".$b." ".$c." ".$d." ".$e;
$to="email#example.com";
$sub=$a;
$msg = wordwrap($msg, 70);
#echo $msg;
$r=mail($to,$sub,$msg);
if( $r== true ) {
echo"<script>alert('mailsent');</script>";
}else {
echo"<script>alert('failure');</script>";
}
The code I have used:
using mail() to send emails is unreliable at best you don't know if the email is actually sent or not like you've discovered passing headers to mail increases the chances of it being delivered.
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if(mail($to, $sub, $msg, $headers)){
echo "<p>Email Sent</p>";
}
A better approach is to use phpmailer - https://github.com/PHPMailer/PHPMailer
Here's their sample of sending email
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
PHPMailer is very flexible and can be used with very little options, it can also be configured to use SMTP to then emails can be sent by authenticated accounts or even use third party email services like SendGrid to delivery and monitor your emails. https://sendgrid.com/

How to add multiple mail address in cc php mail

Here is my code
$form_message .= "Application_Comments" . "\r\n";
ini_set('your_email', 'testsupport#example.com');
$to = "xyz#gmail.com";
$subject = "Collaboration";
$your_email = 'testsupport#example.com';
$headers = "From: $your_email" . "\r\n";
$headers .= "Cc:abc#yahoo.com,efg#gmail.com"."\r\n";
$headers .= "Reply-To: $your_email" . "\r\n";
if(mail($to,$subject,$form_message,$headers))
{
echo " Delete Mail Sent Successfully";
}
else
{
echo "Delete Mail not sent";
}
if I execute above code I am not receiving any mail even 'To' mail is also not getting.
If I use one mail address in cc I am getting mail, but 'To' mail was not received
can any one help me on this?
You should consider using the popular PHPMailer class to handle email needs in PHP. It makes sending emails with multiple TOs, CCs, BCCs and REPLY-TOs easily achieved. It even let's you easily add attachments to your emails. Download the ZIP from Github, and extract it to the directory it will be used in.
<?php
require '/path/to/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>

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);

How can i send an email using php including mail.php file

I want to send an email in php from my localhost which includes mail.php file.
Here is my code for test.php file:
<?php
include_once("Mail.php");
$From = "Sender's name <testing123xyz#gmail.com>";
$To = "Recipient's name <test2#gmail.com>";
$Subject = "Send Email using SMTP authentication";
$Message = "This example demonstrates how you can send email with PHP using SMTP authentication";
$Host = "mail.gmail.com";
$Username = "testing123xyz";
$Password = "testing";
// Do not change bellow
$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, $Message);
if (PEAR::isError($mail)){
echo($mail->getMessage());
} else {
echo("Email Message sent!");
}
?>
When i hit the url of test.php file, server is throwing following error :
Failed to connect to smtp.gmail.com:25 [SMTP: Failed to connect socket: No connection could be made because the target machine actively refused it. (code: -1, response: )]
Please help me out.
Thanks.
Download and use phpmailer from github:https://github.com/PHPMailer/PHPMailer
Also, check this tutorial, it will help you: http://phpmailer.worxware.com/?pg=tutorial
This is the php code to send emails:
<?php
function email($recipient_email_id,$senders_name){
include("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
//IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "sender#gmail.com"; // SMTP username
$mail->Password = "xxx"; // SMTP password
$webmaster_email = "reply#gmail.com"; //Reply to this email ID
$email = "$recipient_email_id"; // Recipients email ID
$name = "$senders_name"; // sender's's name
$mail->From = $webmaster_email;
$mail->FromName = $name;
$mail->AddAddress($email, $name);
$mail->AddReplyTo($webmaster_email, "Name");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
}
?>
My guess is that you are using localhost. Try using Test Mail Server
EDIT as of this link, you are using wrong host. Change it to smtp.gmail.com

How to send an HTML email using SMTP in PHP

I've been able to send an email using SMTP in PHP, but when I try to change the Content Type to HTML, the email doesn't get delivered. This is the code I'm trying to use:
require_once "Mail.php";
$from = "FPM <forms#fpmofames.com>";
$from_name = "FPM";
$host = "localhost";
$username = "username";
$password = "password";
$subject = "Subject";
$message = "Message";
$to = "<example#example.com>";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
'MIME-Version' => '1.0',
'Content-Type' => "text/html; charset=ISO-8859-1"
);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $message);
If I take the 'Content-Type' argument out of the headers, it sends the message just fine. I don't know why adding that causes a problem.
I did some research, then I made my own code to send mail with HTML formatting using SMTP authentication. See here:
<?php
require_once "Mail.php";
$url = $_GET['baseUrl']; // source url
$success = false;
$senderName = isset( $_GET['txtname'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_GET['txtname'] ) : "";
$senderEmail = isset( $_GET['txtemail'] ) ? preg_replace( "/[^\.\-\_\#a-zA-Z0-9]/", "", $_GET['txtemail'] ) : "";
$msg = isset( $_GET['txtDesc'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_GET['txtDesc'] ) : "";
$body = '<table width="400" border="0">
<tr>
<th scope="col">Name:</th>
<th scope="col">'.$senderName.'</th>
</tr>
<tr>
<th scope="col">Company:</th>
<td scope="col">'.$_GET['txtCompany'].'</td>
</tr>
<tr>
<th scope="row">Phone:</th>
<td>'.$_GET['txtphone'].'</td>
</tr>
<tr>
<th scope="row">E-mail:</th>
<td>'.$senderEmail.'</td>
</tr>
<tr>
<th scope="row">URL:</th>
<td>'.$url.'</td>
</tr>
<tr>
<th scope="row">Massage:</th>
<td>'.$msg.'</td>
</tr>
</table>';
$from = $senderName."<".$senderEmail.">";
$to = "Contact ManagerHR<info#aequitas-infotech.com>";
$subject = "Hi!";
$host = "XXX.host.com";
$username = "username#host.com";
$password = "*****";
/* MIME-Version should be "1.0rn" and Content-Type should be "text/html; charset=ISO-8859-1rn" to send an HTML Email */
$headers = array ('MIME-Version' => '1.0rn',
'Content-Type' => "text/html; charset=ISO-8859-1rn",
'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 {
header('Location: '.$url); // redirect to url where from inquiry made
//echo("<p>Message successfully sent!</p>");
}
?>
The problem most likely lies in the Mail class, but since we don't know what Mail class you're using, it's difficult to answer. If you're not already doing so, I'd really think about using PHPMailer: https://github.com/PHPMailer/PHPMailer
You should create the mail body via mime object. And pear will handle it from there.
Ex:
$crlf = "\n";
// Creating the Mime message
$mime = new Mail_mime($crlf);
// Setting the body of the email
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
...
$body = $mime->get();
Send your HTML email using SMTP in PHP to 2 or 3 different emails
WORKING 100%
<?php
require_once "Mail.php";
$host = "ssl://smtp.gmail.com";
$username = "example#gmail.com";
$password = "password";
$port = "465";
$to = '1-to-example#example.com' . ', ';
$to = '2-to-example#example.com' . ', ';
$to = '3-to-example#example.com' . ', ';
$email_from = "from-example#example.com";
$email_subject = "Your Subject";
$email_body = "**<html> <body>**";
$email_body .= "<strong> Your HTML code </strong>";
$email_body .= "**</body></html>**";
$headers = array ('From' => $email_from, 'To' => $to, 'Subject' =>
$email_subject, 'Reply-To' => $email_address , 'MIME-Version' => '1.0', '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, $email_body);
?>
Write content type in a first line of header just like this
$to = "<example#example.com>"; $headers = array ('Content-Type' => "text/html; charset=ISO-8859-1", 'From' => $from, 'To' => $to, 'Subject' => $subject, 'MIME-Version' => '1.0' );
This is working for me..
You can complete in two steps.
Step 1: Put code in your file:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Step:2 Download this included file in given URL.
Url: https://github.com/PHPMailer/PHPMailer.git
and click on clone or download button
and set folder your system.

Categories