PHPMailer emails not sending but there are no errors - php

I am using PHPMailer and using this PHP Code to send emails:
$email = new PHPMailer();
while($result2=mysql_fetch_array($rs2))
{
$email->AddAttachment( $result2["attachment"] , basename($result2["attachment"]) );
}
$email->From = 'Integra Digital';
$email->FromName = $result["emailfrom"];
$email->Subject = $result["subject"];
$email->Body = $result["message"];
$email->AddAddress($result["emailto"]);
$email->IsHTML(true);
if(!$email->Send())
{
echo "<strong>Mailer Error: </strong>" . $email->ErrorInfo;
}
else
{
echo '<strong>Email sent to: </strong>' .implode(',',$emails_list). '<br/ >';
}
$result["emailto"] is equal to a valid email address but i am not receiving the emails
any ideas?

looks like it wanted the AddAttachment part after everything else and just before the Send function

Related

Sending and retriving an email using a php form

I am attempting to retrieve a sender's email, sender's name, mail subject, and mail message sent.
You will also see in the code i want to check if the name, email address, and message of a sender are not empty using empty()function. If one of them are empty, i want to display an error message in the document. Otherwise, call send_email() function.
Right now my output is displaying parts of my code. I need help with the formatting or maybe I am missing a step.
<?php
require_once ('c:/wamp64/www/Lab6/PHPMailerAutoload.php');
function sendEmail($fromEmail,$fromName,$subject, $message){
$mailer = new PHPMailer;
$mailer->isSMTP();
$mailer->Host = 'smtp.gmail.com';
$mailer->Port = 587;
$mailer->SMTPSecure = 'tls';
$mailer->SMTPAuth = true;
$mailer->Username= "geogumd#gmail.com";
$mailer->Password= "gis#umd2016";
$mailer->From= $fromEmail;
$mailer->FromName=$fromName . "(" . $fromEmail . ")";
$mail->IsHTML(true);
$mailer->Subject=$subject;
$mailer->Body=$message;
$mailer->addAddress('worthem.aleah#terpmail.umd.edu', 'Aleah');
$mailer->Sendmail($fromEmail,$fromName,$subject,$message);
if(!$mailer->send()) {
$h = "Mailer Error " . $mailer->ErrorInfo;
$m = "Mailer Error was not sent";
print "<h1>$h</h1>";
print "<pre>$m</pre>";
}else{
print "<h1>Mail Sent</h1>";
print "<p>Thank you. Your message has been sent successfully.</p>";
}
}
?>
<div id="content">
<?php
$user_input1 = $_POST['From'];
$user_input2 = $_POST['FromName'];
$user_input3 = $_POST['Subject'];
$user_input4 = $_POST['Body'];
if(!empty($user_input1) && is_string($user_input1)&&
(!empty($user_input2) && is_string($user_input2))&&
(!empty($user_input3) && is_string($user_input3))&&
(!empty($user_input4) && is_string($user_input4))
)
{
sendEmail($user_input1, $user_input2, $user_input3, $user_input4);
}
else{
print "<h4>Sorry</h4>";
print "<p>You didnt fill out the form completley. <a href='index.php?action=sendmail'>Try Again";
}
?>

Error using phpMailer and isHTML

Im using phpMailer to send an image like attachment and a message in HTML. But the page show me that is not working.
include 'PHPMailer-master/class.phpmailer.php';
$mail = new PHPMailer();
$mail->From = 'someone#some.com';
$mail->FromName = 'someone';
$mail->AddAddress( 'someone#some2.com' );
$mail->Subject = 'Message Subject';
$mail->Body = "<h1>Test 1 of PHPMailer html</h1><p>This is a test</p>";
$mail->AltBody="This is text only alternative body.";
$mail->IsHTML(true);
$file_to_attach = '../images/logo.png';
$mail->AddAttachment( $file_to_attach , 'logo.png"' );
if(!$mail->Send())
echo "Error sending: " . $mail->ErrorInfo;;
else
echo "Letter is sent";
Using this code works perfectly to me send a message with HTML and an attachment.
<?php
require_once 'PHPMailer-master/class.phpmailer.php';
$mail = new PHPMailer(true);
try {
$mail->AddAddress('someone#some.com', 'a name');
$mail->SetFrom('someone2#some2.com', 'another name');
$mail->AddReplyTo('someone3#some3.com', 'another name');
$mail->Subject = 'PHPMailer Test';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$mail->IsHTML(true);
$mail->Body = "<h1>Test 1 of PHPMailer html</h1><p>This is a test</p>";
$mail->AddAttachment('../images/logo.png');
$mail->Send(); echo "Message Sent OK\n";
}
catch (phpmailerException $e) {
echo $e->errorMessage();
}
catch (Exception $e) {
echo $e->getMessage();
}
?>
You are using $email variable to new object PHPMailer but in line 8,9 and 14,15 you're using $mail.
$mail->AltBody="This is text only alternative body.";
$mail->IsHTML(true);
if(!$mail->Send())
echo "Error sending: " . $mail->ErrorInfo;
just use $email.
I see you are using
$file_to_attach = '../images/logo.png';
to get the name for the image file. However the file name is already set in:
$email->AddAttachment( $file_to_attach , 'logo.png"' );
Try putting the whole path in $email->AddAttachment(); like this $email->AddAttachment('../images/Logo.pgn');

Two versions of same email to two different recipients using class.phpmailer.php

I'm trying to send two versions of the same email to two recipients using phpMailer
Depending on the email I need to send one version or another.
At the moment I'm able to send only the same version to both email address
<?php
require_once('class.phpmailer.php');
if(isset($_POST['Submit'])) {
$fname = $_POST['fname'];
$maileremail = $_POST['maileremail'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mail = new PHPMailer();
$text = 'The person that contacted you is '.$fname.' E-mail: '.$maileremail.' Subject: '.$subject.' Message: '.$message.' :';
$body = eregi_replace("[\]",'',$text);
$text2 = 'The person that contacted you is '.$fname.' E-mail: REMOVED - To get email address you must login Subject: '.$subject.' Message: '.$message.' :';
$body2 = eregi_replace("[\]",'',$text2);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "xxxxx.xxxxx.xxx"; // SMTP server
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent
$mail->Host = "xxxxx.xxxxx.xxx"; // sets the SMTP server
$mail->Port = XXXXXXXX; // set the SMTP port for the GMAIL server
$mail->Username = "xxxxx#xxxxx.xxx"; // SMTP account username
$mail->Password = "XXXXXXXX"; // SMTP account password
$mail->SetFrom('xxxxx#xxxxx.xxx', 'XXXXXXXX');
$mail->Subject = $subject;
$add = array("first#email.xxx", "second#email.xxx");
foreach($add as $address) {
if($address == "first#email.xxx") {
$mail->AddAddress($address, "xxxxxxxxx");
$mail->Body = $body;
}
elseif($address == "second#email.xxx") {
$mail->AddAddress($address, "xxxxxxxxx");
$mail->Body = $body2;
}
}
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "<h3><b>Message sent!</b></h3>";
}}?>
If you're looking to do all the setup once for minimal repetition, simply clone your $mail object in your foreach($add) loop:
foreach ( $add as $address ) {
$current = clone $mail;
if ( $address == 'first#mail.com' ) {
$current->AddAddress($address, "xxxxxxxxx");
$current->Body = $body;
$current->send();
} elseif....
This creates a separate clone of your $mail object for every loop, allowing you to add different email bodies, subjects, etc. without having to rewrite all connection settings.
I think you want to seach the $maileremail inside $add and send the desired email.
$add = array("first#email.xxx", "second#email.xxx");
$mail->AddAddress($maileremail, "xxxxxxxxx");
if($maileremail === $add[0])
$mail->Body = $body;
if($maileremail === $add[1])
$mail->Body = $body2;
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "<h3><b>Message sent!</b></h3>";
}
EDITED:
I misunderstood the question. Brian is right.

Mail send with PHPMailer doesn't work

So I'm trying to use PHPMailer to handle the email form on my website.
I wrote the code here based on a tutorial I found.
<?php
error_reporting(E_ALL);
require_once("class.phpmailer.php");
include("class.smtp.php");
$email = new PHPMailer();
//
// Set server details for send
//
$email->IsSMTP();
$email->Host = "mail.loganyoung.za.net";
$email->Port = 25;
$email-SMTPAuth = true;
$email->Username = "<my email>";
$email->Password = "<my password>";
//
// Send mail from the contact form
//
$to = "<my email>";
$from = $_POST["from"];
$name = $_POST["name"];
$subject = "From web: ".$_POST["subject"];
$message = $_POST["message"];
$body = "<p>Hi Logan,</p>";
$body .= "<p>You have received a new query on your website.<br />Please see below:</p>";
$body .= "<p>";
$body .= str_replace("\r\n", "<br />", $message);
$body .= "</p>";
$email->SetFrom($from, $name);
$email->AddReplyTo($from, $name);
$email->AddAddress($to, "LoganYoung.za.net");
$email->Subject = $subject;
$email->Body = $body;
$email->IsHTML = true;
session_start();
if(!$email->Send()) {
$_SESSION["mailresult"] = "success";
echo "success";
} else {
echo "<p>Failed:</p><p>".$email->ErrorInfo."</p>";
$_SESSION["mailresult"] = "failed";
$_SESSION["mailerror"] = $email->ErrorInfo;
}
?>
I figure possible reasons for it not sending could be...
Incorrect SMTP details (possibly send without SMTP auth to resolve?)
Handler isn't getting POST data (the ajax that sends it seems to work fine though)
Some problem with this code that I'm not able to identify...
As a means of eliminating possibilities, can anyone spot anything wrong with the code here? If so, what's wrong, and how do I fix it?
Thanks in advance!
$email-SMTPAuth = true;
Isn't that supposed to be:
$email->SMTPAuth = true;

Sending an EMail using Wordpress

I have tried so many different approaches, but cannot successfully send an EMail through SMTP in PHP using the mail() function.
<?php
require_once ABSPATH . WPINC . '/class-phpmailer.php';
require_once ABSPATH . WPINC . '/class-smtp.php';
$phpmailer = new PHPMailer();
$phpmailer->SMTPAuth = true;
$phpmailer->Username = 'skchourasia#asselslutions.com';
$phpmailer->Password = 'password01';
 
$phpmailer->IsSMTP(); // telling the class to use SMTP
$phpmailer->Host       = "mail.asselsolutions.com"; // SMTP server
$phpmailer->FromName   = $_POST[your_email];
$phpmailer->Subject    = $_POST[your_subject];
$phpmailer->Body       = $_POST[your_message];                      //HTML Body
$phpmailer->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$phpmailer->WordWrap   = 50; // set word wrap
$phpmailer->MsgHTML($_POST[your_message]);
$phpmailer->AddAddress('support#wordpressapi.com/files/', 'Wordpress support');
//$phpmailer->AddAttachment("images/phpmailer.gif");             // attachment
if(!$phpmailer->Send()) {
 echo "Mailer Error: " . $phpmailer->ErrorInfo;
} else {
 echo "Message sent!";
}
$to = $_REQUEST['to'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
$from = $_REQUEST['from'];
$headers = "From:" . $from;
$mail = mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
What am I doing wrong? I am getting the following error:
Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\wp-vtr\wp-content\themes\twentyeleven\content.php on line 8
$phpmailer->IsSMTP(); // telling the class to use SMTP"
This:
$phpmailer->FromName = $_POST[your_email];
$phpmailer->Subject = $_POST[your_subject];
$phpmailer->Body = $_POST[your_message];
$phpmailer->MsgHTML($_POST[your_message]);
should be this:
$phpmailer->FromName = $_POST['your_email'];
$phpmailer->Subject = $_POST['your_subject'];
$phpmailer->Body = $_POST['your_message'];
$phpmailer->MsgHTML($_POST['your_message']);
Anyway, it seems you are trying to send an e-mail both via PHPMailer class and mail() native PHP function. You may be just testing but I am not really sure what are you trying to do.

Categories