Inconsistent duplicate emails occurring when using php mailer.
Function that mails:
function SendEmail($to,$cc,$bcc,$subject,$body) {
require( GetPHPMailPath() );
$mail = new PHPMailer();
$addresses = explode(',', $to);
foreach ($addresses as $address) {
$mail->AddAddress($address);
}
if($cc!='') {
$mail->addCustomHeader("CC: " . $cc);
}
if($bcc!=''){
$mail->addCustomHeader("BCC: " . $bcc);
}
$mail->IsSMTP();
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587;
$mail->Username = "email#email.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "email"; //Reply to this email ID
$name=$email;
$mail->From = $webmaster_email;
$mail->FromName = "Service";
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = $subject;
$mail->Body = $body;
return $mail->Send();
}
How I am calling the function:
echo SendEmail($toAddress,$ccAddress,$bccAddress,$subject,$body);
The really odd part about this whole ordeal is that it is inconsistent which means there may be nothing wrong with the code but the connection to gmail?
Any ideas maybe its a php.ini problem?
This was a lag related issue.
PHPMailer functioned properly. User was sending duplicate requests. Fixed by adding comparison check with MySQL database records.
Related
I don't know how to send a mail using PHPmailer in a function.
I want to do something similar to this - when I'll want to send a mail, I'll just call a function (like send::mail()). here is my code:
<?php
require '.\vendor\autoload.php';
//PHPMailer Object
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->IsSendmail();
//From email address and name
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->setFrom('my.bot.mail#gmail.com', 'Bot name');
$mail->Password = "########"; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;
//To address and name
$mail->addAddress("mymail#gmail.com"); //Recipient name is optional
//Address to which recipient will reply
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
$mail->Mailer = "sendmail";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
can someone help me put it into a function? when I tried, the "use" command didn't work.
First, you must determine the data.
$mailHost = '';
$mailSMTPAuth = '';
$mailUsername = '';
$mailPassword = '';
$mailSMTPSecure = '';
$mailPort = '';
$mailSenderAddress = '';
$mailSenderName = '';
Then you can generate a function that contains only the recipient, subject and message, and you can send the mail easily by pull the settings into this function:
function sendMail($sendMailAdress, $sendMailName, $sendMailSubject, $sendMailMessage) {
global $mailHost;
global $mailSMTPAuth;
global $mailUsername;
global $mailPassword;
global $mailSMTPSecure;
global $mailPort;
global $mailSenderAddress;
global $mailSenderName;
require_once('class/phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $mailHost; // Specify main and backup SMTP servers
$mail->SMTPAuth = $mailSMTPAuth; // Enable SMTP authentication
$mail->Username = $mailUsername; // SMTP username or mail
$mail->Password = $mailPassword; // SMTP password
$mail->SMTPSecure = $mailSMTPSecure; // Enable TLS encryption, `ssl` also accepted
$mail->Port = $mailPort; // TCP port to connect to
$mail->CharSet = 'utf-8';
$mail->setFrom($mailSenderAddress, $mailSenderName);
$mail->addAddress($sendMailAdress, $sendMailName); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $sendMailSubject;
$mail->Body = $sendMailMessage;
if(!$mail->send()) {
return false;
} else {
return true;
}
}
The function will work like this:
sendMail('hello#test.com', 'Test User', 'Subject of test mail', 'And your mail message);
I am trying to send a fax via the eFax service. As I understand, this should be as easy as sending a simple email to the specific address.
I use PHPMailer for emails. Ordinary emails are sending fine. Even when I send to an efax address $mail->send returns true, that means that the email was sent seccessfully, but the recipient didn't receive the fax.
Any suggestions?
Thank you.
function sendFax($fax, $msg) {
$efax_number = $fax . "#efaxsend.com";
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = EMAIL_HOST; // Set mailer to use SMTP
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = EMAIL_SMTP_USERNAME;
$mail->Password = EMAIL_SMTP_PASSWORD; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
//From email address and name
$mail->From = "orders#orderapp.com";
$mail->FromName = "Test";
//To address and name
$mail->addAddress($efax_number); // SEND EMAIL TO USER
//Send HTML or Plain Text email
$mail->isHTML(false);
$mail->Subject = ' New Order Fax ';
$mail->Body = $msg;
$mail->AltBody = "Sample";
$result = $mail->send();
if (!$result)
{
...
}
else
{
...
}
}
require_once( './PHPMailer-master/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.office365.com';
$mail->ClearReplyTos();
$mail->addReplyTo($organizer_email, $organizer);// Specify main and backup server
$mail->SetFrom('xyx#xyx.com', 'zyx');
$mail->SMTPAuth = tls; // Enable SMTP authentication
$mail->Username = 'xyz#xyz.com'; // SMTP username
$mail->Password = 'xyzzzzz'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->Port = 587; //Set the SMTP port number - 587 for authenticated TLS
$mail->addAddress($organizer_email); // Add a recipient
$content = file_get_contents($resume_attach_path);
$content = chunk_split(base64_encode($content));
$eol = "\r\n";
$tags = explode(',', $to);
foreach ($tags as $tag) {
$mail->addCC($tag);
}
$mail->WordWrap = 50;
$mail->Subject = $subject;
$mail->Body = $desc;
$mail->AltBody = $text; // in your case once more the $text string
$mail->Ical = $text; // ical format, in your case $text string
$mail->isHTML(true); // Set email format to HTML
//$mail->addStringAttachment($resume_attach_path,'base64');
if ($mail->send()) {
return true;
}else{
return false;
}
if i use addAttachment i can just see the attachment, cannot see the ical invite ,but if i remove the attachment i can see the ical invite,how can i send both inline ical and external document attachment in phpmailer.
I am showing you a simple working example here
<?php
function sendMail($subject, $body, $name, $email, $attachment = null){
$mail = new PHPMailer(true);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "**************"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "*************"; // GMAIL username
$mail->Password = "***********"; // GMAIL password
//Typical mail data
$mail->AddAddress($email, $name);
$mail->SetFrom('*********', '************');
$mail->Subject = $subject;
$mail->Body = $body;
$mail->IsHTML(true);
if($attachment){
$mail->AddAttachment($attachment['tmp_name'],
$attachment['name']);
}
try{
$mail->Send();
} catch(Exception $e){
//Something went bad
// print_r($e);
echo "Fail :(";
die;
}
}
?>
In this code $attachment is $_FILES['file']. How to call this function is given below.
sendMail($subject, $body, $name, $email, $_FILES['file']);
To use Ical in mailer see a Stackoverflow Question
According to comment in PHPMailer code:
Only supported in simple alt or alt_inline message types
when you add other attachments, part of adding Ical file based on Ical property is ommited.
I have used the PHP mailer function to send a mail. This code has worked on XAMPP and 000webhost.com, but its not working on aws.
<?php
require("\PHPMailer\PHPMailer\class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
//$mail->SMTPDebug = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "****#gmail.com"; // Enter your SMTP username
$mail->Password = "************"; // SMTP password
$webmaster_email = $_POST['email']; //Add reply-to email address
$email= "***#gmail.com"; // Add recipients email address
$name= "Recipient's name"; // Add Your Recipient’s name
$mail->From = $webmaster_email;
$mail->FromName = $_POST['name'];
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,$_POST['name']);
$mail->WordWrap = 500; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "website inquiry - Support";
$mail->Body = nl2br($_POST['message']);
$mail->AltBody = nl2br($_POST['message']); //Plain Text Body
if($mail->Send())
echo 1;
else
echo 0;
?>
Please tell me where I am going wrong.
When I am executing this code on aws..its echoing 0
Thanks in advance!
Try with throwing errors by adding "true" to constructor:
try {
new PHPMailer(true);
...your code here...
}
catch (phpmailerException $e) {
echo $e->errorMessage();
}
So perhaps there are some more informations about the error.
I am trying to setup PHPMailer for a customer. He has his own mail server located at a certain IP address. When asked to give me the information to send email through the system, he gave the following:
Host: xx.xxx.x.x
Port: 25
Domain: mydomain.local
Username: myemail#mydomain.local
Password: <myemailpassword>
From: myemail#anotherdomain.xx
(Which he confirmed is being used for external email sending)
I tried to setup PHPMailer by setting the parameters to the exact namings above.
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "xx.xxx.x.x";
$mail->Port = 25;
$mail->Username = "myemail#mydomain.local";
$mail->Password = <myemailpassword>;
$mail->SetFrom('myemail#anotherdomain.xx', 'Webname');
$mail->[...]
I got the following error:
Failed to connect to server (0)
So I try to send an email through telnet to check if it's the customer's email server or the PHPMailer settings:
telnet xx.xxx.x.x 25
It goes through, I'm connected to the server.
helo mydomain.local
I'm getting 'Hello' as a reply. This leads me to believe it might be the PHPMailer settings that are wrong here.
I also try not using SMTP:
$mail->Host = "ssl://xx.xxx.x.x";
$mail->Port = 25;
$mail->Username = "myemail#mydomain.local";
$mail->Password = "password";
$mail->SetFrom('myemail#anotherdomain.xx', 'Webname');
$mail->[...]
Again no go. Am I going about this wrong? I'm only familiar with setting up PHPMailer to use Gmail before so I'm at a loss as to what could be the issue because I'm using a 'personal' email server.
Thanks Loadparts for your assistance.
I'm still not sure what the issue was but it seems it has resolved itself. It might have been from the email server side because coding wise, I didn't change anything. This is the final code I used.
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Port = 25;
$mail->Host = "xx.xxx.x.x"; // SMTP server
$mail->Username = "myemail#mydomain.local";
$mail->Password = <myemailpassword>;
$mail->From = "myemail#anotherdomain.xx";
$mail->FromName = <Web_Name>;
$mail->AddAddress("email#domain.com");
$mail->Subject = <Subject>;
$mail->AltBody = <Alt_Body>
$mail->WordWrap = 80;
$body = "test message";
$mail->MsgHTML($body);
$mail->IsHTML(true);
$mail->Send();
I use a test function that I know works 100% to test the email servers when using PHPMailer.
I'm not sure why you are having your problem, but try to use the function I have ( I know it's messy but it does the trick). Just replace all the XXXX with your info and make sure you have both class.phpmailer.php and class.smtp.php in the same folder.
<?php
error_reporting(E_ALL);
$toemail = 'XXXX';
$toname = 'XXXX';
$subject = 'Testing Email Sending...';
$bodyhtml = '<H1>yeah</h1>';
$bodytext = 'heres Hoping it works';
$fromemail = 'XXXX';
$fromname = 'XXXX';
var_dump(sendemail($toemail,$toname,$subject,$bodyhtml,$bodytext,$fromemail,$fromname));
function sendemail($toemail,$toname,$subject,$bodyhtml,$bodytext,$fromemail,$fromname)
{
require_once("class.phpmailer.php");
$mail = new phpmailer();
$mail->IsSMTP();
$mail->From = $fromemail;
$mail->FromName = $fromname;
$mail->Host = "XXXX";
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "XXXX"; // SMTP username
$mail->Password = "XXXX"; // SMTP password
$mail->Port="25";
$mail->SMTPDebug=true;
if(strlen($bodyhtml)>0) {
$mail->Body = $bodyhtml;
$mail->IsHTML(true);
}
else if(strlen($bodytext)>0){
$mail->Body = $bodytext;
}
if(strlen($bodytext)>0 && strlen($bodyhtml)>0){
$mail->AltBody = $bodytext;
}
$mail->AddReplyto($fromemail,$fromname);
$mail->Subject = $subject;
// Check if multiple recipients
if(preg_match("/;/",$toemail))
{
$tmp_email=preg_split("/;/",$toemail);
$tmp_contact=preg_split("/;/",$toname);
$mail->AddAddress($tmp_email[0], $tmp_contact[0]);
// echo "<!-- multi email:".$tmp_email[0]." contact:".$tmp_contact[0]." -->\n";
for($j=1;$j<count($tmp_email);$j++)
{
if(preg_match("/\#/",$tmp_email[$j]))
{ $mail->AddCC($tmp_email[$j], $tmp_contact[$j]);
// echo "<!-- multi email cc:".$tmp_email[$j]." contact:".$tmp_contact[$j]." -->\n";
}
}
}
else{
$mail->AddAddress($toemail, $toname);
}
$error= false;
if($mail->Send()){
$error =true;
}
// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
return $error;
}
If this doesn't work, my first try would be using port 80 - which usually isn't blocked, then you can work on getting SSL to work.
PS: because it's a local domain, you may want to consider adding the domain to your /etc/hosts just to be sure.
Best of Luck!