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";
}
?>
Related
http://nodeiterator.pl/
Why my php mailer script is not sending mail when message field is empty but is not required ? I get the message "please try again later". What am I missing ?
This is my script:
$msg = "";
use PHPMailer\PHPMailer\PHPMailer;
include_once "phpmailer/src/PHPMailer.php";
include_once "phpmailer/src/Exception.php";
if (isset($_POST['submit'])) {
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
if (isset($_FILES['attachment']['name']) && $_FILES['attachment']['name'] != "") {
$file = "attachment/" . basename($_FILES['attachment']['name']);
move_uploaded_file($_FILES['attachment']['tmp_name'], $file);
} else
$file = "";
$mail = new PHPMailer();
$mail->addAddress('piterdeja#gmail.com');
$mail->setFrom($email);
$mail->Subject = $subject;
$mail->isHTML(true);
$mail->Body = $message;
$mail->addAttachment($file);
if ($mail->send())
$msg = "Your email has been sent, thank you!";
else
$msg = "Please try again!";
}
I don't think PHPMail by default will let you send an email with an empty body, but you can just go:
$mail->AllowEmpty = true;
Check the error returned:
if(!$mail->Send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
Normally phpmailer does not accept an empty body, you must force it using the attribute $mailer->AllowEmpty = true;
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.
i am trying to send bulk email using phpmailer i have more than 10 email id's in my database when i click on send button then first email will go to one person, the second email sent will go to that same person plus another, the third one will go to those two plus one more, and so on. this is my coding please help me
<?php
$body=$_POST['message'];
$subject=$_POST['sub'];
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
require_once("class.phpmailer.php");
//include("class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "stmp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->CharSet = "big5";
$mail->Username = "abc#gmail.com"; // GMAIL username
$mail->Password = "**********"; // GMAIL password
$mail->SetFrom("abc#gmail.com", ''); // set reply id
$mail->Subject = ($subject); // subject
$mail->MsgHTML("$body"); // message
$mail->AddAddress($address, "abc");
$con=mysql_connect("localhost","root","") or
die("could not connect:".mysql_error());
mysql_select_db("bulkemail");
$qry=mysql_query("SELECT * FROM email_id", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
while($row = mysql_fetch_array($qry))
{
$id= $row["email"];
$address = ($id);
$mail->AddBcc($id);
$mail->send();
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message sent!";
}
}
?>
Remove $mail->send(); and move if(!$mail->Send()) to outside the while($row ..) loop
while($row = mysql_fetch_array($qry)){
$id= $row["email"];
$address = ($id);
$mail->AddBcc($id);
} // end the while loop
// remove $mail->send(); as it is a duplicate of if(!$mail->Send())
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message sent!";
}
I had used phpmailer for mass mailing and I had same problem.
while($row = mysql_fetch_array($qry)){
$id= $row["email"];
$address = ($id);
$mail->AddBcc($id);
//imo if should be in the while loop.
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message sent!";
//I added this function down there so that old address would be removed but in the new loop, new one will be added.
$mail-> ClearAddresses();
}
} // end the while loop
This works for me.
In your case, you are adding a new BCC to the list of all BCC already send in every repeat of the loop.
Like "kworr Sep 27 '13 at 18:36 " said, you need to create new instance for every sending in the loop.
Or you need to put
$mail->send();
out of the loop.
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.
function register_contact ($person = array()) {
$nogood = false;
foreach ($person as $val) {
if (strlen($val)==0) {
$nogood = true;
$status = "There was an error sending the registration please fill in all fields";
}
}
if (!$nogood) {
require_once("class.phpmailer.php");
$message = "New request for Fox In Touch Recipient:.\r\n\r\n";
$message .= "Forename: " . $person['fname'];
$message .= "\r\nSurname: " . $person['sname'];
$message .= "\r\nEmail: " . $person['email'];
$message .= "\r\nJob Title: " . $person['job'];
$message .= "\r\nCompany: " . $person['company'];
$message .= "\r\n\r\nFox In Touch.";
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "ahost"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "name"; // SMTP username
$mail->Password = "pass"; // SMTP password
//$mail->Post = 587;
$mail->From = "foxintouch#bionic-comms.co.uk";
$mail->FromName = "Fox In Touch";
//$mail->AddAddress("foxlicensing.europe#fox.com", "Fox Licensing");
$mail->AddAddress("andrew#yahoo.co.uk", "Andrew");
$mail->AddReplyTo("foxintouch#bionic-comms.co.uk","Information");
$mail->IsHTML(false); // send as HTML
$mail->Subject = "Contact request for Fox In Touch!";
$mail->Body = $message;
if(!$mail->Send()) {
$nogood = true;
$status = "Message was not sent <p>";
$status .= "Mailer Error: " . $mail->ErrorInfo;
} else {
$status = "Thank you! Your message has been sent to 20th Century Fox. Submit another?";
}
}
return array('email_failed'=>$nogood, 'status'=>$status);
}
The above code keeps giving me the error, "Mailer Error: Language string failed to load: recipients_failedandrew#yahoo.co.uk". I have tried changing the AddAddress(). The smtp connection settings are correct, as this was the last error i had! Any help would be much appreciated. Thanks
It sounds like you have two problems.
1) Your language file isn't being loaded - see installation
2) The recipient is being rejected - errr double check the SMTP settings and the recipient address