Mail Applescript Not Reading reference file

The applescript below is supposed to send out an email. When I run the script everything appears fine. The recipients address, subject line, and e-mail body all go to the right places. But when it goes to send the e-mail out, I get the following error:

Recipient’s e-mail address appears to not exist.

I checked for miss spellings, but there were none. However, if I delete the email address and simply re-write it, Mail will send it out no problem. My only thought is that something may be going on with the reference file. The script pulls the email address from a plain text document reference file that is create after the user inserts their address as you will see below. Any ideas as to where my error is coming from?

set theText to "@yahoo.com"
tell application "Finder"
	display dialog "What is your e-mail address?" default answer theText
end tell
set finalResult to text returned of result
set addressFile to "address.txt"
set logName to ((path to movies folder as text) & addressFile)
try
	set fileRef to open for access file logName with write permission
	write finalResult & return to fileRef starting at eof
	close access fileRef
on error
	try
		close file logName
	end try
end try
set theText to "File Name"
tell application "Finder"
	display dialog "What is the name of your subject?" default answer theText
end tell
set finalResult to text returned of result
set videoFile to "File Name.txt"
set logName to ((path to movies folder as text) & videoFile)
try
	set fileRef to open for access file logName with write permission
	write finalResult & return to fileRef starting at eof
	close access fileRef
on error
	try
		close file logName
	end try
end try
tell application "Finder"
	set theFolder to ((path to movies folder as text) & "myFolder") as alias
	set theList to name of (every file in theFolder)
end tell

repeat with i from 1 to (count theList)
	set theName to (item i of theList)
	set item i of theList to ("http://mywebsite.com/" & theName)
end repeat
set theText to "

"
repeat with i from 1 to (count theList)
	set theText to (theText & (item i of theList) & "

")
end repeat
set finalResult to theText
set bodyFile to "Links Body.txt"
set logName to ((path to movies folder as text) & bodyFile)
try
	set fileRef to open for access file logName with write permission
	write finalResult & return to fileRef starting at eof
	close access fileRef
on error
	try
		close file logName
	end try
end try
--direct to your plain text file with addresses, and name, tab delimited
set addressFile to "address.txt"
set sourceFile to open for access ((path to movies folder as text) & addressFile)
set theAddressDOC to read sourceFile
close access sourceFile

set pcount to count paragraphs in theAddressDOC
repeat with i from 1 to number of paragraphs in theAddressDOC
	set this_item to paragraph i of theAddressDOC
	
	set address_text to this_item
	
	set theAddress to address_text
	
	-- direct to a doc of what you want the email to say in plain text
	set bodyFile to "Links Body.txt"
	set sourceFile to open for access ((path to movies folder as text) & bodyFile)
	set theletter to read sourceFile
	close access sourceFile
	
	set subjectFile to "File Name.txt"
	set sourceFile to open for access ((path to movies folder as text) & subjectFile)
	set theSubject to read sourceFile
	close access sourceFile
	-- What is the subject of the Emails
	set theSubject to "Here is your link:" & theSubject
	
	set theBody to " Your Link is located below:" & theletter
	
	-- Choose the account to send the message from
	set theSender to "myaccount@gmail.com"
	
	tell application "Mail"
		
		set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}
		tell newMessage
			-- Default is false. Determines whether the compose window will
			-- show on the screen or whether it will happen in the background.
			set visible to true
			set sender to theSender
			make new to recipient at end of to recipients with properties {address:theAddress}
			tell content
				tell application "Mail"
					send newMessage
				end tell
				-- delay to allow the Mail program to send and not bog down, adjust as needed
				delay 10
			end tell
		end tell
	end tell
	
end repeat

Hi,

there a reference mess in the Mail part


tell application "Mail"
	set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}
	tell newMessage
		-- Default is false. Determines whether the compose window will
		-- show on the screen or whether it will happen in the background.
		set visible to true
		set sender to theSender
		make new to recipient at end of to recipients with properties {address:theAddress}
		send
	end tell
	-- delay to allow the Mail program to send and not bog down, adjust as needed
	delay 10
end tell

I got the same error. Do you think it might be a preference issue mail?

is the email address is the right format?

yeah. all i’m going is running the script. Then once I get the error, I go into mail and manually retype the exact same email address that has already been inserted from my reference file. Then I manually hit send and it sends off the message. I even tried a different email account thinking that I was somehow miss spelling the first one and just not seeing it, but the same error occurred.

I converted this script from a tab delimited script that had the email address and the subject in the same reference text doc. It worked fine that way. Now, for some reason, when i separate the two into their own text Doc’s, Mail is having a problem with the E-mail address. I could post the original tab delimited script if that would help.

maybe the tab character is left in the address

I got rid of the following:

set theAddressDOC to read sourceFile
close access sourceFile

set pcount to count paragraphs in theAddressDOC
repeat with i from 1 to number of paragraphs in theAddressDOC
   set this_item to paragraph i of theAddressDOC
   
   set address_text to this_item
   
   set theAddress to address_text

and re-wrote it as the following:

set this_item to read sourceFile
close access sourceFile

   set theAddress to this_item

I thought that would get rid of any tab related issues, but I am still getting the same thing. This is odd.

reading a paragraph or reading the whole file both gets all characters inclusive tab characters.

By the way: To read small text files you don’t need to open and close for access the file.
read [alias] or read file [HFS path] is sufficient, no open line, no close line

I checked the text reference and there seems to be a keystroke return or a second line added in it. Do you see where it would add that return in the script above?

if you always write starting at eof, the new text will be appended to the former text

I removed “starting at eof” and now it isn’t saving the text that is entered into the prompt dialog box.

keep the starting at eof and insert this line before the write line

set eof fileRef to 0

The script is written as the following:

set theText to "@yahoo.com"
tell application "Finder"
	display dialog "What is your e-mail address?" default answer theText
end tell
set finalResult to text returned of result
set addressFile to "address.txt"
set logName to ((path to movies folder as text) & addressFile)
try
	set fileRef to open for access file logName with write permission
        set eof fileRef to 0
	write finalResult & return to fileRef starting at eof
	close access fileRef
on error
	try
		close file logName
	end try
end try

When I checked the file created, there is nothing in it.

the script must work

wait something else must be wrong. I tried running this solo, and now it’s not working either:

set theText to "@yahoo.com"
tell application "Finder"
	display dialog "What is your e-mail address?" default answer theText
end tell
set finalResult to text returned of result
set addressFile to "address.txt"
set logName to ((path to movies folder as text) & addressFile)
try
	set fileRef to open for access file logName with write permission
	write finalResult & return to fileRef starting at eof
	close access fileRef
on error
	try
		close file logName
	end try
end try

when I open the file, there is nothing in it.

I restarted my script editor, and it runs fine now. I will keep at this. Im going to check with apple to see if there is any reason for this. I will elt you know what I come up with. Thanks for your help stefan.