Solved

Using XPertMailer to Send BCC via SMTP

Posted on 2008-06-13
16
1,001 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
  • 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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 500 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmakerā€¦
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now