0

I would like to send mails with attachements in PHP. I tried this example from a website:

//recipient
$to = 'my email adress';

//sender
$from = 'test@example.com';
$fromName = 'Moduleingabe';

//email subject
$subject = 'test email'; 

//attachment file path
$file = $dateiname;

//email body content
$htmlContent = '<h1>Test email</h1>
   <p>This email has sent from PHP script with attachment.</p>';

//header for sender info
$headers = "From: $fromName"." <".$from.">";

//boundary 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

//headers for attachment 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

//multipart boundary 
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n"; 

//preparing attachment
if(!empty($file) > 0){
    if(is_file($file)){
        $message .= "--{$mime_boundary}\n";
        $fp =    @fopen($file,"rb");
        $data =  @fread($fp,filesize($file));

        @fclose($fp);
        $data = chunk_split(base64_encode($data));
        $message .= "Content-Type: application/octet-stream; name=\"".basename($file)."\"\n" . 
        "Content-Description: ".basename($file)."\n" .
        "Content-Disposition: attachment;\n" . " 
  filename=\"".basename($file)."\"; size=".filesize($file).";\n" . 
        "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
    }
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $from;

//send email
$mail = @mail($to, $subject, $message, $headers, $returnpath); 

//email sending status
echo $mail?"<h1>Mail sent.</h1>":"<h1>Mail sending failed.</h1>";

This gives me the result Mail sending failed. I also tried just the mail() function without attachements and it gave me a false.

I tried to use a real mail adress I had for $from, but it gave me the same result, though I remember being able to use any example mail when I first tried out the function.

alex
  • 131
  • 12
  • 1
    You could [switch on error reporting](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) and remove the `@` in front of the `mail()` function. This will probably give you a more useful error than just `false` or "Mail sending failed". The `@` in front of a function suppresses error reporting. – KIKO Software Apr 29 '19 at 07:54
  • @KIKOSoftware The error it gave me now was ``Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\ModuleingabeL\excelsave.php on line 117`` – alex Apr 29 '19 at 08:09
  • This is a more basic problem. You need to enable a local mailserver and tell PHP what it is. I found [smtp4dev](https://github.com/rnwood/smtp4dev) but I cannot be sure if it is useful to you, it is only for testing purposes. It all depends on your setup. I changed the link to smtp4dev. I see it requires some effort to install. Setting up a mail server is always a bit complicated. – KIKO Software Apr 29 '19 at 08:10
  • https://www.glob.com.au/sendmail/ Use in php.ini: `sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"` – Alessandro Apr 29 '19 at 09:08
  • if you are working in a locahost, it's depends on your provider. Try your code uploaded on a server, it might work – vieroli Apr 29 '19 at 09:13
  • https://sourceforge.net/projects/msmtp/files/msmtp/1.6.2/ binary version for windows, `sendmail_path = 'C:\xampp\msmtp\msmtp.exe --file=C:\xampp\msmtp\msmtprc.ini -t'` – Alessandro Apr 29 '19 at 09:17
  • @vieroli it now says the mail has been sent, but so far nothing arrived... – alex Apr 29 '19 at 09:31
  • @vieroli I tried it again with another email as recipient and it worked, but for some reason my gmail adress won't receive them – alex Apr 29 '19 at 09:40
  • I have just tried with my gmail address, and it worked. Try to check your spam ? – vieroli Apr 29 '19 at 09:44

1 Answers1

0

Try your code on my server.

https://files.olivierlam.fr/test/mail.php?to=YOUREMAIL&from=EMAILSENDER

It worked for me. I have received the email. And it displayed Mail sent.

vieroli
  • 346
  • 3
  • 15