コード例 #1
0
/**
function tell_a_friend(){
	$error = array();
	$session = new Session();
	$name = safe_output( $_POST['invite_name'] );
	if( empty($name) ){ $error[] = "Please enter your name";}
	
	$email = safe_output( $_POST['invite_email'] );
	if( empty($email) ){ $error[] = "Please enter your Friend's Email";}
	
	if( sizeof($error) == 0  )
	{
		$notes= $name." thought you might find this site of interest ";
		$notes.="<br /><br />http://www.".$_SERVER['HTTP_HOST'];
		$subject = SITE_NAME." - a friend thinks you may find this site useful";
		$send_to 	= array("email" => $email, "name" => $email );
		$send_from 	= array("email" => NO_REPLY_EMAIL, "name" => NO_REPLY_EMAIL );
		
		$mail = send_mail( $notes, $subject, $send_to, $send_from, null, null );
		
		if($mail){
			$session->message ( "<div class='success'>Your request has been sent successfully.</div>" );
			//redirect_to( $_SERVER['PHP_SELF']. !empty($_SERVER['QUERY_STRING']) ? "?".$_SERVER['QUERY_STRING'] : "" );
		}else{
			$session->message ( "<div class='error'>Problem sending email, please try again.</div>" );
		}
		
	}
	else
	{
		
		$message = "<div class='error'> 
						following error(s) found:
					<ul> <li />";
		$message .= join(" <li /> ", $error);
		
		$message .= " </ul> 
				   </div>";	
		$session->message ( $message );
	}
	$query_string="";
	if ( !empty( $_SERVER['QUERY_STRING'] ) || $_SERVER['QUERY_STRING'] != "" ){
		$query_string = "?".$_SERVER['QUERY_STRING'];
	}
	redirect_to( $_SERVER['PHP_SELF']. $query_string );
}

/*****/
function send_mail($notes, $subject, $to, $from, $cv_file, $cv_letter)
{
    if ($cv_file || !empty($cv_file) || is_array($cv_file)) {
        $temp_path = $cv_file['tmp_name'];
        //exit;
        $filename = basename($cv_file['name']);
        $type = $cv_file['type'];
        $size = $cv_file['size'];
    }
    //to
    $send_to = $to["email"];
    $send_to_name = $to["name"];
    //from
    $send_from = $from["email"];
    $send_from_name = $from["name"];
    if (!MAILER_HOST && MAILER_HOST == "") {
        require_once LIB_PATH . DS . "Rmail" . DS . "Rmail.php";
        /**
         * Now create the email it will be attached to
         */
        $mail = new Rmail();
        if ($cv_file || !empty($cv_file) || is_array($cv_file)) {
            $mail->AddAttachment(new namedFileAttachment($temp_path, $filename, $type));
        }
        //who is send the email
        $mail->setFrom($send_from_name . ' <' . $send_from . '>');
        $mail->setSubject($subject);
        // body of the text
        $mail->setText($notes);
        $mail->setHTML($notes);
        $result = $mail->send($addresses = array($send_to));
        return $result;
    } else {
        if (MAILER_TEST && MAILER_TEST == 'true') {
            $mail = new PHPMailer(true);
            $mail->SMTPDebug = 2;
            // enables SMTP debug information (for testing)
        } else {
            $mail = new PHPMailer();
        }
        $mail->IsSMTP();
        // telling the class to use SMTP
        $mail->Host = MAILER_HOST;
        // SMTP server
        $mail->SMTPAuth = MAILER_AUTH;
        // turn on SMTP authentication
        $mail->Host = MAILER_HOST;
        // SMTP server
        $mail->Port = MAILER_PORT;
        $mail->Username = MAILER_USERNAME;
        // SMTP username
        $mail->Password = MAILER_PASSWORD;
        // SMTP password
        try {
            $mail->AddAddress($send_to, $send_to_name);
            $mail->SetFrom($send_from, $send_from_name);
            //email, name
            $mail->Subject = $subject;
            $mail->AltBody = $notes;
            // optional - MsgHTML will create an alternate automatically
            $mail->MsgHTML($notes);
            //message
            if ($cv_file || !empty($cv_file) || is_array($cv_file)) {
                $mail->AddAttachment($temp_path, $filename, "base64", $type);
                // attachment
            }
            return $mail->Send();
        } catch (phpmailerException $e) {
            echo $e->errorMessage();
            //Pretty error messages from PHPMailer
            die;
        } catch (Exception $e) {
            echo $e->getMessage();
            //Boring error messages from anything else!
            die;
        }
    }
}