Click here to Skip to main content
15,436,489 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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>";
?>
Posted
Updated 5-Nov-17 21:11pm

1 solution

See the PHP mailer documentation (GitHub - PHPMailer/PHPMailer: The classic email sending library for PHP[^] and Tutorial · PHPMailer/PHPMailer Wiki · GitHub[^] ).

I see at least two problems:

  1. You are sending an HTML mail by setting $mail->Body but did not set $mail->isHTML(true)
  2. You are trying to send to a GMail address with wrong settings
GMail requires different settings (e.g. port 587, see PHPMailer/gmail.phps at master · PHPMailer/PHPMailer · GitHub[^]). Note also that you must allow less secure apps to send mail (see Allow less secure apps to access accounts - G Suite Administrator Help[^]).

I would expect that you get some kind of error message because with your settings the mail will be not accepted by the GMail SMTP server.

I suggest to send a simple mail first. If that works you can add additional content like attachments.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900