Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

php send authenticated email to more than one recipient

Posted on 2015-01-08
5
Medium Priority
?
128 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
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 84

Accepted Solution

by:
Dave Baldwin earned 1500 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
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…
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.
Suggested Courses

971 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