E-Mail versenden mit anhang per SMTP wie?
E-Mail versenden mit anhang per SMTP wie?
Hallo,
Ich habe ein script mit dem ich E-Mails versenden kann. nun möchte ich das die E-Mail einen Anhang hat aus einem bestimmt ordner auf meinem Server.
Hier mal das Script was ich schon habe:
PHP-Code:
function authgMail($from, $namefrom, $to, $nameto, $subject, $message) {
$smtpServer = "mail.gmx.net";
$port = "25";
$timeout = "45";
$username = "sales@mydomain.com";
$password = "XXXXXXX";
$localhost = "127.0.0.1";
$newLine = "\r\n";
$secure = 0;
//connect to the host and port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 4096);
if(empty($smtpConnect)) {
$output = "Failed to connect: $smtpResponse";
echo $output;
return $output;
}
else {
$logArray['connection'] = "<p>Connected to: $smtpResponse";
echo "<p />connection accepted<br>".$smtpResponse."<p />Continuing<p />";
}
//you have to say HELO again after TLS is started
fputs($smtpConnect, "HELO $localhost". $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['heloresponse2'] = "$smtpResponse";
//request for auth login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authrequest'] = "$smtpResponse";
//send the username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authusername'] = "$smtpResponse";
//send the password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authpassword'] = "$smtpResponse";
//email from
fputs($smtpConnect, "MAIL FROM: <$from>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailfromresponse'] = "$smtpResponse";
//email to
fputs($smtpConnect, "RCPT TO: <$to>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailtoresponse'] = "$smtpResponse";
//the email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data1response'] = "$smtpResponse";
//construct headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto <$to>" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;
//observe the . after the newline, it signals the end of message
fputs($smtpConnect, "To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data2response'] = "$smtpResponse";
// say goodbye
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['quitresponse'] = "$smtpResponse";
$logArray['quitcode'] = substr($smtpResponse,0,3);
fclose($smtpConnect);
//a return value of 221 in $retVal["quitcode"] is a success
return($logArray);
}
$err=0;
$err_msg="";
echo $err_msg;
if($err<=0) {
$from="sales@mydomain.com";
$namefrom="Admin";
$to = "internal_user@mydomain.com";
$nameto = "User";
$subject = "Das ist der Betreff";
$message = "Hi ich bin der Text";
// this is it, lets send that email!
authgMail($from, $namefrom, $to, $nameto, $subject, $message);
}
else {
echo "<p /> This form was not filled out correctly, please correct any mistakes.";
}
Ich möchte nun das ich mit diesem Script auch noch einen Anhang versenden kann.
Weis jemand wie ich das mache.
Mein Smtp hoster ist mail.gmx.net
Mit freundlichen grüßen