Solved

php send authenticated email to more than one recipient

Posted on 2015-01-08
5
122 Views
Last Modified: 2015-01-30
Hi

I have some code I am using to send email from a web form

function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
{
//SMTP + SERVER DETAILS
/* * * * CONFIGURATION START * * * */
$smtpServer = "mail.YOUR-DOMAIN.com";
$port = "25";
$timeout = "30";
$username = "your-emaill@domain.com";
$password = "Email password";
$localhost = "mail.YOUR-DOMAIN.com";
$newLine = "\r\n";
/* * * * CONFIGURATION END * * * * */

//Connect to the host on the specified port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 515);
if(empty($smtpConnect))
{
$output = "Failed to connect: $smtpResponse";
return $output;
}
else
{
$logArray['connection'] = "Connected: $smtpResponse";
}

//Request Auth Login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authrequest'] = "$smtpResponse";

//Send username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authusername'] = "$smtpResponse";

//Send password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authpassword'] = "$smtpResponse";

//Say Hello to SMTP
fputs($smtpConnect, "HELO $localhost" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['heloresponse'] = "$smtpResponse";

//Email From
fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailfromresponse'] = "$smtpResponse";

//Email To
fputs($smtpConnect, "RCPT TO: $to" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailtoresponse'] = "$smtpResponse";

//The Email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data1response'] = "$smtpResponse";

//Construct Headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto <$to>" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;

fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data2response'] = "$smtpResponse";

// Say Bye to SMTP
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['quitresponse'] = "$smtpResponse";
}
?>

Open in new window


this code works fine, but I want to copy another email address in to the email...

I have tried adding another email like this:


$to = "jon@home.com, jim@work.com";

but it didn't work....

any able to help?
0
Comment
Question by:cycledude
[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
5 Comments
 
LVL 9

Expert Comment

by:Brian Tao
ID: 40539271
Why don't you use PHPMailer? http://sourceforge.net/projects/phpmailer/
It's easy to use and saves you a lot of trouble.
0
 

Author Comment

by:cycledude
ID: 40539523
Email must be authenticated and its a windows server
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 40539525
PHPMailer will do that on a Windows server.  Where is your mail server?  I've used PHPMailer to send mail thru my Gmail account.  It includes sample code to do that.
0
 
LVL 11

Expert Comment

by:hecgomrec
ID: 40544425
I think what you need should be "mail1@domain.com; mail2@domain.com";  assigned to $TO but because you are using $NAMETO it won't work as you intend on your header section.

As it is now in your header section will be something like:

 To: Name, Last_name <mail1@domain.com; mail2@domain.com>.   Which is invalid!!

You should introduce more VARS to the function to cover more addresses ($to1, $nameto1, $to2, $name2).
0
 

Author Closing Comment

by:cycledude
ID: 40579695
thanks,
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Building a glossary into your website 9 60
&& operators not performing required logic 8 41
Bootstrap - BootBox - PHP - MYSQL - Response Trigger 6 62
Google Recaptcha 13 55
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

751 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