Magento电子邮件附件没有收到姓名

I have created a contact us page with email attachment. I followed the tutorial from here: http://www.blog.magepsycho.com/adding-upload-field-in-contact-form-and-send-as-attachment/.

The contact form and attachment works perfectly however there is an issue.

If I intentionally send the form without attaching any file, I still receive a no name attachment file. Suppose to be no attachment present.

screenshot below:

What was wrong with this email attachment?

If you try to look on the line 67-73 where it sends the attachment, you will notice the if statement that checks if $attachmentFilePath exist.

$attachmentFilePath = Mage::getBaseDir('media'). DS . 'contacts' . DS . $fileName;
if(file_exists($attachmentFilePath)){

This condition returns true even if the user haven't attached anything. If you print $attachmentFilePath, it will give you the full path. When user send email without attachment, $attachmentFilePath it will give you a path until directory contacts.

Try to update your if condition to this

if($fileName){

It will check if the field has a value and skip if there's none.