Solved

Conflict between PhpMailer and phpmail

Posted on 2008-10-17
5
473 Views
Last Modified: 2009-04-02
Hi Am using the following code to send two different emails with the same form.  am using phpmailer to send the mail with attachment and phpmail to send without the attachment to two different recepients.

This code works fine when the size of the attachment is smaill like 3-10kb but if the size is a bigger one something like 245kb only the first mail goes through and second mail doesn't.

Please let me know what is wrong here.
$uploaddir = 'c:\\ABC\\';

$uploadfile = $uploaddir . basename($_FILES['file']['name']);

move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile); 

 

$mail = new PHPMailer();

 

$mail->IsSMTP();                      

$mail->Host = "X.X.X.X"; 

 

 

$mail->From = $_POST['email'];

$mail->FromName = "Submissioin";

$mail->AddAddress("abc@abc.com");                 

                              

$mail->AddAttachment($uploadfile); 

$mail->IsHTML(true);        

$mail->Subject = "Submission Form";

 

$mail_body = "Test mail";

$mail->Body    = $mail_body;

 

 

if(!$mail->Send())

{

   echo "Message could not be sent. <p>";

   echo "Mailer Error: " . $mail->ErrorInfo;

   exit;

}

else

{

$mail->ClearAddresses();

$mail->ClearAttachments();

 

//sending email using phpmailer===============================================================

$Name =  $_POST['fName'] . ' ' . $_POST['Lname']; 

$email = $_POST['email']; 

$recipient = "xyz@xyz.com"; 

$mail_body = "Attachment recieved";                       

$subject = "Submission Form"; //subject 

$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields  

$mail_body = strip_tags( preg_replace('#<br\s*/?>#i',"\n",$mail_body) );

mail($recipient, $subject, $mail_body, $header); 

 

//======================================================================

 

 

header("Location: http://somewhere.ca.com/partnerescalation/reqkd.php");

exit();}

Open in new window

0
Comment
Question by:Shiva_Kumar
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
wildzero earned 500 total points
ID: 22749468
Hi there,

This block of code here

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

Means that it's only ever going to send 1 email. It will try and do PHPMailer, and only if that fails it will use the PHP Mail. If you want it to always send two emails then you can use this. The code below will not stop on errors.

You should also check and make your capitilization consistant
$Name =  $_POST['fName'] . ' ' . $_POST['Lname'];
is it fName or Fname? Lname or lName


$uploaddir = 'c:\\ABC\\';

$uploadfile = $uploaddir . basename($_FILES['file']['name']);

move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile); 

 

$mail = new PHPMailer();

 

$mail->IsSMTP();                      

$mail->Host = "X.X.X.X"; 

 

 

$mail->From = $_POST['email'];

$mail->FromName = "Submissioin";

$mail->AddAddress("abc@abc.com");                 

                              

$mail->AddAttachment($uploadfile); 

$mail->IsHTML(true);        

$mail->Subject = "Submission Form";

 

$mail_body = "Test mail";

$mail->Body    = $mail_body;

 

$mail->Send();

$mail->ClearAddresses();

$mail->ClearAttachments();

 

//sending email using phpmailer=================================

$Name =  $_POST['fName'] . ' ' . $_POST['Lname']; 

$email = $_POST['email']; 

$recipient = "xyz@xyz.com"; 

$mail_body = "Attachment recieved";                       

$subject = "Submission Form"; //subject 

$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields  

$mail_body = strip_tags( preg_replace('#<br\s*/?>#i',"\n",$mail_body) );

mail($recipient, $subject, $mail_body, $header); 

 

//======================================================================

 

 

header("Location: http://somewhere.ca.com/partnerescalation/reqkd.php");

exit();

Open in new window

0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 22749540
> Means that it's only ever going to send 1 email. It will try and do PHPMailer, and only if that fails it will use the PHP Mail.

That's not true. There's a ! in that if that means it will only exit if it fails to send.

It's a complete mystery to me why you'd use PHPMailer to send the first one but not the second. Using mail() directly is generally just asking for trouble, as you're seeing. I'm not going to debug that code because the right fix is to use PHPMailer to send that as well.
0
 

Author Comment

by:Shiva_Kumar
ID: 22778154
Thank you, it worked
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 22778194
What worked? In the selected answer, the logic is completely wrong and it gets around the problem of there being an error by removing the error checking!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Form not operating correctly. 1 22
Load google maps api into wordpress 5 37
xampp tool 12 24
Scope of $_SESSION 17 27
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now