Can someone tell me why phpMailer not working on my webhost. I test my website locally with XAMMP and email sending with phpMailer is working correctly but when I upload my files to my webhost server it doesn't work. my script do not tell me any errors. neither my server error_log message is appended.
Is there any configuration that I may need to do on my server?
This is how am using phpMailer:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//mailer function
function sendmail($mailFrom,$orgName,$toMail,$replyTo,$mailSugject,$mailBody){
//Load composer's autoloader
require 'mailer/vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Recipients
$mail->setFrom($mailFrom, $orgName);
$mail->addAddress($toMail);// Add a recipient
$mail->addReplyTo($replyTo, $orgName);
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $mailSugject;
$mail->Body = $mailBody;
$mail->send();
return array('Mailer' => 1,'msg' => 'Message has been sent');
} catch (Exception $e) {
return array('Mailer' => $mail->ErrorInfo ,'msg' => 'Message could not be sent');
}
}
Related
I am using the last version of PHPMailer to send emails from my web. I can send some emails without SMTP parameters, but i need to give the SMTP configuration to my object PHPMailer(). The functions that sends this emails are in the same file.
Why some emails could need SMTP configurations and not others?
Regards
function sendEmailWithoutSMTP(){
$bodyIntern = file_get_contents('emails/myemail.html');
$mailIntern = new PHPMailer(true);
$mailIntern->CharSet = "UTF-8";
try {
//Recipients
$mailIntern->AddAddress('myemail#myemail.com', 'My address');
$mailIntern->SetFrom('myemail#myemail.com', 'My address');
$mailIntern->AddReplyTo('myemail#myemail.com', 'My address');
//Content
$mailIntern->Subject = 'My subject';
$mailIntern->AltBody = '';
$mailIntern->MsgHTML($bodyIntern);
$mailIntern->Send();
} catch (phpmailerException $e) {
//echo $e->errorMessage();
} catch (Exception $e) {
//echo $e->getMessage();
}
}
function sendEmailWithSMTP($email){
require_once 'smtp/getsmtp.php';
$smtp= get_smtp($email);
$bodyClient = file_get_contents('emails/myotheremail.html');
$mailClient = new PHPMailer(true);
$mailClient->CharSet = "UTF-8";
try {
//Server settings
$mailClient->IsSMTP();
$mailClient->SMTPDebug = SMTP::DEBUG_OFF;
$mailClient->SMTPAuth = true;
$mailClient->SMTPSecure = $smtp["secure"];
$mailClient->Host = $smtp["host"];
$mailClient->Username = $smtp["username"];
$mailClient->Password = $smtp["pass"];
$mailClient->Port = $smtp["port"];
$mailClient->Timeout= 30;
//Recipients
$mailClient->AddAddress('myemail#myemail.com', 'My address');
$mailClient->SetFrom('myemail#myemail.com', 'My address');
$mailClient->AddReplyTo('myemail#myemail.com', 'My address');
//Content
$mailClient->Subject = 'My subject';
$mailClient->AltBody = '';
$mailClient->MsgHTML($bodyIntern);
$mailClient->Send();
} catch (phpmailerException $e) {
//echo $e->errorMessage();
} catch (Exception $e) {
//echo $e->getMessage();
}
$this->sendEmailWithoutSMTP();
}
PHPMailer has several different ways of sending messages.
First of all there is the default, which uses PHP's built-in mail() function behind the scenes. This does not use SMTP, but hands the message to a sendmail program using a path set in your php.ini file. This sending method does not require any SMTP setup, and SMTP-related params (e.g. Username, Host) will just be ignored, but it does require that you have a local mail server.
isSendmail() is like a manually-constructed equivalent to PHP's built-in function, but it gives you a bit more control if you are using some unusual local mail server. Generally you should not use this.
If you call isSMTP(), it will try to use SMTP directly, and all of the SMTP parameters will be used. Even if you do have a local mail server, this is the recommended route because it's faster, safer, and easier to debug. If you need to send through a remote mail server (such as gmail), this is your only option.
So in your code, the first function is sending using mail(), and the second uses SMTP.
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');
i have installed php mailer using composer PHPMailer/PHPMailer and i got PHPMailer-Master directory but how and what files can i include to my mail file i.e sendEmail.php.
Here is my sendEmail.php-
include 'What is include paht?'
$mail = new PHPMailer;
$mail->setFrom($email, $name);
$mail->addAddress('dev5.veomit#gmail.com', 'Admin');
$mail->Subject = 'First PHPMailer Message';
$mail->Body = 'Hi! This is my first e-mail sent through PHPMailer.';
if(!$mail->send()) {
header("Location: https://m-expressions.com/test/voy/");
}else {
echo 'Mailer error: ' . $mail->ErrorInfo;
}
Please help me to complete this code. thanks in advance.
If you have installed the PHPMailer via Composer, you should have a vendor folder in your project. Include /vendor/autoload.php
So I have a contact form that uses phpmailer. It sends an email from one Gmail account to another. But I can't seem to get the receiving email to receive any emails.
The script is hosted on a cpanel (RivalHost) and the domain is on GoDaddy. I asked RivalHost if they were blocking SMTP connections or blocking ports 587 or 465, and they said they weren't. So I have no idea what's causing the issue. The script works perfectly fine on my localhost, just not on cpanel
Heres the mailing script:
<?php
$result="";
if(isset($_POST['submit'])){
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->Host='smtp.gmail.com';
$mail->Port=465;
$mail->SMTPAuth=true;
$mail->SMPTSecure='ssl';
$mail->Username='sendingemail#gmail.com';
$mail->Password='*********';
$mail->setFrom('sendingemail#gmail.com');
$mail->addAddress('receivingemail#gmail.com');
$mail->addReplyTo($_POST['email'],$_POST['name']);
$mail->isHTML(true);
$mail->Subject='Contact: '.$_POST['subject'];
$mail->Body='Message: '.$_POST['msg'].'</h1>';
if(!$mail->send()){
$result='something went wrong';
echo $result;
} else {
$result="thank you";
echo $result;
}
}
?>
I was also told to check my MX records, but wasn't really sure what to change them to, or if I needed to change them at all:
MX 0 ********.com 3599 RBL
Solution 1:
PHPMailer uses exeptions. You can put your code in try/catch block to get exceptions caused emails not to be sent.
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
try {
//Email information comes here
$mail->Send();
echo "Message Sent OK\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
}
Solotion 2:
Are you also using the CSF firewall? If so, check to see if the "SMTP_BLOCK" setting is enabled. If STMP_BLOCK is enable contact to hosting to disable it.
Add this to your settings:
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
I'm working on a registration page on a website where I need to implement a email verification.
I have installed PHPMailer-master on the server. The first part of the code is written directly in the registration php:
include('send_mail.php');
$to=$email;
$subject="Email verification";
$body='Hi, <br/> <br/> thank you for joining us! Please verify your email and get started using your account. <br/> <br/> '.$base_url.'activation/'.$activation.'';
Send_Mail($to,$subject,$body);
$msg= "Registration successful, a verification email has ben sent to you. Please activate this to get started.";
And the code in send_mail.php:
<?php
function Send_Mail($to,$subject,$body)
{
require 'PHPMailer-master/class.phpmailer.php';
require 'PHPMailer-master/PHPMailerAutoload.php';
$from = "*mywebsite*";
$mail = new PHPMailer();
$mail->IsSMTP(true); // use SMTP
$mail->IsHTML(true);
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "*my webhost's mailserver*"; // SMTP host
$mail->Port = 465; // set the SMTP port
$mail->Username = "*username*"; // SMTP username
$mail->Password = "*password*"; // SMTP password
$mail->SetFrom($from, '*mywebsite?*');
$mail->AddReplyTo($from,'*mywebsite?*');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, $to);
$mail->Send();
}
?>
I'm fairly sure the information in send_mail.php is correct. I'm using the information I found on my configure mail client page on cPanel ("Secure SSL/TLS Settings").
When I try to register, the website goes white somehow. The account gets created on phpmyadmin. No error-log in public_html.
Any ideas?
UPDATE: Sorry for late response.
I've tried using the PHPMailer Test Page. When I try SMTP, the page just loads forever. When using Sendmail, I get the error "Could not execute: /usr/sbin/sendmail -t -i". When I use the QMAIL, it says the email was sent but I never recieve it.
BUT... when I use Mail, I recieve it successfully. Is "Mail" secure enough to use? What are the differences? And how should I implement it into the php code of the register page? I can't find any specific part in the PHPMailer Test Page code for the "mail" function.
Code looks like this now:
$email = $_POST['email'];
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer(true);
try {
$mail->AddReplyTo('myemail');
$mail->AddAddress($email);
$mail->SetFrom('myemail');
$mail->AddReplyTo('myemail');
$mail->Subject = 'subject';
$mail->Message = 'message comes here';
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
Error message: "Message body empty"