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.
Related
I'm currently experiencing a very strange issue trying to set up a simple mailer from a form. I've literally used the code on many sites seems to be working fine. But on this particular one I seem to be getting the Error "You must provide at least one recipient email address."
This is the code that i've been using
<?php
require_once('class.phpmailer.php');
$name = $_POST['name'];
$user_email= $_POST['email'];
$query = $_POST['message'];
$message = file_get_contents('email.html');
$message = str_replace('{{name}}', $name, $message);
$message = str_replace('{{email}}', $user_email, $message);
$message = str_replace('{{message}}', $query, $message);
$email = new PHPMailer();
$email->CharSet = 'UTF-8';
$email->IsHTML(true);
$email->From = $user_email;
$email->FromName = $name;
$email->Subject = 'Talking Together Speech Therapy Enquiry From- '. $name;
$email->Body = $message;
$email->AddAddress( 'barry.tickle12#gmail.com​' );
if($email->send()){
// Trigger when email sends
}else{
//Trigger when email doesn't send
echo $email->ErrorInfo;
}
?>
Can a specific server setup be causing this? Other sites on the same server seem to be working fine with it apart from this particular one.
* Edit *
Var dumped all POST Requests to the file and nothing is being returned empty.
Maybe add a check to see that you have all the required data ?
$email= $_POST['email'] ?? '';
if($email){
/** Your code goes inside this if */
} else {
/** Show error */
}
Try using new version of AddAddress():
$email->addAddress('recipient#domain.com', 'Name');
in my case, I have to add the email button on the file list but before it I must be signed first so that the file can be viewed, this email button when clicked the file will automatically be in the form of attachment when sending mail. In that cases is it possible without having to download the file first and then browse the file as attachment ?
Basic version (using php):
$email = new PHPMailer();
$email->From = 'you#example.com';
$email->FromName = 'Your Name';
$email->Subject = 'Message Subject';
$email->Body = $bodytext;
$email->AddAddress( 'destinationaddress#example.com' );
$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';
$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );
return $email->Send();
Reference : Send attachments with PHP Mail()?
Using laravel 5.0:
Mail::send('emails.welcome', $data, function($message)
{
$message->from('us#example.com', 'Laravel');
$message->to('foo#example.com')->cc('bar#example.com');
$message->attach($pathToFile);
});
Refer : Laravel mail
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..?
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)
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();