Authenticated mail() function with attachment

Asked

Viewed 144 times

0

I cannot use Casse PHPMAILER as the hosting server does not accept, (Information from the server support itself)

I have this example, but the attached file, always comes with error. and also can not write message in the body of the email.

$boundary = "XYZ-".md5(date("dmYis"))."-ZYX";
$path = $_FILES['fTxtArquivo']['tmp_name']; 
$fileType = $_FILES['fTxtArquivo']['type']; 
$fileName = $_FILES['fTxtArquivo']['name']; 

// Pegando o conteúdo do arquivo
$fp = fopen( $path, "rb" ); // abre o arquivo enviado
$anexo = fread( $fp, filesize( $path ) ); // calcula o tamanho
$anexo = chunk_split(base64_encode( $anexo )); // codifica o anexo em base 64
fclose( $fp ); // fecha o arquivo

$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=" . $boundary . PHP_EOL;
$headers .= "$boundary" . PHP_EOL;

$mensagem  = "--$boundary" . PHP_EOL;
$mensagem .= "Content-Type: text/html; charset='utf-8'" . PHP_EOL;
$mensagem .= "Mensagem: teste"; // Adicione aqui sua mensagem
$mensagem .= "--$boundary" . PHP_EOL;

$mensagem .= "Content-Type: ". $fileType ."; name=\"". $fileName . "\"" . PHP_EOL;
$mensagem .= "Content-Transfer-Encoding: base64" . PHP_EOL;
$mensagem .= "Content-Disposition: attachment; filename=\"". $fileName . "\"" . PHP_EOL;
$mensagem .= "$anexo" . PHP_EOL;
$mensagem .= "--$boundary" . PHP_EOL;

$envio = mail("[email protected]", $assunto, $mensagem, $headers);

Someone has a simple example, to send email with attachment!

  • 1

    What did the hosting claim? about using phpmailer? another option would be the swiftmailer. Generating this attachment header at hand is quite laborious and subject to errors.

  • I always use PHPMAILER, but for some reason it didn’t work. When contacting the hosting support, I was told that PHPMAILER does not work, it had to be the email function()

  • 2

    Which hosting server? Check the server documentation, that they should have some technical information about the exit rules. As for example in Ocaweb, some mandatory headers enter. 
http://wiki.locaweb.com.br/pt-br/PHP_Mail_-_Como_enviar_e-mails_utilizando_a_fun%C3%A7%C3%A3o_Mail_nativa_do_PHP

  • I already talked to them, the return I had, was that it cannot be PHPMAILER, BECAUSE IT IS BLOCKED, it has to be via mail function()

  • 2

    It seems that it is Locaweb, I searched by the domain fmaconsulting.com.br: https://registro.br/cgi-bin/whois/#lresp Remembering that to send email by their mail function, the email you send has to be of the same domain, and it has some rules for sending. Read the Ocaweb wiki.

  • This is my test item, which is really from Ocaweb, how it works when text on the server, but the client’s email service does not work when I put his email, when I put the information in the existing PHPMAILER structure. I just need an example with the Emal() function that sends an attachment... Only this, because I’ve already looked on the net, and most of them are in error

Show 1 more comment

1 answer

0

I did not get the answer I wanted, but I got the result I expected. I contacted again with redeHost, gave me another and smtpexc01.redehost.com.br solved my problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.