Mail with attachments doesn't get past 'sent' - PHP 8.0
Go to solution
Solved by Jonathan_bdlf,
On 11/25/2023 at 9:07 AM, C2dan88 said:Try using PHPMailer. It provides alot more flexibility over the simple mail function.
https://github.com/PHPMailer/PHPMailer
No need to memorize mail headers/formatting which is probably why your email is sent incorrectly.
Worked like a charm, thanks
My mails are getting a 9/10 score on https://www.mail-tester.com/
I have to sign the message with DKIM to receive a 10/10
Here's some information I've gathered while debugging that may help some people :
-
Install PHPMailer
-
Run at the base directory of your web server (e.g. /opt/bitnami/apache/htdocs) :
composer require phpmailer/phpmailer
- RTFM, it's well documented !
-
Run at the base directory of your web server (e.g. /opt/bitnami/apache/htdocs) :
-
Check if your mail address isn't flagged as spam
-
Due to extensive testing, my (OVH) mail have been flagged as spam.
- The resulting logs were "SASL authentication failed."
- The result behavior was simple : No mail received nor sent
-
I had to change the password. Now everything's fine ! (Don't forget to flush postfix queue )
sudo postqueue -f
-
Due to extensive testing, my (OVH) mail have been flagged as spam.
-
(Re)configure postfix -> https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-postfix-as-a-send-only-smtp-server-on-ubuntu-18-04-fr
- Configure the SMTP relay
- Write PHP to send mail using postfix :
$to = $user['usr_mail']; $from = 'contact@mydomain.fr'; $fromName = 'Jonathan Bdlf Photographie '; if ($bc_type == 1){ // Si couple $subject = 'Votre bon cadeau pour une séance photo en couple '; } else if ($bc_type == 2){ // Si famille $subject = 'Votre bon cadeau pour une séance photo en famille '; } $prenom = $user['usr_prenom']; $nomcomplet = $user['usr_prenom'].$user['usr_nom']; //Import PHPMailer classes into the global namespace //These must be at the top of your script, not inside a function use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; // Not sure if useful use PHPMailer\PHPMailer\Exception; //Load Composer's autoloader require '/opt/bitnami/apache2/htdocs/vendor/autoload.php'; //Create an instance; passing `true` enables exceptions $mail = new PHPMailer(true); try { //To load the French version $mail->setLanguage('fr', '/opt/bitnami/apache2/htdocs/vendor/phpmailer/phpmailer/language/phpmailer.lang-fr.php'); // Because I'm french ! Lots of language come with the package //Server settings $mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output $mail->isMail(); //Send using Postfix $mail->CharSet = "UTF-8"; //Recipients $mail->setFrom($from, $fromName); $mail->addAddress( $to,$nomcomplet ); //Add a recipient $mail->addReplyTo($from, $fromName); //Attachments $mail->addAttachment($outputPDF); //Optional name //Content $mail->isHTML(true); //Set email format to HTML $mail->Subject = $subject; $mail->Body = '<p>Merci pour votre achat</p>'; $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}"; }
Voila
I hope this may help some one over on the internet.
Thanks again @C2dan88 for your help
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now