I’ve seen many examples of PHPMailer (adding a whole new email to the same object) but I just can’t understand how to apply it to my specific context… I am fairly new to PHP itself, so this is really frustrating me.
Please be so kind as to show me where exactly in my code I can send 2 emails at once… (One email goes to my own website and another goes to the person who submitted a form on my website).
Currently this code is working perfectly:
<?php
/*...*/
if (array_key_exists('email', $_POST)) {
date_default_timezone_set('Etc/UTC');
//Load Composer's autoloader
require '../mailer/vendor/autoload.php';
$body = "...1";
$body2 = "...2";
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
//$mail->SMTPDebug = 1; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->SMTPKeepAlive = true; // Send messages in a single SMTP session, much faster
$mail->Username = 'transcriptersdesk@gmail.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls';
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('transcriptersdesk@gmail.com', 'Sir Francis Fish Gelatine'); //from address must be yourself! otherwise will land in spam folder as forgery
$mail->addAddress('info@fishgelatine.co.za', 'Sir Francis Fish Gelatine'); // Add a recipient
//Content
if ($mail->addReplyTo($_POST['email'], $_POST['fname'].' '.$_POST['lname'])) {
$mail->Subject = "Order #".$order_number." received on FishGelatine";
$mail->isHTML(true);
$mail->AddEmbeddedImage('../img/header.png', 'emailheader');
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
if (!$mail->send()) {
//The reason for failing to send will be in $mail->ErrorInfo
$msg = 'Sorry, something went wrong. Please try again later.';
} else {
header("Location: ../thankyou.php?date=".$_POST['date']."&order=".$_POST['order_number']."&product=".$_POST['u_weight']."&qty=".$_POST['u_qty']."&total=".$_POST['u_total']."&fname=".$_POST['fname']."&lname=".$_POST['lname']."&delivery=".$_POST['shipping']."&addr1=".$_POST['addr1']."&addr2=".$_POST['addr2']."&addr3=".$_POST['addr3']."&addr4=".$_POST['addr4']);
$mail->ClearAddresses();
$mail->ClearAttachments();
//Send another email
/*
//This is where I place the new email....Not working though!
*/
}
} else {
$msg = 'Invalid email address, message ignored.';
}
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}
} else {
header("Location: ../order.php?error");
exit();
}
But as soon as I place this piece of code inside it:
//Recipients
$mail->setFrom('transcriptersdesk@gmail.com', 'Sir Francis Fish Gelatine');
$mail->addAddress($_POST['email'], $_POST['fname'].' '.$_POST['lname']));
//Content
if ($mail->addReplyTo('info@fishgelatine.co.za', 'Sir Francis Fish Gelatine') {
$mail->Subject = "Order #".$order_number." placed on FishGelatine";
$mail->isHTML(true);
//Build a simple message body
$mail->AddEmbeddedImage('../img/header.png', 'emailheader');
$mail->Body = $body2;
$mail->AltBody = strip_tags($body2);
if (!$mail->send()) {
//The reason for failing to send will be in $mail->ErrorInfo
//but you shouldn't display errors to users - process the error, log it on your server.
$msg = 'Sorry, something went wrong. Please try again later.';
} else {
exit();
}
I get an http internal 500 error on my website. I tried placing it at different sections of the object but they won’t work. Please help!