Page 1 of 2

[SOLVED] Adding customer's name to mail

Posted: Sun Jan 09, 2011 7:29 pm
by philbydevil
If I'm sending out an email to my entire store, is there a way that I can personalize the email so it includes the customer's name?

Something like:

Hello {NAME},

We have great products.

Regards,

philbydevil

{NAME} = customer's first name

I tried : http://forum.opencart.com/viewtopic.php ... il#p123544 but didn't have any luck.

Thanks.

EDIT-SOLVED: this is now a VQMod so you don't have to edit your core file: viewtopic.php?f=121&t=26380

Re: Adding customer's name to mail

Posted: Mon Jan 10, 2011 6:55 pm
by philbydevil
Sorry to bump this up but I was hoping that someone might have an answer (even if it's just yes/no). I've just switched my old cart off and OC is now live, so need to send an email out (within the next 24hrs) to my customers to let them know about reseting their password, etc.

Sounds a bit more professional (and less like a scam) if the email has their name on it.

If there's no mod/extension already done then I'll just do a nameless email.

[SOLVED - MAYBE] Adding customer's name to mail

Posted: Tue Jan 11, 2011 2:51 pm
by philbydevil
I believe that I have solved this. If someone who actually knows php wants to look over the file attached to this post (http://forum.opencart.com/viewtopic.php ... il#p123544), and let me know if there are any problems/issues it would be appreciated.

Re: [SOLVED - MAYBE] Adding customer's name to mail

Posted: Tue Jan 11, 2011 4:40 pm
by philbydevil
There is a problem with the mod - it only sends to one email address (the first on the list), even if multiple are selected. Will have to look into this.

Re: [ALMOST SOLVED] Adding customer's name to mail

Posted: Wed Jan 12, 2011 6:51 am
by philbydevil
Could someone with some php skills please have a look at the file attached to the post listed above as I've tried about 13 slightly different changes but still can't get it working (and really, I don't know what I'm doing).

It's so seems close....

Re: [ALMOST SOLVED] Adding customer's name to mail

Posted: Wed Jan 12, 2011 9:25 am
by afwollis
tell once more - what EXACTLY you need.
5 posts and many self-linked questions...

give us an URLs where you trying to do some actions.
describe what actions you trying to do...

and better in step-by-step form, i.e.:
1) what you have (URL || files || code)
2) what you going to apply/change (buttons/links clicked || new code blocks used)
3) what should you get (describe expected result)
4) what you got instead (describe what you've got in real)

Re: [ALMOST SOLVED] Adding customer's name to mail

Posted: Wed Jan 12, 2011 9:36 am
by philbydevil
It actually explains all of this on the other thread that is mentioned above:

http://forum.opencart.com/viewtopic.php ... il#p123544

But here it is in relation to your questions:

1. File: admin/controller/sale/contact.php (file is attached to other thread). Code: see other thread.

2. See other thread. No buttons/links changed. I didn't make the original changes to the code. I did however, modify the code listed on that thread. What I did was change all instances of 'name' to 'firstname', and merged it with my current (1.4.9.3) contact.php file (this is the file that is attached to that thread).

3. When writing an email, if you type in {NAME}, the email sent will change this to the customers first name. If you type in {EMAIL} it will change it to the customer's email address (these are usually known as "fields" in mail merge applications).

4. {NAME} and {EMAIL} both work, but when sending to multiple contact, only the first contact/customer on the list actually receives the email. When sending to one contact, it works fine.

So, the mail merge ({NAME}/{EMAIL) is working correctly. The only bug is that of sending to multiple contacts/customers. Thanks.

Re: [ALMOST SOLVED] Adding customer's name to mail

Posted: Wed Jan 12, 2011 1:02 pm
by philbydevil
Getting more specific, I believe that the problem lies somewhere in this modified code:

Code: Select all

			// Prevent Duplicates
			$emails = array_unique($emails);
			
			if ($emails) {
                $MailTitle = $this->request->post['subject'];
				$message  = '<html dir="ltr" lang="en">' . "\n";
				$message .= '<head>' . "\n";
				$message .= '<title>{TITLE}</title>' . "\n";
				$message .= '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' . "\n";
				$message .= '</head>' . "\n";
				$message .= '<body>' . html_entity_decode($this->request->post['message'], ENT_QUOTES, 'UTF-8') . '</body>' . "\n";
				$message .= '</html>' . "\n";

				$attachments = array();

				if (preg_match_all('#(src="([^"]*)")#mis', $message, $matches)) {
					foreach ($matches[2] as $key => $value) {
						$filename = md5($value) . strrchr($value, '.');
						$path = rtrim($this->request->server['DOCUMENT_ROOT'], '/') . parse_url($value, PHP_URL_PATH);
						
						$attachments[] = array(
							'filename' => $filename,
							'path'     => $path
						);
						
						$message = str_replace($value, 'cid:' . basename($filename), $message);
					}
				}	
				
				foreach ($emails as $email) {
                    $TmpTitle = $MailTitle;
                    $TmpBody = $message;
                    $TmpTitle = str_replace("{NAME}",$email["firstname"],$TmpTitle);
                    $TmpTitle = str_replace("{EMAIL}",$email["email"],$TmpTitle);
                    $TmpBody = str_replace("{NAME}",$email["firstname"],$TmpBody);
                    $TmpBody = str_replace("{EMAIL}",$email["email"],$TmpBody);
                    $TmpBody = str_replace("{TITLE}",$TmpTitle,$TmpBody);
					$mail = new Mail();	
					$mail->protocol = $this->config->get('config_mail_protocol');
					$mail->parameter = $this->config->get('config_mail_parameter');
					$mail->hostname = $this->config->get('config_smtp_host');
					$mail->username = $this->config->get('config_smtp_username');
					$mail->password = $this->config->get('config_smtp_password');
					$mail->port = $this->config->get('config_smtp_port');
					$mail->timeout = $this->config->get('config_smtp_timeout');				
					$mail->setTo($email['email']);
					$mail->setFrom($this->config->get('config_email'));
					$mail->setSender($store_name);
					$mail->setSubject($TmpTitle);					
					
					foreach ($attachments as $attachment) {
						$mail->addAttachment($attachment['path'], $attachment['filename']);
					}
					
					$mail->setHtml($TmpBody);
					$mail->send();
				}
			}
I don't think that the other changes have caused the sending problem, but you never know:

From:

Code: Select all

						foreach ($results as $result) {
							$emails[$result['customer_id']] = $result['email'];
						}
To:

Code: Select all

						foreach ($results as $result) {
							$emails[] = array(
                                'firstname'           => $result['firstname'],
                                'email'          => $result['email']                                  
                            );
						}
AND

From:

Code: Select all

					if ($customer_info) {
						$emails[] = $customer_info['email'];
					}
				}
			}
To:

Code: Select all

					if ($customer_info) {
                        $emails[] = array(
                            'firstname'           => $customer_info['firstname'],
                            'email'          => $customer_info['email']
                        );
						//$emails[] = $customer_info['email'];
					}
				}
			}

Re: [ALMOST SOLVED] Adding customer's name to mail

Posted: Thu Jan 13, 2011 12:37 am
by afwollis
ok, I'll help you solve it, but there (admin/index.php?route=sale/contact) a few different ways/types of mailing.
tell me (or give me a screenshot) what options (see my attached screenshot) are you choosing on that page

Re: [ALMOST SOLVED] Adding customer's name to mail

Posted: Thu Jan 13, 2011 4:57 am
by philbydevil
The testing I've been doing has been sending emails to 1 - 3 customers:
Mail.jpg

Mail.jpg (19.32 KiB) Viewed 9169 times

As mentioned above, I can get it to work with 1 customer/email, but when I add any additional customers through "Search", only the first customer will receive the email. I am unable to test "To: All Customers" or any other way as this is a live store.

It does really need to work with all ways of sending emails:

"Search" for customer/email address

OR

"To: All Customers"

OR

"To: All Newsletter Subscribers"

OR

"To customers who have ordered Products"

I have been attempting to modify all ways, but can only test the way it's being done in my screenshot, as I'm working with a live store.

Your help is appreciated.

Re: [ALMOST SOLVED] Adding customer's name to mail

Posted: Thu Jan 13, 2011 6:18 am
by philbydevil
Ok. Just tested "To: All Customers" and "To: All Newsletter Subscribers" and it's the same problem: email is only sent to one person.

{NAME} and {EMAIL} still work.

So it seems that the coding problem may actually be related to the parts above that I thought it wasn't related to.

Re: [ALMOST SOLVED] Adding customer's name to mail

Posted: Thu Jan 13, 2011 10:08 pm
by afwollis
the problem is on lines 80-81:

Code: Select all

			// Prevent Duplicates
			$emails = array_unique($emails);
after this step $emails array contains only ONE email-data.

so, all file need to be rewrited.
it would be better to use this style of getting emails-array

Code: Select all

$emails[$result['email']] = array(
							'firstname'           => $result['firstname']
						);
instead of current

Code: Select all

$emails[] = array(
							'firstname'           => $result['firstname'],
							'email'          => $result['email']
						);
I will post a fixed file (contact.php) here later this evening

Re: [ALMOST SOLVED] Adding customer's name to mail

Posted: Fri Jan 14, 2011 6:26 am
by philbydevil
Hello afwollis. Thanks for looking into this. I do look forward to seeing the file you post as I couldn't get the above code to work (I'm probably leaving something out).

Re: [ALMOST SOLVED] Adding customer's name to mail

Posted: Fri Jan 14, 2011 7:32 am
by afwollis
Well, I revised my opinion about file rewriting.
So here is my last variant:

Code: Select all

			$customers = $emails;
			$emails = array();
			foreach($customers as $customer) {
				if (!in_array($customer['email'], $emails)) {
					$emails[$customer['email']] = array(
						'firstname'           => $customer['firstname'],
						'email'          => $customer['email']
					);
				}
			}
			
			// Prevent Duplicates
			//$emails = array_unique($emails);
It is the only changes within the contact.php file.
Not counting two commented out debug_prints

Re: [SOLVED] Adding customer's name to mail

Posted: Fri Jan 14, 2011 8:47 am
by philbydevil
Excellent work afwollis! Thank you for resolving this, I really do appreciate it. I'm sure that many people will find this useful.

I've tested the changes on my live store (v1.4.9.3) and it is working for:

"Search" for customer/email address

"To: All Customers"

"To: All Newsletter Subscribers"

Haven't tested for:

"To customers who have ordered Products"

But I am confident that it will work.

For those that want to use this modification, here are the fields to use in your emails:

{NAME} = customer's first name
{EMAIL} = customer's email address
{TITLE} = subject of email (ie. the subject that you enter when composing the email)

Example:

Dear {NAME},

Your registered email address is: {EMAIL}

Thanks,

Store Owner

I don't know when you would actually use the {TITLE} field, but it was in the code from the other thread.

Thanks again afwollis!

Re: [SOLVED] Adding customer's name to mail

Posted: Fri Jan 14, 2011 11:45 am
by philbydevil
I've just added a new field:

{SURNAME} = customer's last name

Updated file attached. Just rename to contact.php and upload to admin/controller/sale

Make sure you back up first.

Thanks again to afwollis for getting this to work!

Re: [SOLVED] Adding customer's name to mail

Posted: Fri Jan 14, 2011 11:55 am
by marc_cole
Thanks for posting this, but you should really remove the old copy, especially if there are still problems with it, so that some unsuspecting person doesn't download it by mistake.

Re: [SOLVED] Adding customer's name to mail

Posted: Fri Jan 14, 2011 12:00 pm
by philbydevil
My old/not working file has been removed from other thread (thanks marc).

Both files in this thread are working copies.

Re: [SOLVED] Adding customer's name to mail

Posted: Sat Jan 15, 2011 3:52 am
by marc_cole
Another suggestion. You should post this as a VQMod script to make it easy for everyone to implement without messing around with the core file. If you haven't tried VQMod yet, you really should. It's truly amazing.

Re: [SOLVED] Adding customer's name to mail

Posted: Sat Jan 15, 2011 4:17 am
by philbydevil
I'll have to have a look into VQMod. I've read the sticky thread about it but haven't played around with it yet.

EDIT: this is now a VQMod so you don't have to edit your core file: http://forum.opencart.com/viewtopic.php?f=121&t=26380