Welcome Guest, Not a member yet? Register   Sign In
[solved] Multiple spaces in plain-text email
#1

[eluser]leviathan28[/eluser]
Hi people,

I'm trying to send an email using mutiple spaces to make the message look like this:

Code:
Internet Agentur                          t: 0681/1112222
Irgendeine Straße 1, 66117 Saarbrücken    f: 0681/2223333
Geschäftsführer: Irgendwer                w: www.somesite.de

But the additional spaces disappear when sending the email.

Here is how I send the email:
Code:
$str_kunde_mail = $objAP->email;
$str_user_mail  = $objUser->email;
$str_user_name  = $objUser->first_name.' '.$objUser->last_name;
$str_msg        = $this->CI->input->post('nachricht');
$str_subject    = $this->CI->input->post('betreff');

$this->CI->load->library('email');
$this->CI->email->from($str_user_mail, $str_user_name);
$this->CI->email->to($str_kunde_mail);
$this->CI->email->subject($str_subject);
$this->CI->email->message($str_msg);
$this->CI->email->send();

I don't really know wether this is a problem caused by the library..
Has someone an idea what I'm doing wrong or how to make this work?
#2

[eluser]sophistry[/eluser]
welcome to CI leviathan!

yes, you found out that the Email.php file strips multiple spaces.

here is the code from Email.php (the stripping happens right at the top, but i included the whole word_wrap() method for fun):

my advice is comment out the preg_ command that strips spaces. you could also make a MY_Email.php class and override the functionality with your own word_wrap() method. and you could file abug report and see if it sticks. i haven't had this issue, but haven't tried to format with text/plain (i usually do it with HTML because the client email reader can also do ungracious things to your text/plain emails like render them in non-monospace fonts and such.)

cheers.

Code:
/**
     * Word Wrap
     *
     * @access    public
     * @param    string
     * @param    integer
     * @return    string
     */
    function word_wrap($str, $charlim = '')
    {
        // Se the character limit
        if ($charlim == '')
        {
            $charlim = ($this->wrapchars == "") ? "76" : $this->wrapchars;
        }

        // Reduce multiple spaces
        $str = preg_replace("| +|", " ", $str);

        // Standardize newlines
        if (strpos($str, "\r") !== FALSE)
        {
            $str = str_replace(array("\r\n", "\r"), "\n", $str);
        }

        // If the current word is surrounded by {unwrap} tags we'll
        // strip the entire chunk and replace it with a marker.
        $unwrap = array();
        if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
        {
            for ($i = 0; $i < count($matches['0']); $i++)
            {
                $unwrap[] = $matches['1'][$i];
                $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
            }
        }

        // Use PHP's native function to do the initial wordwrap.
        // We set the cut flag to FALSE so that any individual words that are
        // too long get left alone.  In the next step we'll deal with them.
        $str = wordwrap($str, $charlim, "\n", FALSE);

        // Split the string into individual lines of text and cycle through them
        $output = "";
        foreach (explode("\n", $str) as $line)
        {
            // Is the line within the allowed character count?
            // If so we'll join it to the output and continue
            if (strlen($line) <= $charlim)
            {
                $output .= $line.$this->newline;
                continue;
            }

            $temp = '';
            while((strlen($line)) > $charlim)
            {
                // If the over-length word is a URL we won't wrap it
                if (preg_match("!\[url.+\]|://|wwww.!", $line))
                {
                    break;
                }

                // Trim the word down
                $temp .= substr($line, 0, $charlim-1);
                $line = substr($line, $charlim-1);
            }

            // If $temp contains data it means we had to split up an over-length
            // word into smaller chunks so we'll add it back to our current line
            if ($temp != '')
            {
                $output .= $temp.$this->newline.$line;
            }
            else
            {
                $output .= $line;
            }

            $output .= $this->newline;
        }

        // Put our markers back
        if (count($unwrap) > 0)
        {
            foreach ($unwrap as $key => $val)
            {
                $output = str_replace("{{unwrapped".$key."}}", $val, $output);
            }
        }

        return $output;
    }
#3

[eluser]leviathan28[/eluser]
Thanks! Just finished working but I will try it first thing monday morning.
Have a nice weekend :-)
#4

[eluser]leviathan28[/eluser]
Of course it works ^^ thanks again.




Theme © iAndrew 2016 - Forum software by © MyBB