I have been using the following to sent an email and it was working great! However, the for the past few days, it stopped working on my live server and localhost. I thought that it should work? Should i try to use phpmailer instead? I did have problems setting up phpmailer before, that's why I didn't use it....
$to = $email;
//sender
$from = 'pianocourse101@hotmail.com';
$fromName = 'PianoCourse101';
//email subject
$subject = 'Activate your Level 1 Monthly Membership Plan!';
//attachment file path
$file = "codexworld.pdf";
//email body content
$htmlContent = "<h1>Activate your Level 1 Monthly Membership Plan!</h1>
<p>Thank you for registering your Level 1 Monthly Membership Plan with PianoCourse101! You are receiving this e-mail because you or someone else claiming to be you has bought a Level 1 Monthly Membership Plan \n\nIf you believe that this is a mistake, please send us a ticket with the subject \"How to cancel my Level 1 Monthly Membership Plan?\" and allow at least 48 hours before receiving a reply.\n\nHowever, if this is correct, then you must activate your Level 1 Monthly Membership Plan by clicking on the link below: \n\n <a href=http://localhost/loginsystem/includes/activatepremium.php?email=".htmlspecialchars($to)."&activatetoken=".htmlspecialchars($token).">Click here to activate your Level 1 Monthly Membership Plan</a>;
</p>";
//header for sender info
$headers = "From: $fromName"." <".$from.">";
//boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
//headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
//multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n";
//preparing attachment
if(!empty($file) > 0){
if(is_file($file)){
$message .= "--{$mime_boundary}\n";
$fp = @fopen($file,"rb");
$data = @fread($fp,filesize($file));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($file)."\"\n" .
"Content-Description: ".basename($file)."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($file)."\"; size=".filesize($file).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $from;
//send email
$mail = @mail($to, $subject, $message, $headers, $returnpath);
//email sending status
echo $mail?"<h1>Mail sent.</h1>":"<h1>Mail sending failed.</h1>";
I have tried to follow a tutorial from passive coding income and now my updated code looks like this but I am getting the following error. In the tutorial, he is showing how to use both smtp and email but I can't get it to work with the latter:
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
include_once 'PHPMailer/PHPMailer.php';
//Load Composer's autoloader
$mail = new PHPMailer(); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('piano0011@hotmail.com', 'Recipients');
$mail->addAddress('piano0011@pianocourse101.com', 'Joe U'); // Add a recipient
$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
I get this error:
Could not access file: /var/tmp/file.tar.gz Could not access file: /tmp/image.jpg
Fatal error: Uncaught Error: Class 'PHPMailer\PHPMailer\SMTP' not found in C:\xampp\htdocs\loginsystem\PHPMailer\PHPMailer.php:1736 Stack trace: #0 C:\xampp\htdocs\loginsystem\PHPMailer\PHPMailer.php(1861): PHPMailer\PHPMailer\PHPMailer->getSMTPInstance() #1 C:\xampp\htdocs\loginsystem\PHPMailer\PHPMailer.php(1774): PHPMailer\PHPMailer\PHPMailer->smtpConnect(Array) #2 C:\xampp\htdocs\loginsystem\PHPMailer\PHPMailer.php(1516): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Sat, 8 De...', 'This is a multi...') #3 C:\xampp\htdocs\loginsystem\PHPMailer\PHPMailer.php(1352): PHPMailer\PHPMailer\PHPMailer->postSend() #4 C:\xampp\htdocs\loginsystem\phpmailer.php(41): PHPMailer\PHPMailer\PHPMailer->send() #5 {main} thrown in C:\xampp\htdocs\loginsystem\PHPMailer\PHPMailer.php on line 1736
I have tried it with smtp and now I have the following error:
Could not access file: /var/tmp/file.tar.gz Could not access file: /tmp/image.jpg 2018-12-08 04:21:25 SERVER -> CLIENT: 220 ME2PR01CA0106.outlook.office365.com Microsoft ESMTP MAIL Service ready at Sat, 8 Dec 2018 04:21:25 +0000 2018-12-08 04:21:25 CLIENT -> SERVER: EHLO localhost 2018-12-08 04:21:25 SERVER -> CLIENT: 250-ME2PR01CA0106.outlook.office365.com Hello [203.192.94.108]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-STARTTLS250-8BITMIME250 SMTPUTF8 2018-12-08 04:21:25 CLIENT -> SERVER: STARTTLS 2018-12-08 04:21:25 SERVER -> CLIENT: 220 2.0.0 SMTP server ready SMTP Error: Could not connect to SMTP host. 2018-12-08 04:21:25 CLIENT -> SERVER: QUIT 2018-12-08 04:21:25 2018-12-08 04:21:25 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message has been sent
I have have the following errors:
Could not access file: /var/tmp/file.tar.gz Could not access file: /tmp/image.jpg Could not instantiate mail function. Message has been sent