Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using XPertMailer to Send BCC via SMTP

Posted on 2008-06-13
16
Medium Priority
?
1,062 Views
Last Modified: 2013-12-25
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

0
Comment
Question by:alexmay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 3
16 Comments
 
LVL 4

Expert Comment

by:afzz
ID: 21784181
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
 
LVL 4

Expert Comment

by:afzz
ID: 21784187
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
 
LVL 2

Author Comment

by:alexmay
ID: 21784205
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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
LVL 4

Expert Comment

by:afzz
ID: 21784219
Try it if it works. I remember using it earlier
0
 
LVL 2

Author Comment

by:alexmay
ID: 21784292
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
 
LVL 4

Expert Comment

by:afzz
ID: 21784391
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
 
LVL 4

Expert Comment

by:afzz
ID: 21784396
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
 
LVL 2

Author Comment

by:alexmay
ID: 21784411
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
 
LVL 4

Expert Comment

by:afzz
ID: 21784420
can you paste the headers received from above as well as the smtp.php
0
 
LVL 3

Expert Comment

by:Xorlev
ID: 21784647
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
 
LVL 2

Author Comment

by:alexmay
ID: 21784770
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
 
LVL 3

Expert Comment

by:Xorlev
ID: 21784784
$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
 
LVL 2

Author Comment

by:alexmay
ID: 21787681
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
 
LVL 2

Author Comment

by:alexmay
ID: 21787699
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
 
LVL 3

Accepted Solution

by:
Xorlev earned 1500 total points
ID: 21787732
Outside it. Glad it worked out for you.
0
 
LVL 2

Author Closing Comment

by:alexmay
ID: 31467151
Thanks
0

Featured Post

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question