Send GIF image in mail by cron job - php

I want to send an email with PHP script which include a gif image in it. It executed successfully(send mail with GIF Image) when I run it via browser URL, but when I run it via cron job/ schedule task it send only the mail body without GIF image.
CODE :
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';
require("connectionFile.php");
$mail = new PHPMailer(true);
try
{
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'xyz.domain.com';
$mail->SMTPAuth = true;
$mail->Username = 'xyz#domain.com';
$mail->Password = 'xyz';
$mail->Port = 25;
$mail->setFrom(xyz#domain.com, 'From:test server');
$mail->addAddress('abc#domain.com');
$mail->isHTML(true);
$mail->Subject = "TEST MAIL";
$mail->AddEmbeddedImage('test1.gif', 'logo_2u');
$mail->Body = "Dear user,<br/><br/><img src='cid:logo_2u' width='500px' height='750px'/></br></br>IT IS A TEST MAIL..<br/>";
$mail->send();
echo 'Message has been sent';
$mail->ClearAllRecipients();
}
catch (Exception $e)
{
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}
?>

I found that cron jobs do not set a PATH environment var by default, so I set an absolute path of image in my script. Below are the changes required :
$path='/var/usr/folder_name/test1.gif';
$mail->AddEmbeddedImage($path, 'logo_2u');

Related

Change Message-ID PHPMailer

Hello I am making a php based mailing application that will connect with a external smtp server and send emails. Now I have managed to match everything but the Message-ID's #domain-name and Sender domain name are not matching...
This is the result I am getting :
Wrong Message ID Header
and this is the result I should be getting (this email is sent from Mailwizz connected with the same SMTP server I am trying to connect with my application)
Expected Message ID Header
send.php file which I am using to connect with SMTP using PHPMailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp*****************';
$mail->SMTPAuth = true;
$mail->Username = 'notify#send.al***********';
$mail->Password = '**********';
$mail->SMTPSecure = 'tls';
$mail->Port = 80;
$mail->SetFrom('jon#al***********','John Adams');
$mail->Sender = 'notify#send.al*********';
$mail->addAddress('*********#gmail.com');
$mail->Subject = 'Hello This is a TEST FROM SMTP';
$mail->isHTML(false);
$mail->Body = 'Hello let me know when its received';
$mail->addCustomHeader('X-Sender', 'notify#send.al**********');
$mail->XMailer=null;
$mail->MessageID = md5('HELLO'.(idate("U")-1000000000).uniqid()).'-'.$type.'-'.$id.'#send.al*********';
if(!$mail->send()){
echo 'Error is '.$mail->ErrorInfo;
}
else{
echo 'Message has been sent!';
}
?>
In my experience, you do not need to use addCustomHeader if you want to set the MessageID.
Assuming that you want to set the Message ID to be [random]#send.alok, then please use the following:
$mail->MessageID = "<" . md5('HELLO'.(idate("U")-1000000000).uniqid()).'#send.alok>';
Hence, please the following will be ok:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require './Exception.php';
require './PHPMailer.php';
require './SMTP.php';
$user='smtp_xxxxxx_user';
$pass='smtp_password';
$mail = new PHPMailer(true);
try {
$mail->CharSet ="UTF-8";
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.xxxxx.com';
$mail->Username = $user;
$mail->Password = $pass;
$mail->MessageID = "<" . md5('HELLO'.(idate("U")-1000000000).uniqid()).'#send.alok>';
$mail->setFrom('jon#alxxxx.com', 'Jon Al');
$mail->addAddress('jon#gmail.com');
$subject="test";
$message="test123";
$mail->Port = 25;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->send();
// echo 'Success';
} catch (Exception $e) {
//echo 'Failed';
}
?>
You may refer to the screen dump below for the result

PHPMailer sends the same email twice

I am using PHPMailer to send emails from a PHP file.
Here you have all the code for it:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
function php_mailer($destinatario,$nombre,$order,$texto,$nom){
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = "...";
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "...";
$mail->Password = "...";
$mail->setFrom("...", "..");
$mail->addAddress($destinatario, $nombre);
$mail->Subject = 'Your Order #:'.$order." at ".$nom;
$mail->msgHTML($texto);
$mail->AltBody = 'HTML messaging not supported';
$status = $mail->Send();
if ($status) {
echo 'Message has been sent.';
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}
}
And here is how am I calling the php_mailer function:
php_mailer($email,"Online Customer",$num_order,$completo,$nombre);
My issue is that PHPMailer is sending every email twice.
I suspect your browser is sending repeated requests due to a plugin. This is not an unusual problem; there is an article about it in the PHPMailer wiki. Try turning off plug-ins and appending random numbers to your subject line, or check your web logs for the repeated requests to be certain.
While I’m here, would you find a PHPMailer video course useful? I’m thinking of creating one and I’m trying to gauge interest.
Refactor a bit. Can you please try this:
// $status = $mail->Send();
if ($mail->Send()) {
echo 'Message has been sent.';
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}

PHPMailer: Parse error on the line 'composer install'

I am trying to use PHPMailer on GoDaddy, this is my first time using it - however, it gives me the error; Parse error: syntax error, unexpected 'install' (T_STRING); on the line composer install, what is wrong? I have looked around at other posts and played around with the code but I still cannot figure out what is wrong.
Any help is greatly appreciated!
<?php
composer install
composer require phpmailer/phpmailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = 'stmp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'MY EMAIL';
$mail->Password = 'MY PASSWORD';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
$mail->setFrom('MY EMAIL', 'MY NAME');
$mail->addAddress('xxx');
// Content
$mail->isHTML(true);
$mail->Subject = 'Subject';
$mail->Body = 'This is the main message';
$mail->AltBody = 'Some body text';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
note: If you are using a shared hosting on go daddy and not a dedicated server, I think you will not be able to run the composer, you will only use the SMTP configuration
otherwise try this
Run the commands via the command line.
composer require phpmailer/phpmailer
A folder called vendor will appear in your directory
root_path/
vendor/
index.php
Your php file should look like this
index.php
<?php
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = 'stmp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'MY EMAIL';
$mail->Password = 'MY PASSWORD';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
$mail->setFrom('MY EMAIL', 'MY NAME');
$mail->addAddress('xxx');
// Content
$mail->isHTML(true);
$mail->Subject = 'Subject';
$mail->Body = 'This is the main message';
$mail->AltBody = 'Some body text';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

Is PHPmailer from github only used for your local host?

My PHPmailer from Github does work on my local host but not on my Firebase hosted website. When I submit on my website it downloads the file. Now how to solve it? Do I have to put some extra code in? Someone experienced with Firebase?
<?php
// get variables from the form
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Load Composer's autoloader
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try{
//Server settings
$mail->SMTPDebug = 2; // 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->Username = 'example#gmail.com'; // SMTP username
$mail->Password = '°°°°°°°'; // SMTP password
$mail->SMTPSecure = 'TLS'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
// Sender
$mail->setFrom($email, $name);
// Recipients
$mail->addAddress('example#gmail.com', 'Luk Ramon'); // Add a recipient
// Body content
$body = "<p>You received an email from your website <br>name:<strong>".$name." </strong><br>subject: <strong>".$subject."</strong><br>message:<br><i>".$message."</i></p> Contact back on ".$email;
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Company-name message from '.$name;
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
Firebase hosting does not support PHP which is why the file is being downloaded.
You'd need to write this in Javascript to use with Firebase - https://firebase.google.com/docs/hosting/functions

PHPMailer, no Email and no Error

I want to send a large mail from my webspace. This mail contains many variables, which are inputed through a form.
First I tried the php mail(), but I didn't receive any mail. I used mail() in another part of my website and from this script I receive a mail. So the function should work on my webspace.
For the large mail I tried to use PHPMailer, because the mail() didn't work as expected. The code for the PHPMailer is:
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'xxx.xxx.de';
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->Username = 'xxx';
$mail->Password = 'xxx';
$mail->SMTPSecure = 'tls';
$mail->From = 'xxx#xxx.de';
$mail->FromName = 'xxx';
$mail->addAddress('xxx#xxx.de', 'xxx xxx');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $mess;
$mail->AltBody = $mess;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
After the script has finished, I don't get an error and I don't receive a mail. What could be a problem for this? How can I check, why my PHPMailer is not working? Has the require path to be absolute or relative?
Try this way:
//... another option code
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
//...another your code
dump($mail->send());
Each time ,you open this php-page,the mail result will let you know.what are you missed.
use try/catch as follow :
$mail = new PHPMailer(true); // enable exceptions
try {
$mail->isSMTP();
//....
}
catch (phpmailerException $e) {
echo $e->errorMessage();
}

Categories