0

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

  • Did you notice any error displayed while running this code ????? – JIJOMON K.A Dec 07 '18 at 12:21
  • 1
    You use @ its error suppression, you should delete @ and truing send and show erros –  Dec 07 '18 at 12:22
  • 1
    Why not using proper libraries for that? Like https://github.com/PHPMailer/PHPMailer – Justinas Dec 07 '18 at 12:30
  • I tried using phpmailer before but for some reason, it could never find the class or libraries –  Dec 07 '18 at 12:34
  • I will check again but there are no errors relating to my mail function –  Dec 07 '18 at 12:38
  • In one of my other section, when I upgraded my plan, it said that the mail was sent but I still didn't receive any email confirmation.... –  Dec 07 '18 at 12:43
  • for the mail function, which port do you need to be free? –  Dec 07 '18 at 13:33
  • Possible duplicate of [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – aynber Dec 07 '18 at 14:27

2 Answers2

0

Not an answer, but path to find answer

Try mail function without suppressing errors i.e. remove @

$mail = @mail($to, $subject, $message, $headers, $returnpath);

to

$mail = mail($to, $subject, $message, $headers, $returnpath);

This way you will be able to error while sending email.

codemirror
  • 3,164
  • 29
  • 42
  • Warning: Your version of PHP is affected by a bug that may result in corrupted messages. To fix it, switch to sending using SMTP, disable the mail.add_x_header option in your php.ini, switch to MacOS or Linux, or upgrade your PHP to version 7.0.17+ or 7.1.3+. in C:\xampp\htdocs\loginsystem\PHPMailer\PHPMailer.php on line 1396 Message has been sent –  Dec 08 '18 at 04:12
  • even my live server email function has stopped working –  Dec 08 '18 at 05:59
  • With regard to turning on error reporting, must I put this on this particular page that I am having problem with or can it just be on the main index.php page? –  Dec 08 '18 at 12:18
  • I will try again guys but really need this function to work... I am still baffled as to why it doesn't work... –  Dec 09 '18 at 01:17
0

I think your problem should be because you're using your personal computer as SMTP server and the DKIM or SPF tools on the hotmail.com domain are doing it's job, blocking your email message.

You should use phpMailer library, or other similar library, and log in on the SMTP server of the from address domain with a valid user and password.

require("class.phpmailer.php");
$mail = new PHPMailer();

//Luego tenemos que iniciar la validación por SMTP:
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "*** your smtp server ***";
$mail->Username = "*** your username on the smtp server ***";
$mail->Password = "*** the password for the user on the smtp server ***";

You can search on this site other answers related about using hotmail.com as SMTP server, like this one: Sending mail in PHP using Hotmail smtp

NetVicious
  • 3,848
  • 1
  • 33
  • 47