Javascript-форум (https://javascript.ru/forum/)
-   Серверные языки и технологии (https://javascript.ru/forum/server/)
-   -   отправка письма на почту (php) (https://javascript.ru/forum/server/45193-otpravka-pisma-na-pochtu-php.html)

cyber 18.02.2014 20:47

вот код

<?php 

$boundary =  md5(uniqid(time()));
$mail = "mail";

if(empty($_POST["subject"]) 
	|| empty($_POST["user_mail"])) {
	http_response_code(400);
}

$subject = htmlspecialchars($_POST["subject"]);
$backMail = htmlspecialchars($_POST["user_mail"]);

$html = "<html><head></head> <body>" ;
$html .= "Email пользователь:<a href='mailto:{$backMail}'> {$backMail}</a>";

if($_POST["message-text"]) {
	$messageText = htmlspecialchars($_POST["message-text"]);
	$html .= "<p>Сообщение пользоавателя: </p>" .$messageText;
}

$html .= "</body></html>";

$header = "FROM: 'Online book feeback' <{$mail}> \n";
$header .= "TO: {$mail} \n";
$header .= "Subject: {$subject} \n";
$header .= "Mime-Version: 1.0 \n";
$header .= "Content-Type: multipart/mixed; boundary='{$boundary}'";
$body = "\n--{$boundary}";
$body .= "Content-Type: text/plain; charset='windows-1251' \n";
$body .= "Content-Transfer-Encoding: quoted-printable \n";
$body .= $html;


if(count($_FILES) > 0) {
	foreach ($_FILES as $name => $file) {

		if(!$file["size"])  continue;

		$file = fopen($file["tmp_name"], "rb");
		$body .= "\n--{$boundary}";
		$body .= "Content-Type: application/octet-stream;";
		$body .= "name=".basename($name)."\n";
		$body .= "Content-Transfer-Encoding:base64 \n";
	    $body .= "Content-Disposition:attachmentn \n";
	    $body .= base64_encode(fread($file, filesize($file["tmp_name"]))); // вот тут ошибка, пока не понял почему
	    //Warning: fread(): Length parameter must be greater than 0 in E:\OpenServer\domains\localhost\test\sendMail.php on line 48

	}

}

$body .= "{$boundary}--";


if(mail($mail, $subject, $body, $header)){
	echo "Письмо успешно отправлено";
} 
 else {
 	http_response_code(400);
 }

?>

danik.js 18.02.2014 21:19

Цитата:

Сообщение от cyber
$file = fopen($file["tmp_name"], "rb");

Ну ты лох )) ты же затер переменную
Цитата:

Сообщение от cyber
Content-Disposition:attachmentn

n не лишняя на конце?

cyber 19.02.2014 00:02

Цитата:

Сообщение от danik.js
Ну ты лох )) ты же затер переменную

сам редиска, я 10 часов подряд кодил:)

cyber 19.02.2014 17:07

всеравно не работает, почему на почту приходит пустое письмо??
что я не так сделал?
<?php 

$boundary =  md5(uniqid(time()));
$mail = "mail";

if(empty($_POST["subject"]) 
	|| empty($_POST["user_mail"])) {
	http_response_code(400);
}

$subject = htmlspecialchars($_POST["subject"]);
$backMail = htmlspecialchars($_POST["user_mail"]);

$html = "<html><head></head> <body>" ;
$html .= "Email пользователь:<a href='mailto:{$backMail}'> {$backMail}</a>";

if($_POST["message-text"]) {
	$messageText = htmlspecialchars($_POST["message-text"]);
	$html .= "<p>Сообщение пользоавателя: </p>" .$messageText;
}

$html .= "</body></html>";

$header = "FROM: 'Online book feeback' <{$mail}> \n";
$header .= "TO: {$mail} \n";
$header .= "Subject: {$subject} \n";
$header .= "Mime-Version: 1.0 \n";
$header .= "Content-Type: multipart/mixed; boundary='{$boundary}' \n";
$body = "--{$boundary} \n";
$body .= "Content-Type: text/html; charset='utf-8' \n";
$body .= "Content-Transfer-Encoding: quoted-printable \n";
$body .= $html;


if(count($_FILES) > 0) {
	foreach ($_FILES as $name => $file) {

		if(!$file["size"])  continue;

		$fopn = fopen($file["tmp_name"], "rb");
		$body .= "--{$boundary}\n";
		$body .= "Content-Type: application/octet-stream; \n";
		$body .= "name=".basename($name)."\n";
		$body .= "Content-Transfer-Encoding:base64 \n";
	    $body .= "Content-Disposition:attachment \n";
	    $body .= base64_encode(fread($fopn, filesize($file["tmp_name"]))); 
	}

}

$body .= "{$boundary}--";



if(mail($mail, $subject, $body, $header)){
	echo "Письмо успешно отправлено";
} 
 else {
 	http_response_code(400);
 }

?>

danik.js 19.02.2014 17:10

хз, подключи либу какую-нибудь для mail'инга, чтоб не возиться с этим "низкоуровневым" барахлом.

cyber 19.02.2014 18:38

Цитата:

Сообщение от danik.js
хз, подключи либу какую-нибудь для mail'инга, чтоб не возиться с этим "низкоуровневым" барахлом.

какую посоветуешь?

danik.js 19.02.2014 20:22

Цитата:

Сообщение от cyber
какую посоветуешь?

Никакую, я с php раз в полгода вожусь )
Помню что тоже нужна была. Так я загуглил, взял первую попавшуюся и был доволен как слон :)
К слову файлы тоже аттачил.

danik.js 19.02.2014 20:23

Короче, глянул - PHPMailer юзал я.

Цитата:

## Class Features

- Probably the world's most popular code for sending email from PHP!
- Used by many open-source projects: Drupal, SugarCRM, Yii, Joomla! and many more
- Integrated SMTP support - send without a local mail server
- send emails with multiple TOs, CCs, BCCs and REPLY-TOs
- Multipart/alternative emails for mail clients that do not read HTML email
- Support for 8bit, base64, binary, and quoted-printable encoding
- SMTP authentication with LOGIN, PLAIN, NTLM and CRAM-MD5 mechanisms
- Native language support
- Compatible with PHP 5.0 and later
- Much more!

cyber 19.02.2014 20:51

Цитата:

Сообщение от danik.js
Короче, глянул - PHPMailer юзал я.

ок, спс попробую.

4ITEROK 22.02.2014 11:47

Не атачь мозг. Юзай и качай исходники . И надо если дорабатывй


Часовой пояс GMT +3, время: 19:56.