Hello, i’m new here and new on Yii…sorry if my English is not very good…i would like to know how to send email to multiple recipient using SMTP-mail. This is my coding
model :
function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
{
//SMTP + SERVER DETAILS
/* * * * CONFIGURATION START * * * */
$smtpServer = "xxx.xxxxxx.com.my";
$port = "25";
$timeout = "30";
$username = "xxx@xxxxxxx.com.my";
$password = 'xxxxxxx';
$localhost = "localhost";
$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";
}
//Say Hello to SMTP
fputs($smtpConnect, "HELO $localhost" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['heloresponse'] = "$smtpResponse";
//Request Auth Login
fputs($smtpConnect,"AUTH LOGIN" . "\r\n");
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authrequest'] = "$smtpResponse";
//Send username
fputs($smtpConnect, base64_encode($username) . "\r\n");
//fwrite($smtpConnect, base64_encode($username) . "\r\n");
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authusername'] = "$smtpResponse";
//Send password
fputs($smtpConnect, base64_encode($password) . "\r\n");
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authpassword'] = "$smtpResponse";
//Email From
fputs($smtpConnect, "MAIL FROM: <$from>" . "\r\n");
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailfromresponse'] = "$smtpResponse";
//Email To
fputs($smtpConnect, "RCPT TO: <$to>" . "\r\n");
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailtoresponse'] = "$smtpResponse";
//The Email
fputs($smtpConnect, "DATA" . "\r\n");
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data1response'] = "$smtpResponse";
/*//Construct Headers
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "To: $nameto <$to>" . "\r\n";
$headers .= "From: $namefrom <$from>" . "\r\n";
fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data2response'] = "$smtpResponse";*/
fputs($smtpConnect, 'From: "'.$namefrom.'" <'.$from.'>' . "\r\n");
fputs($smtpConnect, 'To: "'.$nameto.'" <'.$to.'>' . "\r\n");
fputs($smtpConnect, 'To: "'.$nameto.'" <'.$to.'>' . "\r\n");
fputs($smtpConnect, 'Date: Tue, 15 May 2010 18:50:00 -0000' . "\r\n");
fputs($smtpConnect, 'Subject: ' . $subject . "\r\n");
fputs($smtpConnect, '' . "\r\n");
fputs($smtpConnect, 'Greetings. ' .$message. "\r\n");
fputs($smtpConnect, "\r\n" . '.' . "\r\n"); // Is this OK?
fputs($smtpConnect, "\r\n"); // Is this OK?
$logArray['data2response'] = trim(fgets($smtpConnect));
// Say Bye to SMTP
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['quitresponse'] = "$smtpResponse";
//print_r($logArray);
//echo 'sent!';
//$this->redirect(Yii::app()->user->returnUrl);
echo '<script type="text/javascript">alert("Sent!");</script>';
controller :
public function actionEmailthis($id)
{
$model=$this->loadModel($id);
//echo $model->id_applydriver;
//die();
$model->email_status = "Approved";
$model->update();
$id_applydriver= $model->apply_id_cardriver;
$cardriver=Cardriver::model()->findByPk($id_cardriver);
$id_driver = $model->apply_id_driver;
$driver=Driver::model()->findByPk($id_driver);
$namefrom = "Administrator Department";
$from = "xxx@xxxxxx.com.my";
$nameto = "Name to";
$to = $model->email_user;
$to = $model->email_user;
$subject = "Status of .........";
$message = "Message of .........\n".
"\n Status Application: " ." $model->status \n".
"\n User : " ." $model->name_user\n".
"\n Date start : "."$model->date_start \n" .
"\n Date end : "."$model->date_end\n" .
"\n Time start : "."$model->time_start\n" .
"\n purpose: "."$model->purpose\n".
"\n Destination: "."$model->destination \n".
"\n Plat number : " . "$cardriver->plat_number\n";
if($model->authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)){
$model->email_status = "Approved";
$model->update();
}
}