Click here to Skip to main content
15,746,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I'm trying to build a test PHP send email page. I'm really new to PHP.
I attach an PDF to the email, but it doesn't send the email

Please advise

What I have tried:

Here's html page:
	<form action="php_sendmail_upload2.php" method="post" name="form1" enctype="multipart/form-data">
	<table width="343" border="1">
		<td><input name="txtTo" type="text" id="txtTo"></td> 
		<td><input name="txtSubject" type="text" id="txtSubject"></td>
		<td><textarea name="txtDescription" cols="30" rows="4" id="txtDescription"></textarea></td>
		<td>Form Name</td>
		<td><input name="txtFormName" type="text"></td>
		<td>Form Email</td>
		<td><input name="txtFormEmail" type="text"></td>
	  <td><input name="fileAttach" type="file"></td>
		<td> </td>
		<td><input type="submit" name="Submit" value="Send"></td>

PHP code:
	$strTo = $_POST["txtTo"];
	$strSubject = $_POST["txtSubject"];
	$strMessage = nl2br($_POST["txtDescription"]);

	//*** Uniqid Session ***//
	$strSid = md5(uniqid(time()));

	$strHeader = "";
	$strHeader .= "From: ".$_POST["txtFormName"]."<".$_POST["txtFormEmail"].">\nReply-To: ".$_POST["txtFormEmail"]."";

	$strHeader .= "MIME-Version: 1.0\n";
	$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
	$strHeader .= "This is a multi-part message in MIME format.\n";

	$strHeader .= "--".$strSid."\n";
	$strHeader .= "Content-type: text/html; charset=utf-8\n";
	$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
	$strHeader .= $strMessage."\n\n";
	//*** Attachment ***//
	if($_FILES["fileAttach"]["name"] != "")
		$strFilesName = $_FILES["fileAttach"]["name"];
		$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"]))); 
		$strHeader .= "--".$strSid."\n";
		$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n"; 
		$strHeader .= "Content-Transfer-Encoding: base64\n";
		$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
		$strHeader .= $strContent."\n\n";

	$flgSend = @mail($strTo,$strSubject,null,$strHeader);  // @ = No Show Error //

		echo "Mail send completed.";
		echo "Cannot send mail.";
Updated 15-Sep-17 3:48am
Richard MacCutchan 13-Sep-17 2:34am    
Remove the @ so you can see any error message.
Samira Radwan 13-Sep-17 19:43pm    
That was helpful, thank you.
I managed to send the email, but i receive it like all hashed or encrypted!! do you have any idea why? thanks
Richard MacCutchan 14-Sep-17 1:07am    
Sorry, no. You will need to do some debugging of your code to see exactly what is happening. As a starting point never disable error messages, they are there to help you.

1 solution

I managed to send the email, but i receive it like all hashed or encrypted!! do you have any idea why?
Your mail is not properly formed. You can check it by inspecting the received plain text (how to show that depends on your mail client; with Thunderbird and some other clients the shortcut Ctrl+U can be used).

The first error is that you put all content into headers and have no body:
$flgSend = @mail($strTo,$strSubject,null,$strHeader);

Then there is no epilogue (end boundary).

Finally you are using "\n" line terminations while email requires CRLF terminated lines. Note that this applies also to your $strMessage text. See PHP: mail - Manual[^]:
Message to be sent.
Each line should be separated with a CRLF (\r\n). Lines should not be larger than 70 characters.

Do it as intended and create strings for the header and the body, and use CRLF terminated lines:
// Additional headers.
// Note that there is no need to use two new line sequences here. 
// It will be inserted by the mail() function in front of the body.
$strHeader = "MIME-Version: 1.0\r\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\r\n";

// For non-MIME compliant readers 
$strBody = "This is a multi-part message in MIME format.\r\n";

// The text part
$strBody .= "--".$strSid."\r\n";
$strBody .= "Content-type: text/html; charset=utf-8\r\n";
// It makes no sense to use 7bit with UTF-8!
// If you want to avoid 8bit, you have to encode the string
//  and set the decoding accordingly (e.g. quoted-printable).
$strBody .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
// You must ensure that $strMessage is valid UTF-8 encoded HTML
//  and has CRLF terminated lines.
$strBody .= $strMessage."\r\n\r\n";

// Append additional content to $strBody here using also CRLF terminated lines
// ...

// The epilogue (trailing "--"). Anything beyond this is ignored.
$strBody .= "--".$strSid."--\r\n";

$flgSend = mail($strTo,$strSubject,$strBody,$strHeader);
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900