Click here to Skip to main content
15,946,988 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:

      $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";

// PHPMailer script below

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

if (!class_exists("phpmailer")) {
$mail = new PHPMailer(true);
$mail->Host = "";
$mail->SMTPAuth = true; 

$mail->Username = "MY EMail  ID"; // SMTP username
$mail->Password = "******"; // SMTP password
$mail->AddAttachment($file_tmp, $file_name);
$mail->From = $email;
$mail->SMTPSecure = 'ssl'; 
$mail->Port = 25; //SMTP port
$mail->addAddress("", "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;

echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;

echo "<script>alert('Message has been sent')</script>";
Updated 5-Nov-17 21:11pm

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
try {
    $mail = new PHPMailer();
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;
    $mail->Host = '';
    $mail->SMTPAuth = true;
    $mail->Username = ''; //Your email address
    $mail->Password = '*************'; //Password of your email address
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = 587;

    $mail->addAddress(''); //Recipient email address

    $mail->Subject = 'Subject';
    $mail->Body = 'This is the HTML message body';
    if ($mail->send()) {
        echo 'Message has been sent.';
    } else {
        echo 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
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