Problem inserting new line \n

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
Shenz
Forum Newbie
Posts: 16
Joined: Sat Nov 25, 2006 10:47 am

Problem inserting new line \n

Post by Shenz »

When using this code it gives de wrong output:

Code: Select all

if ($confirm->isConnected()) {
		
			$Message = "$name,\n\n";
			$Message .= "Thank you for registering your $product with Lum LCE.\n\n";
			$Message .= "Registration details:\n";
			$Message .= "=====================\n\n";
			$Message .= "Name: $name";
			$Message .= "\nCompany: $company";
			$Message .= "\nAddress: $address1";
			$Message .= "\n          $address2";
			$Message .= "\n          $postcode $city";
			$Message .= "\n          $country";
			$Message .= "\nTelephone: $telephone";
			$Message .= "\nE-mail: $email";
			$Message .= "\n\nProduct: $product";
			$Message .= "\nSerial: $serial";
			$Message .= "\nDealer: $dealer";
			$Message .= "\nPurchase date: $purchase_date";
			$Message .= "\n\nKind regards,\n\nLum Equipment.\n";
			
			$confirm->addPart($Message);
			
			if(!$confirm->send("$name <$email>", '$sender, 'Product registration'))
Output:

Code: Select all

Name: Egwin
Company: Address: Boomstraat 10
                  3940 Hechtel
         Belgium
Telephone: E-mail: bla@bla.be

Product: Ethernet-DMX8
Serial: LUM0085111
Dealer: Lum
Purchase date: 2004-05-11
'Addres:' and 'E-mail:' are supposed to start on a new line, but they don't. If the fields Company en Telephone are filled in then Address and E-mail start on a new line.
I tried to send my message with the PHP mail() funtion to see what output it would generate and here everything was the way it was supposed to be.
Can somebody explain me what's going wrong and how I can fix it?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

There's a syntax error in your call to send() above (a lone quote).

That's not causing this though. I'll have to have a closer look, but have you run this with full error reporting on? Does those variables exist all the time even if they are not filled in? If not, they'll be generating notices and choking the script.

If that's not the issue, change \n to \r\n for now and see if it helps. I'm not sure what could cause that issue though.

Code: Select all

error_reporting(E_ALL); //At the top of your script
Shenz
Forum Newbie
Posts: 16
Joined: Sat Nov 25, 2006 10:47 am

Post by Shenz »

I had some errors indeed. But now I've tested it without the variables:

Code: Select all

$header = "name,\n\n";
			$header .= "Thank you for registering your product with Lum LCE.\n\n";
			$header .= "Registration details:\n";
			$header .= "=====================\n\n";
			$body = "Name: name";
			$body .= "\r\nCompany: ";
			$body .= "\r\nAddress: address1";
			$body .= "\r\n          address2";
			$body .= "\r\n          postcode $city";
			$body .= "\r\n          country";
			$body .= "\r\nTelephone: ";
			$body .= "\nE-mail: email";
			$body .= "\n\nProduct: product";
			$body .= "\nSerial: serial";
			$body .= "\nDealer: dealer";
			$body .= "\nPurchase date: purchase_date";
			$footer = "\n\nKind regards,\n\nLum.\n";
			
			$Message = $header . $body . $footer;
			
			$confirm->addPart($Message);
			
			if(!$confirm->send("$name <$email>", 'bla@bla.be', 'Lum product registration'))
				echo '<span class="alert">Confirmation email could not be sent.</span>';
			
			
			$confirm->close();
Gives output:

Code: Select all

name,

Thank you for registering your product with Lum.

Registration details:
=====================

Name: name
Company: Address: address1
         address2
         postcode city
         country
Telephone: E-mail: email

Product: product
Serial: serial
Dealer: dealer
Purchase date: purchase_date

Kind regards,

Lum.
It doesn't matter if I use \n or \r\n. But now I did some further tests and I it seems that a simple space character just before a double quote is causing the problem.
Test 1 (no space before closing double quote)

Code: Select all

$body = "Name: name";
			$body .= "\r\nCompany:";
			$body .= "\r\nAddress: address1";
Output test 1

Code: Select all

Name: name
Company:
Address: address1
         address2
Test 2 (space added before closing double quote)

Code: Select all

$body = "Name: name ";
			$body .= "\r\nCompany: ";
			$body .= "\r\nAddress: address1";
Output test 2

Code: Select all

Name: name Company: Address: address1
How to fix this problem?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Thanks for the clarification. I'll have a look but if you can work around it for a week or two there's a new version coming out. Otherwise, if there's an obvious bug I'll fix it in subversion and make a quick last minute release of version 2.

I'll post back tomorrow ;)
Shenz
Forum Newbie
Posts: 16
Joined: Sat Nov 25, 2006 10:47 am

Post by Shenz »

Did you already find if this is a bug or not?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Whoops sorry. Which connector are you using? Is it SMTP, Sendmail or NativeMail?

Code: Select all

/**
	 * Converts lone LF characters to CRLF
	 * @param  string  input
	 * @return  string  converted output
	 */
	public function LFtoCRLF($string)
	{
		return preg_replace("@(?:(?<!\r)\n)|(?:\r(?!\n))@", "\r\n", $string);
	}
	/**
	 * Prevents premature <CRLF>.<CRLF> strings
	 * Converts any lone LF characters to CRLF
	 * @param  string  input
	 * @return  string  escaped output
	 */
	public function makeSafe($string)
	{
		return str_replace("\r\n.", "\r\n..", $this->LFtoCRLF($string));
	}
Those two method are the only bits which would modify your data that way, unless of course QP encoding has a bug (I've not been informed of one) and your code happens to use it (you may not be aware of it if it does). If anybody can see an issue with the above do say so :)

If it's a QP encoding problem $confirm->addPart($Message, "text/plain", "8bit"); would help.

If you were using NativeMail there are some modifications made against the data so if you let me know which connector you use I can rule that out.
Shenz
Forum Newbie
Posts: 16
Joined: Sat Nov 25, 2006 10:47 am

Post by Shenz »

I'm using the SMTP connector.

Using $confirm->addPart($Message, "text/plain", "8bit"); doesn't help :(. I still get the same output, aaaaaargh.
Shenz
Forum Newbie
Posts: 16
Joined: Sat Nov 25, 2006 10:47 am

Post by Shenz »

Are there other possibilities I can try? Or do I just have to wait for the new version of Swiftmailer to see if that one will work correctly?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Shenz wrote:Are there other possibilities I can try? Or do I just have to wait for the new version of Swiftmailer to see if that one will work correctly?
Sorry I've decided not to make any more updates to version 2 right now unless they are critical. v3 is sitting in a branch of svn due to very shortly hit the trunk. I'm just tying up some loose ends then I need to document it, convert to PHP4 and release the first rc.

We're only looking at around about a week now. I'm working on this every day and taking time out during quiet patches at work to get this completed. Hopefully the rc will be stable and just become 3.0.0 since the test coverage is very solid.

I basically just need to tie in the complete (updated) API for plugins and write some smoke tests before doing docs and a translation to PHP4.
Post Reply