Sonntag, 13. Juni 2010

PHP gemerkt

PHP gemerkt


=ROUND($C$1*C10,-1.0)  runden auf volle zehner, bzw. -1 stelle nach dem komma

=ROUND($C$1*C10,0) runden auf volle Einer



Umlaute- Problem beim Email mittels PHP mail()

Kaputte Umlaute im Betreff (Subject) und Inhalt (Body) mit der Funktion mail(), vor allem, wenn die Mails per SMTP abgeholt werden

$header = "From: Ronnie James Dio <".$from_email.">\n";
$header .= "Reply-To: ".$replyemail."\n";
$header .= 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable' . "\r\n";
$subject = '=?utf-8?b?'.base64_encode($subject).'?=';

mail($to_email, $subject, $message, $header);



in modx:

save_web_user.processor.php, save_user.processor.php 

// in case any plugins include a quoted_printable function
function save_user_quoted_printable($string) {
$crlf = "\n" ;
$string = preg_replace('!(\r\n|\r|\n)!', $crlf, $string) . $crlf ;
$f[] = '/([\000-\010\013\014\016-\037\075\177-\377])/e' ;
$r[] = "'=' . sprintf('%02X', ord('\\1'))" ; $f[] = '/([\011\040])' . $crlf . '/e' ;
$r[] = "'=' . sprintf('%02X', ord('\\1')) . '" . $crlf . "'" ;
$string = preg_replace($f, $r, $string) ;
return trim(wordwrap($string, 70, ' =' . $crlf)) ;
}

// Send an email to the user
function sendMailMessage($email, $uid, $pwd, $ufn) {


global $websignupemail_message;
global $emailsubject, $emailsender;
global $site_name, $site_start, $site_url;
$message = sprintf($websignupemail_message, $uid, $pwd); // use old method
// replace placeholders
$message = str_replace("[+uid+]", $uid, $message);
$message = str_replace("[+pwd+]", $pwd, $message);
$message = str_replace("[+ufn+]", $ufn, $message);
$message = str_replace("[+sname+]", $site_name, $message);
$message = str_replace("[+saddr+]", $emailsender, $message);
$message = str_replace("[+semail+]", $emailsender, $message);
$message = str_replace("[+surl+]", $site_url, $message);
$headers = "From: " . $emailsender . "\r\n";
$headers .= "X-Mailer: Content Manager - PHP/" . phpversion();
$headers .= "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/plain; charset=utf-8\r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable\r\n";
$subject = "=?UTF-8?Q?".$emailsubject."?=";
$message = save_user_quoted_printable($message);

if (ini_get('safe_mode') == FALSE) {
if (!mail($email, $subject, $message, $headers, "-f $emailsender")) {
webAlert("$email - {$_lang['error_sending_email']}");
exit;
}
} elseif (!mail($email, $subject, $message, $headers)) {
webAlert("$email - {$_lang['error_sending_email']}");
exit;
}
}




class.phpmailer.php

class PHPMailer
{
    /////////////////////////////////////////////////
    // PUBLIC VARIABLES
    /////////////////////////////////////////////////

    /**
     * Email priority (1 = High, 3 = Normal, 5 = low).
     * @var int
     */
    var $Priority          = 3;

    /**
     * Sets the CharSet of the message.
     * @var string
     */
    //var $CharSet           = "iso-8859-1";
    var $CharSet           = "utf-8";
   

    /**
     * Sets the Content-type of the message.
     * @var string
     */
    var $ContentType        = "text/plain";

    /**
     * Sets the Encoding of the message. Options for this are "8bit",
     * "7bit", "binary", "base64", and "quoted-printable".
     * @var string
     */
    var $Encoding          = "8bit";


/assets/snippets/weblogin/weblogin.processor.inc.php

# process password reminder
    if ($isPWDReminder==1) {
        $email = $_POST['txtwebemail'];
        $webpwdreminder_message = $modx->config['webpwdreminder_message'];
        $emailsubject = $modx->config['emailsubject'];
        $emailsender = $modx->config['emailsender'];
        $site_name = $modx->config['site_name'];
        // lookup account
        $sql = "SELECT wu.*, wua.fullname
                FROM $dbase.`".$table_prefix."web_users` wu
                INNER JOIN $dbase.`".$table_prefix."web_user_attributes` wua ON wua.internalkey=wu.id
                WHERE wua.email='".$modx->db->escape($email)."'";

        $ds = $modx->db->query($sql);
        $limit = $modx->recordCount($ds);
        if($limit==1) {
            $newpwd = webLoginGeneratePassword(8);
            $newpwdkey = webLoginGeneratePassword(8); // activation key
            $row = $modx->fetchRow($ds);
            //save new password
            $sql="UPDATE $dbase.`".$table_prefix."web_users`
                  SET cachepwd='".$newpwd."|".$newpwdkey."'
                  WHERE id=".$row['id'];
            $modx->db->query($sql);
            // built activation url
            $xhtmlUrlSetting = $modx->config['xhtml_urls'];
            $modx->config['xhtml_urls'] = false;
            if($_SERVER['SERVER_PORT']!='80') {
              $url = $modx->config['server_protocol'].'://'.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$modx->makeURL($modx->documentIdentifier,'',"webloginmode=actp&wli=".$row['id']."&wlk=".$newpwdkey);
            } else {
              $url = $modx->config['server_protocol'].'://'.$_SERVER['SERVER_NAME'].$modx->makeURL($modx->documentIdentifier,'',"webloginmode=actp&wli=".$row['id']."&wlk=".$newpwdkey);
            }
            $modx->config['xhtml_urls'] = $xhtmlUrlSetting;
            // replace placeholders and send email
            $message = str_replace("[+uid+]",$row['username'],$webpwdreminder_message);
            $message = str_replace("[+pwd+]",$newpwd,$message);
            $message = str_replace("[+ufn+]",$row['fullname'],$message);
            $message = str_replace("[+sname+]",$site_name,$message);
            $message = str_replace("[+semail+]",$emailsender,$message);
            $message = str_replace("[+surl+]",$url,$message);
//Rainer
$headers = "From: " . $emailsender . "\r\n";
$headers .= "X-Mailer: Content Manager - PHP/" . phpversion();
$headers .= "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/plain; charset=utf-8\r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable\r\n";
$subject = "=?UTF-8?Q?".$emailsubject."?=";
$message = save_user_quoted_printable($message);
//$emailsubject = '=?utf-8?b?'.base64_encode("Neue Passwortbestätigung für $site_name").'?=';
$emailsubject = '=?utf-8?b?'.base64_encode("Neue Passwortbestätigung für $site_name").'?=';
           //if (!ini_get('safe_mode')) $sent = mail($email, "New Password Activation for $site_name", $message, "From: ".$emailsender."\r\n"."X-Mailer: MODx Content Manager - PHP/".phpversion(), "-f {$emailsender}");
            //else $sent = mail($email, "New Password Activation for $site_name", $message, "From: ".$emailsender."\r\n"."X-Mailer: MODx Content Manager - PHP/".phpversion());
            if (!ini_get('safe_mode')) $sent = mail($email, $emailsubject, $message, $headers, "-f {$emailsender}");
            else $sent = mail($email, $emailsubject, $message, $headers);
            if(!$sent) {
                // error
                //$output =  webLoginAlert("Error while sending mail to $email. Please contact the Site Administrator");
                $output =  webLoginAlert("Fehler beim Sender der Email an $email. Bitte kontaktieren Sie den Administrator");
                return;
            }
            //if(!$pwdReqId) $output = webLoginAlert("Please check your email account ($email) for login instructions.");
            if(!$pwdReqId) $output = webLoginAlert("Bitte prüfen Sie Ihren Emailaccount ($email). Dort finden Sie Login-Infos.");
            else {
                // redirect to password request notification page
                $url = $modx->makeURL($pwdReqId);
                $modx->sendRedirect($url,0,'REDIRECT_REFRESH');
            }
        }
        else {
            //$output = webLoginAlert("We are sorry! We cannot locate an account using that email.");
            $output = webLoginAlert("Leider gibt es keinen Account, der diese Emailadresse benutzt.");
        }

        return;

    }




/assets/snippets/weblogin/weblogin.common.inc.php

    // Send new password to the user
    function webLoginSendNewPassword($email,$uid,$pwd,$ufn){
        global $modx, $site_url;
        $mailto = $modx->config['mailto'];
        $websignupemail_message = $modx->config['websignupemail_message'];    
        $emailsubject = $modx->config['emailsubject'];
        $emailsender = $modx->config['emailsender']; 
        $site_name = $modx->config['site_name'];
        $site_start = $modx->config['site_start'];
        $message = sprintf($websignupemail_message, $uid, $pwd); // use old method
        // replace placeholders
        $message = str_replace("[+uid+]",$uid,$message);
        $message = str_replace("[+pwd+]",$pwd,$message);
        $message = str_replace("[+ufn+]",$ufn,$message);
        $message = str_replace("[+sname+]",$site_name,$message);
        $message = str_replace("[+semail+]",$emailsender,$message);
        $message = str_replace("[+surl+]",$site_url,$message);
//Rainer



$headers = "From: " . $emailsender . "\r\n";
$headers .= "X-Mailer: Content Manager - PHP/" . phpversion();
$headers .= "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/plain; charset=utf-8\r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable\r\n";
$subject = "=?UTF-8?Q?".$emailsubject."?=";
//$message = save_user_quoted_printable($message);

        if (!ini_get('safe_mode')) $sent = mail($email, $emailsubject, $message, $headers, "-f {$emailsender}");
        else $sent = mail($email, $emailsubject, $message, $headers);
        if (!$sent) webLoginAlert("Error while sending mail to $mailto",1);
        return true;
    }

/*
// in case any plugins include a quoted_printable function
function save_user_quoted_printable($string) {
$crlf = "\n" ;
$string = preg_replace('!(\r\n|\r|\n)!', $crlf, $string) . $crlf ;
$f[] = '/([\000-\010\013\014\016-\037\075\177-\377])/e' ;
$r[] = "'=' . sprintf('%02X', ord('\\1'))" ; $f[] = '/([\011\040])' . $crlf . '/e' ;
$r[] = "'=' . sprintf('%02X', ord('\\1')) . '" . $crlf . "'" ;
$string = preg_replace($f, $r, $string) ;
return trim(wordwrap($string, 70, ' =' . $crlf)) ;
}
*/