Automatic Email response + adding content to numbers.

Hi guys,
I’m a game developer and we sell “early access invitations” through our website.
We accept payments with paypal.
Everytime we get a payment, we get an email from paypal with the info about the purchase and the buyer. Then we have to invite the buyer for the early access.
The way we invite the people now is: We send them manually a “thank you…” email and then we include their data into a numbers file and then we invite them manually in the browser to our beta.

You can imagine how much work it is doing this routine work of sending out the invitations.
I would like to build something which doing all this routine work for us.
I read a lot and find out, that every time we receive an email from paypal, “rules” im mail can check up the email and start an appleScript.
This appleScript should reply to the buyer automatically with his name in the topic like:
Dear “buyerName”, thank you for your purchase.

After the automatically reply the script should copy some informations from the email and put it automatically in a numbers file in the last row.

After the numbers thing is done, the appleScript should call an Automator workflow.
This workflow sends out the invitation through the browser.

Mail rules and automator is easy for me.
But appleScript I have to learn and it takes so much time.
Can someone maybe help me out here a little bit ?

How can I copy exact informations from the email ?
Like every email content includes a field where the name of the person is. How can I find this out ?
And also how do I include the copied information into a numbers file ?

I already know how to start a Automator workflow through appleScript.
I also already build a automatically reply script, but the name in the topic isn’t there, because I don’t know how to get the name from the email content.

Hope someone can help me here.

Thank you so much in advance and best regards!
Dimitri

Here is a simple script which extract the name of every sender of selected messages.
It would be easy to edit it so that it may be triggered by a rule but I’m not sure that it’s what you want.

tell application "Mail"
	set theMessages to get selection
	set theSenders to {}
	repeat with aMessage in theMessages
		set end of theSenders to (get extract name from sender of aMessage)
	end repeat
end tell
log theSenders (*Quark Software Inc., MacWay, Carrefour Antibes & moi, EDF ENR, Fleurus Presse, Box Updates, André Willemse, eBay, Mosaic, Maison a Part, À VIVRE éditions*)

set nbNews to count theSenders
set docName to "essai-jeu.numbers" # Replace by the real name
set numbersDoc to (path to desktop as text) & docName # Edit to match the real pathname
tell application "Numbers"
	open file numbersDoc
	tell document docName to tell sheet "Feuille 1" to tell table "Tableau 1" # Edit to fit the real names
		set nbRows to count rows
		# Here I assume that the table has one header row,
		# that gamers names are stored in column B
		# and that the cell A1 contains the formula : = NBVAL (B)
		set lowerUsedRow to 1 + (value of cell "A1")
		if (lowerUsedRow + nbNews) > nbRows then
			repeat nbNews times
				try # useful if the count of rows reach the limit of 65535
					add row below row lowerUsedRow
				end try
			end repeat
		end if
		repeat with i from 1 to nbNews
			try # useful if the count of rows reach the limit of 65535
				set value of cell (lowerUsedRow + i) of column 2 to theSenders's item i
			on error
				error "CAUTION, only " & i - 1 & " values among " & nbNews & " ones are stored !"
			end try
		end repeat
	end tell
end tell

When the names are extracted they are inserted in a Numbers document.

The sender’s name is not guaranteed to be the name which you have to extract because the correspondant is not forced to use its true name in the game. He may use a pseudo.
It he use a pseudo, extracting it from the body of a message would require details about what may allow the script to determine which string in the body is really the name.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) dimanche 20 septembre 2015 19:42:31

Hi Yvan,
thanks for your help!
The emails we get from paypal are looking like this:

I made all private stuff black here, but two fields I made red.
The first red text is the full name of the buyer,
The second field is his email.

Is it possible to get this name from the email content ?
Its always the first line under the word “Käufer”.

I already trigger the appleScript through a rule in mail.
Every time I get an email, the rule checks if it was a purchase email or not.
If yes, it starts the appleScript.

The senders email of those purchase emails are always member@paypal.de.
The email of the buyer is in a field down below called “reply to” (“Antwort an” on german). (or also under the full name that I want to get from the mail content)

Here this is my current code that runs after the rule has triggered it:

[code]set mailContent to "Dear NAME,

thank you for purchasing …"

set theSubject to “Thank you for purchasing!”

tell application “Mail”
– GET USERS PAYPAL MESSAGE –
set userMessage to message 1 of mailbox “INBOX” of account “my_email@account.com
– CREATE NEW OUTGOING EMAIL –
set outMessage to make new outgoing message with properties {visible:false, subject:theSubject, content:mailContent}
– GET OUTADDRESS –
set outAddress to reply to of userMessage
tell outMessage
make new to recipient with properties {address:outAddress}
send outMessage
end tell
end tell
– PUT DATA IN LIST –
tell application “Numbers”

end tell
– START AUTOMATOR WORKFLOW –


– WAIT 30 seconds (for the automator to finish the workflow) –


– CHECK FOR OTHER UNREAD EMAILS --[/code]
Also it would be cool, if at the end of this whole process, the script would check my email account for unread emails. And if it finds an unread message, it should check if the subject is containing the words “product name” for example.
If yes it should start the whole process for this email again.
This way the system would be able to catch up on the unreplied emails when something goes wrong (for example computer crashed for a while).

I know thats a lot, but It would be sooo perfect if we can get this process automated like this.
We also would like to pay you for this work!
Thank you for your help!!!

best regards
Dimitri

You may use that as a starting point.


-- set theKey to linefeed & "Marchand" & linefeed # I tested with mails where I am the customer so its the vendor which is referenced.
set theKey to linefeed & "Käufer" & linefeed # Awful typo corrected

tell application "Mail"
	set theMessages to get selection
	set theParameters to {}
	repeat with aMessage in theMessages
		tell (contents of aMessage)
			set outAddress to its reply to
			-- log result (*service@paypal.fr*)
			set itsContent to its content # text object
		end tell
		
		if itsContent contains theKey then
			set maybe to item 2 of my decoupe(itsContent, theKey)
			set customerName to paragraph 1 of maybe
			-- log result (*Fleurus Presse*)
			set customerMailAddress to paragraph 2 of maybe
			-- log result (*a.peys@fleuruspresse.com*)
			set end of theParameters to {outAddress, customerName, customerMailAddress}
		end if
	end repeat
end tell
theParameters
--> {{"service@paypal.fr", "DIRECT CAFE SARL", "contact@directcafe.fr"}, {"service@paypal.fr", "JV2R", "paypal@macway.com"}, {"service@paypal.fr", "Fnac Direct", "service-clientele@fnac.com"}, {"service@paypal.fr", "Fleurus Presse", "a.peys@fleuruspresse.com"}}

#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

The return values are those of tradesmen or companies so I have no problem to insert them.
It’s an old fashioned code. Maybe it may be enhanced which modern features.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) lundi 21 septembre 2015 21:15:22

Hi Yvan,
what exactly should I do with the code ?
I’m a big noob in that.
I’m just copy pasting it and running it.
What I get is a result like this: {}

thanks and best regards
Dimitri

I assumed that the instruction :

set theMessages to get selection

didn’t required comments.

It seems that I was wrong.

Did you selected some Paypal mails before running the script ?
It’s a basic requirement.

I will not waste time sending fake Paypal messages to one of my mailboxes just to test a rule script.
I gave you a “standard” script which you may test easily as is before encapsulating it in a rule script.

If it continue to return a blank list when Paypal mails with the wanted data it may be that in Germany Paypal use return characters when it use linefeeds in France.
If it’s the case you must edit one instruction :
set theKey to linefeed & “Käufer” & linefeed
would become :
set theKey to return & “Käufer” & return

Without the line break characters, the script extract exotic values from mails where the word “Käufer” is not used as a balise announcing the customer’s name and its mail address.

If you want, you may forward one of these mails after replacing the personal datas by fake values.
A text one, not a screenshot or pdf like what you inserted here.
My mailbox address is : koenig yvan sfr fr
I will be back tomorrow.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) lundi 21 septembre 2015 22:42:07

Hi Yvan,
seems to work now :smiley:
I changed
set theKey to return & “Käufer” & linefeed
to
set theKey to linefeed & “Käufer” & linefeed

and now I get a result like this:
{{“customer@email.com”, “Full Name”, “customer@email.com”}}

Perfect now I have the data to do an personally auto reply!
What should I do next ?

Thank you and best regards!!!
Dimitri

I apologize for the typo. I corrected it in the message.

In my first message I gave you a script built to store in a Numbers document the name of the author of a mail.
May you try to do by yourself a synthesis of the two scripts ?
Of course, I’m able to do the job but I’m sure that if you scrap your head a bit trying to solve the problem you will learn useful things.
When you will post the result of your attempts, I will be glad to read it and correct possible anomalies.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) mardi 22 septembre 2015 17:03:05

I’m puzzled by a detail.

You posted :

--.
-- GET OUTADDRESS --
set outAddress to reply to of userMessage
tell outMessage
make new to recipient with properties {address:outAddress}
send outMessage
end tell
end tell
--.

According to the example which you posted, I’m not sure that your mails must be sent to what you extract as outAddress. In your example outAddress is the mail address of Paypal.
My understanding is that the mail must be sent to what I extracted from the content of the received mail in the variable customerMailAddress.

Am’I right ?

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) mardi 22 septembre 2015 20:58:21

Hi Yvan,
yes you’re right.
But in my old code I also got the right reply email.
I got it from the “reply to” from my “message”.

I’m working on the numbers code right now.
In think it will work but the formula = NBVAL (B) seems to be a france formula.
what exactly is this formula doing ?
I need to find the german one. :smiley:

[code]-- set theKey to linefeed & “Marchand” & linefeed # I tested with mails where I am the customer so its the vendor which is referenced.
set theKey to linefeed & “Käufer” & linefeed

tell application “Mail”
set theMessages to get selection
set theParameters to {}
repeat with aMessage in theMessages
tell (contents of aMessage)
set outAddress to its reply to
– log result (service@paypal.fr)
set itsContent to its content # text object
end tell

	if itsContent contains theKey then
		set maybe to item 2 of my decoupe(itsContent, theKey)
		set customerName to paragraph 1 of maybe
		-- log result (*Fleurus Presse*)
		set customerMailAddress to paragraph 2 of maybe
		-- log result (*a.peys@fleuruspresse.com*)
		set end of theParameters to {outAddress, customerName, customerMailAddress}
	end if
end repeat

end tell
theParameters

on decoupe(t, d)
local oTIDs, l
set {oTIDs, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, d}
set l to text items of t
set AppleScript’s text item delimiters to oTIDs
return l
end decoupe

– ADD TO NUMBERS –
set docName to “essai-jeu.numbers” # Replace by the real name
set numbersDoc to (path to desktop as text) & docName # Edit to match the real pathname
tell application “Numbers”
open file numbersDoc
tell document docName to tell sheet “Blatt 1” to tell table “Tabelle 1”
set nbRows to count rows
set lowerUsedRow to 1 + (value of cell “A1”)

	try # useful if the count of rows reach the limit of 65535
		set value of cell (lowerUsedRow + 1) of column 2 to customerName
		set value of cell (lowerUsedRow + 1) of column 3 to customerMailAddress
	on error
		
	end try
	
end tell

end tell[/code]

Ok seems like I celebrated to early.
I don’t know why, but it stopped adding rows to the list.
After it added two rows it stop working now.

I still have the mailbox selected.
The numbers list opens when I run the code, but no new row there.
Thats weird.

Did I something wrong ?

EDIT:

Ah no it works again sorry,
I have to create empty rows before I run the code, because it doesn’t create it itself.

best regards
Dimitri

(1) Don’t put the handler(s) in the main code.
Put all of them at the beginning or all of them at the very end.

(2) You dropped the instructions carefully inserted to urge the script to insert the needed empty rows at the bottom
Not surprising that you got an odd result.

Here is a version which extract more data from the mails.
I thought that it would be useful to store them in the Numbers document and maybe use them to build the body of the built mails.
As is it build drafts of the mails.
For safe I disabled the send outMessage instruction.

set speakFrench to false

if speakFrench then
	set theKey1 to "Nº de transaction : "
	set theKey2 to linefeed & "Marchand" & linefeed # I tested with mails where I am the customer so its the vendor which is referenced.
	set theKey3 to "Montant" & linefeed
	set theKey4 to "Paiement" & linefeed
	set theKey5 to "Numéro de facture :"
else
	set theKey1 to "Transaktioncode :"
	set theKey2 to linefeed & "Käufer" & linefeed # Awful typo corrected
	set theKey3 to "Betrag" & linefeed
	set theKey4 to "Zahlung" & linefeed
	set theKey5 to "Rechnungsnummer :" CAUTION, check the spelling. I just translated the french one.
end if

# Decipher the mails

tell application "Mail"
	set theMsgs to get selection
	set theParameters to {}
	repeat with aMessage in theMsgs
		tell (contents of aMessage)
			set SenderAddress to its reply to
			-- log result (*service@paypal.fr*)
			set itsContent to its content # text object
		end tell
		if itsContent contains theKey1 then
			set Transaktioncode to paragraph 1 of item 2 of my decoupe(itsContent, theKey1)
			-- log result (*69R45671NK5693338*)
		else
			set Transaktioncode to ""
			log "missing1"
		end if
		
		if itsContent contains theKey2 then
			set maybe to item 2 of my decoupe(itsContent, theKey2)
			set customerName to paragraph 1 of maybe
			-- log result (*Centrale d'Achat IDLABU*)
			set customerMailAddress to paragraph 2 of maybe
			-- log result (*service-client@idlabu.com*)
			
			if itsContent contains theKey3 then
				set Object to paragraph 1 of item 2 of my decoupe(itsContent, theKey3)
				-- log result (*Séchoir à linge BRABANTIA BRA-654321*)
			else
				set Object to ""
				log "missing3"
			end if
			
			if itsContent contains theKey4 then
				set Amount to paragraph 1 of item 2 of my decoupe(itsContent, theKey4)
				-- log result (*€59,00 EUR*)
			else
				set Amount to ""
				log "missing4"
			end if
			if itsContent contains theKey5 then
				set InvoiceNum to paragraph 1 of item 2 of my decoupe(itsContent, theKey5)
				-- log result (*1PEI4ZI66TF7C*)
			else
				set InvoiceNum to ""
				log "missing5"
			end if
			set end of theParameters to {SenderAddress, Transaktioncode, customerName, customerMailAddress, Object, Amount, InvoiceNum}
		end if
	end repeat
	
	set nbNews to count theParameters
	repeat with i from 1 to nbNews
		set {SenderAddress, Transaktioncode, customerName, customerMailAddress, Object, Amount, InvoiceNum} to item i of theParameters
		set mailContent to "Dear " & customerName & ", 

thank you for purchasing ......."
		
		set theSubject to "Thank you for purchasing!"
		-- CREATE NEW OUTGOING EMAIL --
		set outMessage to make new outgoing message with properties {visible:false, subject:theSubject, content:mailContent}
		tell outMessage
			make new to recipient with properties {address:customerMailAddress}
			-- send outMessage
		end tell
	end repeat
end tell # Mail
-- theParameters
--> {{"service@paypal.fr", "DIRECT CAFE SARL", "contact@directcafe.fr"}, {"service@paypal.fr", "JV2R", "paypal@macway.com"}, {"service@paypal.fr", "Fnac Direct", "service-clientele@fnac.com"}, {"service@paypal.fr", "Fleurus Presse", "a.peys@fleuruspresse.com"}}

#==========
set docName to "essai-jeu.numbers" # Replace by the real name
set numbersDoc to (path to desktop as text) & docName # Edit to match the real pathname
# Below I assume that the table has one header row,
set columnSenderAddress to 2 # outAddresses are stored in column B
set columnCustomerName to 3 # customerName are stored in column C
set columnCustomerMailAddress to 4 # customerMailAddress are stored in column D
set columnTransaktioncode to 5
set columnObject to 6
set columnAmount to 7
set columnInvoiceNum to 8

# and that the cell A1 contains the formula : = NBVAL (B)
#==========

tell application "Numbers"
	open file numbersDoc
	tell document docName to tell sheet "Feuille 1" to tell table "Tableau 1" # Edit to fit the real names
		set nbRows to count rows
		
		set lowerUsedRow to 1 + (value of cell "A1")
		if (lowerUsedRow + nbNews) > nbRows then
			repeat nbNews times
				try # useful if the count of rows reach the limit of 65535
					add row below row lowerUsedRow
				end try
			end repeat
		end if
		repeat with i from 1 to nbNews
			set {SenderAddress, Transaktioncode, customerName, customerMailAddress, Object, Amount, InvoiceNum} to item i of theParameters
			try # useful if the count of rows reach the limit of 65535
				tell row (lowerUsedRow + i)
					set value of cell columnSenderAddress to outAddress
					set value of cell columnCustomerName to customerName
					set value of cell columnCustomerMailAddress to customerMailAddress
					set value of cell columnTransaktioncode to Transaktioncode
					set value of cell columnObject to Object
					set value of cell columnAmount to Amount
					set value of cell columnInvoiceNum to InvoiceNum
				end tell
			on error
				error "CAUTION, only " & i - 1 & " values among " & nbNews & " ones are stored !"
			end try
		end repeat
	end tell
end tell


#===== # The main code stop here, we may put handlers below.

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

For the late question,
I know two ways to urge Mail to grab the newly delivered messages.
(1) use an idle script but it would be completely different than the one triggered by your rule.
(2) use a daemon.

I apologize but I’m not at ease with these features.
The good scheme would be to :
(a) finish the code extracting the datas, creating/sending mails, writing datas in Numbers.
(b) open a new thread to ask for the new features.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) mardi 22 septembre 2015 22:55:50

Thats awesome!
Thank you Yvan!

I’ll trying to finish this now.
When I find out how to get the unread Emails I will post it here :slight_smile:

best regards
Dimitri

Here is a skeleton for a script scanning unread Mail messages.


on idle
	my Germaine()
	return 60 * 30 # will be triggered every 30 minutes
end idle

on run
	my Germaine()
end run

on Germaine()
	tell application "Mail"
		activate
		set unReadMsgs to messages of inbox whose read status is false
		# Put here the code treating the unread messages
		# Here I put a fake one
		set unreadSubjects to subject of messages of inbox whose read status is false
	end tell # Mail
	tell application "SystemUIServer" to display dialog my recolle(unreadSubjects, linefeed)
end Germaine

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

The run handler gives the ability to test the script from the Script Editor.
If you save the script as a stay-open application it will do its duty every 30 minutes.

When the system is used in Deutsch, save the script as a : Programm
check the box entitled : Nach run-Handler anzeigen
To quit the application, force it to quit thru the Apple menu or thru Activity Monitor.

Yvan KOENIG (VALLAURIS, France) mercredi 23 septembre 2015 17:21:17

The stay-open application may be used to do the entire job. You just have to filter the mails with a replicate of the filter used in your rule.
For instance you may choose to apply the entire treatment to the messages containing a link to paypal.

if itsContents contains “www.paypal.com” then

add it to the list theParameters

end if

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) jeudi 24 septembre 2015 11:42:02

Hi Yvan,
Wow thank you :smiley:
Unfortunately I can’t test it now, because we did some changes.
We changed our way we accept payments.
Now its not paypal only anymore, its a service that provides us also other payment methods.
For now I don’t know how the incoming emails of this service are look like. (When someone purchased)
I’m working on that right now to implement this service in our site.
When I’m done with that I will modify the code you wrote us to this new emails.

Thank you very much and best regards!
Dimitri

It was quite easy to edit the script so that it treat differently different sources of messages.
Here it’s ready for three different sources.


on idle
	my Germaine()
	return 60 * 30 # will be triggered every 30 minutes
end idle

on run
	my Germaine()
end run

on Germaine()
	tell application "Mail"
		activate
		set theMsgs to messages of inbox whose read status is false
		set msgsFromPaypal to {}
		set msgsFromTrucmuche to {}
		set msgsFromMachin to {}
		repeat with aMessage in theMsgs
			tell (contents of aMessage)
				set SenderAddress to its reply to
				set itsContent to its content # text object
			end tell
			
			if (SenderAddress contains "Paypal") or (itsContent contains "Paypal") then
				set aMessage to end of msgsFromPaypal
			else if (SenderAddress contains "Trucmuche") or (itsContent contains "Trucmuche") then
				set aMessage to end of msgsFromTrucmuche
			else if (SenderAddress contains "Machin") or (itsContent contains "Machin") then
				set aMessage to end of msgsFromMachin
			end if
		end repeat
	end tell # Mail
	
	set everyRetainedMsgs to {}
	if msgsFromPaypal ≠ {} then set everyRetainedMsgs to everyRetainedMsgs & my treatPaypal(msgsFromPaypal)
	if msgsFromTrucmuche ≠ {} then set everyRetainedMsgs to everyRetainedMsgs & my treatTrucmuche(msgsFromTrucmuche)
	if msgsFromMachin ≠ {} then set everyRetainedMsgs to everyRetainedMsgs & my treatMachin(msgsFromMachin)
	
	# Here we have the datas from every retained mails in a single list allowing us to call Numbers only once
	
	if everyRetainedMsgs + {} then
		set docName to "essai-jeu.numbers" # Replace by the real name
		set numbersDoc to (path to desktop as text) & docName # Edit to match the real pathname
		# Below I assume that the table has one header row,
		set columnSenderAddress to 2 # SenderAddresses are stored in column B
		set columnCustomerName to 3 # customerName are stored in column C
		set columnCustomerMailAddress to 4 # customerMailAddress are stored in column D
		set columnTransaktioncode to 5 # Transaktioncode are stored in column E
		set columnObject to 6 # Object are stored in column F
		set columnAmount to 7 # Amount are stored in column G
		set columnInvoiceNum to 8 # InvoiceNum are stored in column H
		# and that the cell A1 contains the formula : = NBVAL (B)
		# In English it would be = COUNTA (B)
		# In deutsch it would be = ANZHAL2 (B)
		
		set nbNews to count everyRetainedMsgs
		tell application "Numbers"
			open file numbersDoc
			tell document docName to tell sheet "Feuille 1" to tell table "Tableau 1" # Edit to fit the real names
				set nbRows to count rows
				
				set lowerUsedRow to 1 + (value of cell "A1")
				if (lowerUsedRow + nbNews) > nbRows then
					repeat nbNews times
						try # useful if the count of rows reach the limit of 65535
							add row below row lowerUsedRow
						end try
					end repeat
				end if
				repeat with i from 1 to nbNews
					set {SenderAddress, Transaktioncode, customerName, customerMailAddress, Object, Amount, InvoiceNum} to item i of everyRetainedMsgs
					try # useful if the count of rows reach the limit of 65535
						tell row (lowerUsedRow + i)
							set value of cell columnSenderAddress to SenderAddress
							set value of cell columnCustomerName to customerName
							set value of cell columnCustomerMailAddress to customerMailAddress
							set value of cell columnTransaktioncode to Transaktioncode
							set value of cell columnObject to Object
							set value of cell columnAmount to Amount
							set value of cell columnInvoiceNum to InvoiceNum
						end tell
					on error
						error "CAUTION, only " & i - 1 & " values among " & nbNews & " ones are stored !"
					end try
				end repeat
			end tell
		end tell
	end if
	
end Germaine

#=====

on treatPaypal(theMsgs)
	if (do shell script "defaults read 'Apple Global Domain' AppleLocale") starts with "fr_" then
		set theKey1 to "Nº de transaction : "
		set theKey2 to linefeed & "Marchand" & linefeed # I tested with mails where I am the customer so its the vendor which is referenced.
		set theKey3 to "Montant" & linefeed
		set theKey4 to "Paiement" & linefeed
		set theKey5 to "Numéro de facture :"
	else
		set theKey1 to "Transaktioncode :"
		set theKey2 to linefeed & "Käufer" & linefeed # Awful typo corrected
		set theKey3 to "Betrag" & linefeed
		set theKey4 to "Zahlung" & linefeed
		set theKey5 to "Rechnungsnummer :" # CAUTION, check the spelling. I just translated the french one.
	end if
	
	# Decipher the mails issued by Paypal
	
	tell application "Mail"
		set theParameters to {}
		repeat with aMessage in theMsgs
			tell (contents of aMessage)
				set SenderAddress to its reply to
				-- log result (*service@paypal.fr*)
				set itsContent to its content # text object
			end tell
			if itsContent contains theKey1 then
				set Transaktioncode to paragraph 1 of item 2 of my decoupe(itsContent, theKey1)
				-- log result (*69R45671NK5693338*)
			else
				set Transaktioncode to ""
				log "missing1"
			end if
			
			if itsContent contains theKey2 then
				set maybe to item 2 of my decoupe(itsContent, theKey2)
				set customerName to paragraph 1 of maybe
				-- log result (*Centrale d'Achat IDLABU*)
				set customerMailAddress to paragraph 2 of maybe
				-- log result (*service-client@idlabu.com*)
				
				if itsContent contains theKey3 then
					set Object to paragraph 1 of item 2 of my decoupe(itsContent, theKey3)
					-- log result (*Séchoir à linge BRABANTIA BRA-654321*)
				else
					set Object to ""
					log "missing3"
				end if
				
				if itsContent contains theKey4 then
					set Amount to paragraph 1 of item 2 of my decoupe(itsContent, theKey4)
					-- log result (*€59,00 EUR*)
				else
					set Amount to ""
					log "missing4"
				end if
				if itsContent contains theKey5 then
					set InvoiceNum to paragraph 1 of item 2 of my decoupe(itsContent, theKey5)
					-- log result (*1PEI4ZI66TF7C*)
				else
					set InvoiceNum to ""
					log "missing5"
				end if
				set end of theParameters to {SenderAddress, Transaktioncode, customerName, customerMailAddress, Object, Amount, InvoiceNum}
			end if
		end repeat
	end tell # Mail
	
	my buildMails(theParameters)
	return theParameters
end treatPaypal

#=====

on treatTrucmuche(theMsgs)
	# Of course you will have to edit this piece of code according to the structure of Trucmuche's messages
	if (do shell script "defaults read 'Apple Global Domain' AppleLocale") starts with "fr_" then
		set theKey1 to "Nº de transaction : "
		set theKey2 to linefeed & "Marchand" & linefeed # I tested with mails where I am the customer so its the vendor which is referenced.
		set theKey3 to "Montant" & linefeed
		set theKey4 to "Paiement" & linefeed
		set theKey5 to "Numéro de facture :"
	else
		set theKey1 to "Transaktioncode :"
		set theKey2 to linefeed & "Käufer" & linefeed # Awful typo corrected
		set theKey3 to "Betrag" & linefeed
		set theKey4 to "Zahlung" & linefeed
		set theKey5 to "Rechnungsnummer :" # CAUTION, check the spelling. I just translated the french one.
	end if
	
	# Decipher the mails issued by Trucmuche
	
	tell application "Mail"
		set theParameters to {}
		repeat with aMessage in theMsgs
			tell (contents of aMessage)
				set SenderAddress to its reply to
				-- log result (*service@trucmuche.fr*)
				set itsContent to its content # text object
			end tell
			if itsContent contains theKey1 then
				set Transaktioncode to paragraph 1 of item 2 of my decoupe(itsContent, theKey1)
				-- log result (*69R45671NK5693338*)
			else
				set Transaktioncode to ""
				log "missing1"
			end if
			
			if itsContent contains theKey2 then
				set maybe to item 2 of my decoupe(itsContent, theKey2)
				set customerName to paragraph 1 of maybe
				-- log result (*Centrale d'Achat IDLABU*)
				set customerMailAddress to paragraph 2 of maybe
				-- log result (*service-client@idlabu.com*)
				
				if itsContent contains theKey3 then
					set Object to paragraph 1 of item 2 of my decoupe(itsContent, theKey3)
					-- log result (*Séchoir à linge BRABANTIA BRA-654321*)
				else
					set Object to ""
					log "missing3"
				end if
				
				if itsContent contains theKey4 then
					set Amount to paragraph 1 of item 2 of my decoupe(itsContent, theKey4)
					-- log result (*€59,00 EUR*)
				else
					set Amount to ""
					log "missing4"
				end if
				if itsContent contains theKey5 then
					set InvoiceNum to paragraph 1 of item 2 of my decoupe(itsContent, theKey5)
					-- log result (*1PEI4ZI66TF7C*)
				else
					set InvoiceNum to ""
					log "missing5"
				end if
				set end of theParameters to {SenderAddress, Transaktioncode, customerName, customerMailAddress, Object, Amount, InvoiceNum}
			end if
		end repeat
	end tell # Mail
	
	my buildMails(theParameters)
	return theParameters
end treatTrucmuche

#=====

on treatMachin(theMsgs)
	# Of course you will have to edit this piece of code according to the structure of Machin's messages
	if (do shell script "defaults read 'Apple Global Domain' AppleLocale") starts with "fr_" then
		set theKey1 to "Nº de transaction : "
		set theKey2 to linefeed & "Marchand" & linefeed # I tested with mails where I am the customer so its the vendor which is referenced.
		set theKey3 to "Montant" & linefeed
		set theKey4 to "Paiement" & linefeed
		set theKey5 to "Numéro de facture :"
	else
		set theKey1 to "Transaktioncode :"
		set theKey2 to linefeed & "Käufer" & linefeed # Awful typo corrected
		set theKey3 to "Betrag" & linefeed
		set theKey4 to "Zahlung" & linefeed
		set theKey5 to "Rechnungsnummer :" # CAUTION, check the spelling. I just translated the french one.
	end if
	
	# Decipher the mails issued by Machin
	
	tell application "Mail"
		set theParameters to {}
		repeat with aMessage in theMsgs
			tell (contents of aMessage)
				set SenderAddress to its reply to
				-- log result (*service@machin.fr*)
				set itsContent to its content # text object
			end tell
			if itsContent contains theKey1 then
				set Transaktioncode to paragraph 1 of item 2 of my decoupe(itsContent, theKey1)
				-- log result (*69R45671NK5693338*)
			else
				set Transaktioncode to ""
				log "missing1"
			end if
			
			if itsContent contains theKey2 then
				set maybe to item 2 of my decoupe(itsContent, theKey2)
				set customerName to paragraph 1 of maybe
				-- log result (*Centrale d'Achat IDLABU*)
				set customerMailAddress to paragraph 2 of maybe
				-- log result (*service-client@idlabu.com*)
				
				if itsContent contains theKey3 then
					set Object to paragraph 1 of item 2 of my decoupe(itsContent, theKey3)
					-- log result (*Séchoir à linge BRABANTIA BRA-654321*)
				else
					set Object to ""
					log "missing3"
				end if
				
				if itsContent contains theKey4 then
					set Amount to paragraph 1 of item 2 of my decoupe(itsContent, theKey4)
					-- log result (*€59,00 EUR*)
				else
					set Amount to ""
					log "missing4"
				end if
				if itsContent contains theKey5 then
					set InvoiceNum to paragraph 1 of item 2 of my decoupe(itsContent, theKey5)
					-- log result (*1PEI4ZI66TF7C*)
				else
					set InvoiceNum to ""
					log "missing5"
				end if
				set end of theParameters to {SenderAddress, Transaktioncode, customerName, customerMailAddress, Object, Amount, InvoiceNum}
			end if
		end repeat
	end tell # Mail
	
	my buildMails(theParameters)
	return theParameters
end treatMachin

#=====

on buildMails(theParameters)
	set nbNews to count theParameters
	tell application "Mail"
		repeat with i from 1 to nbNews
			set {SenderAddress, Transaktioncode, customerName, customerMailAddress, Object, Amount, InvoiceNum} to item i of theParameters
			set mailContent to "Dear " & customerName & ", 

thank you for purchasing ......."
			
			set theSubject to "Thank you for purchasing!"
			-- CREATE NEW OUTGOING EMAIL --
			set outMessage to make new outgoing message with properties {visible:false, subject:theSubject, content:mailContent}
			tell outMessage
				make new to recipient with properties {address:customerMailAddress}
				-- send outMessage
			end tell
		end repeat
	end tell # Mail
	-- theParameters
	--> {{"service@paypal.fr", "DIRECT CAFE SARL", "contact@directcafe.fr"}, {"service@paypal.fr", "JV2R", "paypal@macway.com"}, {"service@paypal.fr", "Fnac Direct", "service-clientele@fnac.com"}, {"service@paypal.fr", "Fleurus Presse", "a.peys@fleuruspresse.com"}}
	
end buildMails

#===== # The main code stop here, we may put handlers below.

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) jeudi 24 septembre 2015 17:39:43

Replaced by an enhanced version.