[英]Trying to send email with attachment
I can not seem to sort out what I am doing wrong here for sending an email with a text file attachment. 我似乎无法整理出我在发送带有文本文件附件的电子邮件时做错的事情。 I get no email, see no errors.
我没有收到电子邮件,没有看到错误。 New to all this and been looking at examples and tutorials for hours now and what I am missing is not standing out.
所有这些都是新手,并且已经看了几个小时的示例和教程,而我所缺少的并不突出。 Thanks for any help on this.
感谢您对此的任何帮助。
//core/email_cfg.php
function mail_attachment($git_messages, $file)
{
$filename = basename($file);
//define the receiver of the email
$to = 'whoever@domain.com';
//define the subject of the email
$subject = 'Change to Config File '.$filename;
//message
$username = isset($_SESSION["username"]) ? $_SESSION["username"] : 'Unknown User';
$message = 'Notification: The config file named '.$filename. ' was changed by USER='.$username.', GIT Results: '. $git_messages;
//from:
$from = "SomeUserEmail@domain.com";
//how do we fit this in?
//\r\nReply-To: no_reply@domain.com\r\n
// $file should include path and filename
$file_size = filesize($file);
$content = chunk_split(base64_encode(file_get_contents($file)));
$uid = md5(uniqid(time()));
$from = str_replace(array("\r", "\n"), '', $from); // to prevent email injection
$header = "From: ".$from."\r\n"
."MIME-Version: 1.0\r\n"
."Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"
."This is a multi-part message in MIME format.\r\n"
."--".$uid."\r\n"
."Content-type:text/plain; charset=iso-8859-1\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"
.$message."\r\n\r\n"
."--".$uid."\r\n"
."Content-Type: text/html; charset=iso-8859-1\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"
.$message."\r\n\r\n"
."--".$uid."\r\n"
."Content-Type: text/plain; name=\"".$filename."\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"
.$content."\r\n\r\n"
."--".$uid."--";
return mail($to, $subject, "", $header);
}
Personally, if you're trying to send an attachment it might be a good idea to look into using a library as doing it straight in PHP can be rather tricky. 就个人而言,如果您要发送附件,那么最好考虑使用库,因为直接在PHP中完成它可能很棘手。
For example I use PHPMailer . 例如,我使用PHPMailer 。
You can easily send attachments in the following way: 您可以通过以下方式轻松发送附件:
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.example.com"; // SMTP server
$mail->From = "from@example.com";
$mail->AddAddress("myfriend@example.net");
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
$mail->AddAttachment("fileNameToAttach");
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.