Using XPertMailer to Send BCC via SMTP

I have managed to use XPertMailer to send HTML email via SMTP, but I can't work out how to add CC and BCC addresses.
The code that I am using is below.
I am looking for an answer that explains how to modify this code to add CC and BCC addresses.
require_once 'SMTP.php'; 
 
function mymailer ($from, $to, $subj, $text, $html) { 
 
$relay = 'auth.smtp.1and1.co.uk'; 
$user = (username removed); 
$pass = (password removed); 
$port = 25; 
 
// set text/plain version of message 
$msg1 = MIME::message($text, 'text/plain'); 
// set text/html version of message 
$msg2 = MIME::message($html, 'text/html'); 
// compose message in MIME format 
$mess = MIME::compose($msg1, $msg2); 
// standard mail message RFC2822 
$body = 'From: '.$from."\r\n". 
                'To: '.$to."\r\n". 
                'Subject: '.$subj."\r\n". 
                $mess['header']."\r\n\r\n". 
                $mess['content']; 
 
$c = SMTP::connect($relay, $port, $user, $pass ) or die(print_r($_RESULT)); 
 
// send mail relay 
$s = SMTP::send($c, array($to), $body, $from); 
 
// print result 
if ($s) { 
// echo 'Sent !'; 
}else{ 
 print_r($_RESULT); 
} 
// disconnect 
SMTP::disconnect($c); 
 
}

Open in new window

LVL 2
alexmayAsked:
Who is Participating?
 
XorlevCommented:
Outside it. Glad it worked out for you.
0
 
afzzCommented:
try this
require_once 'SMTP.php'; 
 
function mymailer ($from, $to, $subj, $text, $html, $cc="", $bcc="") { 
 
$relay = 'auth.smtp.1and1.co.uk'; 
$user = (username removed); 
$pass = (password removed); 
$port = 25; 
 
 
 
// set text/plain version of message 
$msg1 = MIME::message($text, 'text/plain'); 
// set text/html version of message 
$msg2 = MIME::message($html, 'text/html'); 
// compose message in MIME format 
$mess = MIME::compose($msg1, $msg2); 
 
if($cc != "")
$mess['header'] .= $cc."\r\n";
if($bcc != "")
$mess['header'] .= $bcc."\r\n";
 
// standard mail message RFC2822 
$body = 'From: '.$from."\r\n". 
                'To: '.$to."\r\n". 
                'Subject: '.$subj."\r\n". 
                $mess['header']."\r\n\r\n". 
                $mess['content']; 
 
$c = SMTP::connect($relay, $port, $user, $pass ) or die(print_r($_RESULT)); 
 
// send mail relay 
$s = SMTP::send($c, array($to), $body, $from); 
 
// print result 
if ($s) { 
// echo 'Sent !'; 
}else{ 
 print_r($_RESULT); 
} 
// disconnect 
SMTP::disconnect($c); 
 
}

Open in new window

0
 
afzzCommented:
sorry, try the following
require_once 'SMTP.php'; 
 
function mymailer ($from, $to, $subj, $text, $html, $cc="", $bcc="") { 
 
$relay = 'auth.smtp.1and1.co.uk'; 
$user = (username removed); 
$pass = (password removed); 
$port = 25; 
 
 
 
// set text/plain version of message 
$msg1 = MIME::message($text, 'text/plain'); 
// set text/html version of message 
$msg2 = MIME::message($html, 'text/html'); 
// compose message in MIME format 
$mess = MIME::compose($msg1, $msg2); 
 
if($cc != "")
$mess['header'] .= "Cc: ".$cc."\r\n";
if($bcc != "")
$mess['header'] .= "Bcc: ".$bcc."\r\n";
 
// standard mail message RFC2822 
$body = 'From: '.$from."\r\n". 
                'To: '.$to."\r\n". 
                'Subject: '.$subj."\r\n". 
                $mess['header']."\r\n\r\n". 
                $mess['content']; 
 
$c = SMTP::connect($relay, $port, $user, $pass ) or die(print_r($_RESULT)); 
 
// send mail relay 
$s = SMTP::send($c, array($to), $body, $from); 
 
// print result 
if ($s) { 
// echo 'Sent !'; 
}else{ 
 print_r($_RESULT); 
} 
// disconnect 
SMTP::disconnect($c); 
 
}

Open in new window

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
alexmayAuthor Commented:
I don't think that will work.
You have added the cc and bcc to the message header, but you haven't done anything with SMTP.
Doesn't that need to be told to use the cc and bcc addresses to send to?
0
 
afzzCommented:
Try it if it works. I remember using it earlier
0
 
alexmayAuthor Commented:
No, it doesn't work. The header fields all appear in all the received messages, and the message is not received by the cc and bcc.
0
 
afzzCommented:
ok Try the following. Seperate each to,cc,bcc address argument by comma
require_once 'SMTP.php'; 
 
function mymailer ($from, $to, $subj, $text, $html, $cc="", $bcc="") { 
 
$relay = 'auth.smtp.1and1.co.uk'; 
$user = (username removed); 
$pass = (password removed); 
$port = 25; 
 
 
 
// set text/plain version of message 
$msg1 = MIME::message($text, 'text/plain'); 
// set text/html version of message 
$msg2 = MIME::message($html, 'text/html'); 
// compose message in MIME format 
$mess = MIME::compose($msg1, $msg2); 
 
$to_array=array();
 
if($cc != ""){
$mess['header'] .= "Cc: ".$cc."\r\n";
$cc_array=explode(","$cc);
}
if($bcc != ""){
//$mess['header'] .= "Bcc: ".$bcc."\r\n";
$bcc_array=explode(","$bcc);
}
 
$to_array=explode(","$to);
 
$recepients=array_merge($to_array,$cc_array,$bcc_array);
 
// standard mail message RFC2822 
$body = 'From: '.$from."\r\n". 
                'To: '.$to."\r\n". 
                'Subject: '.$subj."\r\n". 
                $mess['header']."\r\n\r\n". 
                $mess['content']; 
 
$c = SMTP::connect($relay, $port, $user, $pass ) or die(print_r($_RESULT)); 
 
// send mail relay 
$s = SMTP::send($c, $recepients, $body, $from); 
 
// print result 
if ($s) { 
// echo 'Sent !'; 
}else{ 
 print_r($_RESULT); 
} 
// disconnect 
SMTP::disconnect($c); 
 
}

Open in new window

0
 
afzzCommented:
the cc/bcc arrays may need initialization. done it here
require_once 'SMTP.php'; 
 
function mymailer ($from, $to, $subj, $text, $html, $cc="", $bcc="") { 
 
$relay = 'auth.smtp.1and1.co.uk'; 
$user = (username removed); 
$pass = (password removed); 
$port = 25; 
 
 
 
// set text/plain version of message 
$msg1 = MIME::message($text, 'text/plain'); 
// set text/html version of message 
$msg2 = MIME::message($html, 'text/html'); 
// compose message in MIME format 
$mess = MIME::compose($msg1, $msg2); 
 
$to_array=array();
$cc_array=array();
$bcc_array=array();
 
if($cc != ""){
$mess['header'] .= "Cc: ".$cc."\r\n";
$cc_array=explode(","$cc);
}
if($bcc != ""){
//$mess['header'] .= "Bcc: ".$bcc."\r\n";
$bcc_array=explode(","$bcc);
}
 
$to_array=explode(","$to);
 
$recepients=array_merge($to_array,$cc_array,$bcc_array);
 
// standard mail message RFC2822 
$body = 'From: '.$from."\r\n". 
                'To: '.$to."\r\n". 
                'Subject: '.$subj."\r\n". 
                $mess['header']."\r\n\r\n". 
                $mess['content']; 
 
$c = SMTP::connect($relay, $port, $user, $pass ) or die(print_r($_RESULT)); 
 
// send mail relay 
$s = SMTP::send($c, $recepients, $body, $from); 
 
// print result 
if ($s) { 
// echo 'Sent !'; 
}else{ 
 print_r($_RESULT); 
} 
// disconnect 
SMTP::disconnect($c); 
 
}

Open in new window

0
 
alexmayAuthor Commented:
Yes, of course, everyone gets the mail now as you have basically just added the CC and BCC addresses to the TO list, but the headers don't work. The CC and BCC are not really CC and BCC unless the headers work. Neither are being recognised by the server or client software. They sit there in the headers for all to see which means that the BCC is not blind at all.
0
 
afzzCommented:
can you paste the headers received from above as well as the smtp.php
0
 
XorlevCommented:
http://www.xpertmailer.com/documentation/mail/addcc.html
http://www.xpertmailer.com/documentation/mail/addbcc.html

What you want to do is make a new MAIL class, define your message, headers, CCs, and BCCs, then use the mail class' ability to send via SMTP. it seems far less difficult to work with than attempting to force the SMTP class to do so.

Instead of $msg1 and $msg2 you'd do $mail->text(MIME::message($text, 'text/plain')); and $mail->html(IME::message($html, 'text/html'));

Here's the part for sending it:
// connect to SMTP server
$smtp = $mail->Connect('smtp.hostname.tld', 25, 'user', 'pass');
// send mail relay using '$c' resource connection
$mail->Send($smtp);

Open in new window

0
 
alexmayAuthor Commented:
Xorlev,
What your paste seems to be saying is "read the manual"!
Well, guess what, I did read the manual and I came up with what I showed above.
I'm sure doing it the way you suggest would work, but I don't know how.
If I did, I wouldn't have asked this question.
If you know how it works, please tell me how to modify my code, or suggest alternate code.
The snippet that you posted from the manual really doesn't get me very far.
But, I have tried to just throw everything together that has been thrown at me from different sources,
and the result it below.
Are you saying that this would work?
Thanks for your help.
require_once 'SMTP.php'; 
require_once 'MAIL.php'; 
$relay = 'auth.smtp.1and1.co.uk'; 
$user = (username removed); 
$pass = (password removed); 
$port = 25; 
$mail = new MAIL ;
$mail->Subject($subject);
$mail->text(MIME::message($text, 'text/plain')); 
$mail->html(IME::message($html, 'text/html'));
$mail->AddTo($to, $to); //first is email address and 
$mail->AddCc($cc, $cc); //second is alias
$mail->AddBcc($bcc, $bcc);
$m->From($from, $from);
$smtp = $mail->Connect($relay, $port, $user, $pass);
$mail->Send($smtp);
$mail->Disconnect();

Open in new window

0
 
XorlevCommented:
$mail->html(IME::message($html, 'text/html'));

to

$mail->html(MIME::message($html, 'text/html'));

I did an incorrect copy/paste.

From what I read in the manual, yes that's how it'd work. Give it a try.
0
 
alexmayAuthor Commented:
Made a few tweaks, and it now seems to work.
I have to agree, this looks much cleaner than what I had.
I don't understand why they don't write the examples like this.
Thanks for your help.
One small point. Should the require_once statements be inside the function?
require_once 'SMTP.php';
require_once 'MAIL.php';
 
function mymailer ($from, $to, $cc, $bcc, $subj, $text, $html) {
 
// CONFIGURATION ------------------
$relay = [relay host name];
$user = [relay login name];
$pass = [relay password];
$port = 25;
// CONFIGURATION ------------------
 
$mail = new MAIL ;
$mail->Subject($subject);
$mail->Text($text); 
$mail->Html($html);
$mail->AddTo($to, $to); //first is email address and 
$mail->AddCc($cc, $cc); //second is alias
$mail->AddBcc($bcc, $bcc);
$mail->From($from, $from);
$smtp = $mail->Connect($relay, $port, $user, $pass);
$mail->Send($smtp);
$mail->Disconnect();
}

Open in new window

0
 
alexmayAuthor Commented:
Just realised that I don't need the require_once 'SMTP.php'; at all as that is done by MAIL.php anyhow.
Still, the question is where the other require_once line should really be: in the function or outside it?
0
 
alexmayAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.