PHPMailer not sending mail - php

I'm trying to send an order confirmation and also notify the seller about a user purchase. However, PHPMailer only sends the first email. Here's quick and dirty:
$bodytext = 'Mail.';
$email = new PHPMailer();
$email->From = 'mail#mail.com';
$email->FromName = 'Sender';
$email->Subject = 'Subject';
$email->Body = $bodytext;
$email->AddAddress($_REQUEST['sahkoposti']);
$email->AddAttachment($path, 'kuitti'.$ordernumber.'.pdf');
return $email->Send();
?>
<?php
//send message to seller
$bodytext = 'Mail.';
$email = new PHPMailer();
$email->From = 'mail#mail.com';
$email->FromName = 'Sender';
$email->Subject = 'Tilaus vastaanotettu';
$email->Body = $bodytext;
$email->AddAddress("mail#mail.com");
$email->AddAttachment($path, 'kuitti'.$ordernumber.'.pdf');
return $email->Send();
?>
Is it even possible to send multiple emails from one script?

It is possible, however you're using return in the first statement, which will stop execution of the function. Remove the first return (just use $email->Send();) and it should work.

The second email does not get executed because you are returning right after sending the first email, you should change:
return $email->Send();
for this:
$email->Send();

Related

PHP mailer redirect issue

I came across an issue using PHPmailer that I just don't understand. I have an assist function redirect for the header function that works 100% in the position shown. It fails to work in the positions shown in the comments where I would like it to work. Everything else works perfectly regardless of where the redirect function is placed. Any ideas? This is also my first post so apologies in advance if there is already a solution which I couldn't find.
<?php
if(isset($_POST['replyall'])) {
redirect("index.php?leadreply"); // Why does this have to be here to work???? //
if(isset($_POST['chk1'])) {
$email = new PHPMailer();
$email->From = $_POST['author'];
$email->FromName = 'JGM Decorating';
$email->Subject = 'Reply to your contact request';
$email->Body = $_POST['comments'];
$email->AddAddress( $_POST['destination'] );
$file_to_attach = '../crm/gtcjgm.pdf';
$email->AddAttachment( $file_to_attach , 'Terms and Conditions.pdf' );
return $email->Send();
// I would like to have the redirect here but it doesn't work??//
} else {
$email = new PHPMailer();
$email->From = $_POST['author'];
$email->FromName = 'JGM Decorating';
$email->Subject = 'Reply to your contact request';
$email->Body = $_POST['comments'];
$email->AddAddress( $_POST['destination'] );
$file_to_attach = '';
$email->AddAttachment( $file_to_attach , 'Terms and Conditions.pdf' );
return $email->Send();
// I would like to have a different redirect here but it doesn't work??//
}
}
?>
This code:
return $email->Send();
Explanation: This will be returning the function and it will stop the process at this stage itself and after that whatever line you place it will not work. (E.g) Echo statement also will not work.
If you want to do both you have to do the mail sending process using Ajax and after that alone you have to redirect as per you need .
Since the return will stop the execution at that instant itself. So in order to do so you can use it with the hep of AJAX and then redirect it.

PHPMailer script working Hostinger not on fatcow

I have a PHPMailer script. It's working fine on my Hostinger account, but when I move it over to my company's FatCow server it stops working.
Any help would be much appreciated!
The code is:
$email = new PHPMailer();
$email->IsSendmail();
$email->From = "stuart#ubrew.cc";
$email->FromName = "Stuart";
$email->Subject = "Subject...";
$email->Body = "text"; // BODY TEXT HERE
$email->AddAddress("$customeremail");
$email->AddAttachment($certificateName, 'GiftCertificate.pdf');
if (!$email->Send()) {
$message = $email->ErrorInfo;
mail('sewelly#gmail.com', 'MAILER ERROR', $message);
}

Using PHPMailer send attachment file in PHP

I currently using PHPMailer to sending mail with attachment file in localhost and also in my server.
The problem I have is the attachment file does not receive in email when execute in server. But in other side, the attachment file receive perfectly when I execute on localhost.
Here is my simple code:
$email = new PHPMailer();
$email->From = 'william#msmedia.com.sg';
$email->FromName = 'John';
$email->Subject = 'Message Subject';
$email->Body = 'test';
$email->AddAddress( 'william#msmedia.com.sg' );
$file_to_attach = $_FILES["cv"]["tmp_name"];
$email->AddAttachment( $file_to_attach , $_FILES["cv"]["name"]);
return $email->Send();
Can anyone able to correct my mistake..?

How do i remove attachment form phpmailer when sending a different mail for second time in php

In a php file i need to send 2 different emails to 2 different id's. It did not work when i used two variables like this shown below.
require 'PHPmailer/class.phpmailer.php';
/* First Email*/
$email = new PHPMailer();
$email->From = 'admin#mywebsite.com';
$email->FromName = 'My Webisite';
$email->Subject = 'Subject of first email';
$email->Body = 'Body of the message to first person';
$email->AddAddress( 'to first person' );
$file_to_attach = 'path of the file';
$email->AddAttachment( $file_to_attach, '' );
$email->Send();
/* Second Email*/
require 'PHPmailer/class.phpmailer.php';
$confirm = new PHPMailer();
$confirm-> From = 'noreply#mywebsite.com';
$confirm-> FromName = 'Admin # MyWebsite';
$confirm-> Subject = 'Subject of second email';
$confirm-> Body = 'Body of second email';
$confirm-> AddAddress('Email ID of second person');
$confirm->Send();
But if i use the same variable twice i will work as shown below
require 'PHPmailer/class.phpmailer.php';
/* First Email*/
$email = new PHPMailer();
/* Same as above*/
$file_to_attach = 'path of the file';
$email->AddAttachment( $file_to_attach, '' );
$email->Send();
/* Second Email*/
$email-> From = 'noreply#mywebsite.com';
$email-> FromName = 'Admin # MyWebsite';
$email-> Subject = 'Subject of second email';
$email-> Body = 'Body of second email';
$email-> AddAddress('Email ID of second person');
$email->Send();
But the problem is it is sending the attachment to both the email ids. Please help me how do i not send the attachment to second id.
unset($mail->attachment) won't work as attachment is a protected variable. Instead use:
$email->clearAttachments();
Before execute /* Second Email */
You can try:
unset($mail->attachment)

PHPMail - can I send two unique emails

I'm using PHPMailer to successfully send an email upon a webform submission (so just using basic post data, no mysql databases or any lookups).
What I need to do is send two seperate emails, one version for the customer and the other for a customer service agent - so that when a user completes a webform, they will receive a 'marketing' version of the email, whilst the customer service agent will receive an email just containing the details submitted by the user.
See below what im using at the moment, but not sure how to best implement to send the secoind email? It presently fails and the script exits after sending only one email.
$body = ob_get_contents();
$to = 'removed';
$email = 'removed';
$fromaddress = "removed";
$fromname = "removed";
require("phpmailer.php");
$mail = new PHPMailer();
$mail->From = $fromaddress;
$mail->FromName = $fromname ;
$mail->AddAddress("email#example.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Subject";
$mail->Body = $body;
$mail->AltBody = "This is the text-only body";
if(!$mail->Send()) {
$recipient = 'email#example.com';
$subject = 'Contact form failed';
$content = $body;
mail($recipient, $subject, $content,
"From: mail#yourdomain.com\r\nReply-To: $email\r\nX-Mailer: DT_formmail");
exit;
}
//Send the customer version
$mail=new PHPMailer();
$mail->SetFrom('email', 'FULLNAME');
$mail->AddAddress($mail_vars[2], 'sss');
$mail->Subject = "Customers email";
$mail->MsgHTML("email body here");
//$mail->Send();
In the customer version you are not setting the email's properties the same way you are in the first. For example you use $mail->From = $fromaddress; in the first and $mail->SetFrom('email', 'FULLNAME'); in the second.
Because you instantiated a new $mail=new PHPMailer(); you need to set the properties again.
Instead of just bailing out with a useless "something didn't work" message, why not have PHPMailer TELL you what the problem is?
if (!$mail->send()) {
$error = $mail->ErrorInfo;
echo "Mail failed with error: $error";
}

Categories