如何使用附件在PHP中发送邮件 [英] How to send mail in PHP with attachments

查看:94
本文介绍了如何使用附件在PHP中发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP发送附带代码的附件邮件。我工作正常,但没有收到电子邮件ID的任何邮件



我尝试过:



I am using PHP for sending mail with attachmentusing below code. i work fine but didnot get any mail in EMail ID

What I have tried:

<?php
   if(isset($_FILES['image'])){
      $errors= array();
      $file_name = $_FILES['image']['name'];
      $file_size = $_FILES['image']['size'];
      $file_tmp = $_FILES['image']['tmp_name'];
      $file_type = $_FILES['image']['type'];
     // $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
	  $array = explode('.', $file_name);
       $file_ext = end( $array); 
      
      $expensions= array("jpeg","jpg","png","pdf");
      
      if(in_array($file_ext,$expensions)=== false){
         $errors[]="extension not allowed, please choose a PDF, JPEG or PNG file.";
      }
      
      if($file_size > 2097152) {
         $errors[]='File size must be excately 2 MB';
      }
      
      if(empty($errors)==true) {
        //move_uploaded_file($file_tmp,"uploads/".$file_name); //The folder where you would like your file to be saved

         echo "Success";
      }else{
         print_r($errors);
      }
   }

// PHPMailer script below

$email = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$phone = $_REQUEST['phone'] ;
$message = $_REQUEST['message'] ;

if (!class_exists("phpmailer")) {
require_once('PHPMailer_5.2.0/class.phpmailer.php');
}
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true; 


$mail->Username = "MY EMail  ID"; // SMTP username
$mail->Password = "******"; // SMTP password
//$mail->addAttachment("uploads/".$file_name);
$mail->AddAttachment($file_tmp, $file_name);
$mail->From = $email;
$mail->SMTPSecure = 'ssl'; 
$mail->Port = 25; //SMTP port
$mail->addAddress("xyz@gmail.com", "Name");
$mail->Subject = "You have an email from a website visitor!";
$mail->Body ="
Name: $name<br>
Email: $email<br>
Telephone: $phone<br><br><br>
Comments: $message";
$mail->AltBody = $message;

if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}

echo "<script>alert('Message has been sent')</script>";
?>

推荐答案

_FILES ['image'])){
_FILES['image'])){


errors = array();
errors= array();


file_name =
file_name =


这篇关于如何使用附件在PHP中发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆