I'm trying to create a mailing system for our library. People receive an e-mail when they requested a book. However, I want them to receive an e-mail one day before the hand-in date as well.
So I'm not looking for a sleep script code, I'm looking for a code that can maybe store the data on the server until the hand-in date arrived.
I'm using Joomla 3.5.1 for our website, this is the code for the PHPMailer:
jimport('joomla.mail.mail');
try{
$mail = new PHPMailer();
$body = $mailmsgadmin;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "example.host.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "example.host.com"; // sets the SMTP server
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "email"; // SMTP account username
$mail->Password = "password"; // SMTP account password
$mail->SetFrom('email', 'First Last');
$mail->AddReplyTo($mailuseremail, $mailusername);
$mail->Subject = $mailsubject;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($body);
$mail->AddAddress("Users e-mail","Users name");
if(!$mail->Send()) {
JError::raiseWarning( 'SOME_ERROR_CODE', $mail->ErrorInfo);
}
} catch (Exception $e) {JError::raiseWarning( 'SOME_ERROR_CODE', $e->getMessage());}
Instead of trying to get PHPMailer delay the sending, add your email details to a database table. And then write a cron job that checks the table for emails that need to be sent.
Related
I need to connect to the GMAIL SMTP server with email and password, without sending email and then receiving 200 OK.
I using the code PHPMailer:
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "myuser#gmail.com";
$mail->Password = "password";
$mail->SetFrom("");
$mail->Subject = utf8_decode("My subject");
$mail->Body = "hello";
$mail->AddAddress("destination#gmail.com");
$mail->Body = template('emails/reset-password', compact('comentario'));
if ($mail->Send()) {
$mail->ClearAllRecipients();
$mail->ClearAttachments();
}
My script is sending email, I would just like to validate the authentication.
PHPMailer itself isn't much use for this as it's all about the sending. However, the SMTP class that's part of PHPMailer can do this just fine, and there is a script provided with PHPMailer that does exactly as you ask. I'm not going to reproduce the script here as it just creates maintenance for me!
The script connects to an SMTP server, starts TLS, and authenticates, and that's all.
I have the following code sending email shipping confirmations to my customers:
while ($i < count($tracked) ) {
try {
//Server settings
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.office365.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'support#mycompany.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('support#mycompany.com', 'mycompany');
$mail->addAddress($tracked[$i]['customers_email_address'], $tracked[$i]['customers_name']); // Add a recipient
$mail->addReplyTo('support#mycompany.com', 'mycompany');
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = EMAIL_TEXT_SUBJECT;
$mail->Body = $html;
$mail->AltBody = 'Your order with mycompany.com has shipped';
$mail->send();
echo 'order confirmation sent to order#:'.$tracked[$i]['orders_id'].'<br/>';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
$i++;
}
It appears to be sending to multiple emails. What I believe is happening is that in the while loop each new customers address is added to the list without clearing it. What doesnt seem to be happening though is the message does not seem to be duplicated? Wouldn't that duplicate at as well with each pass in the loop?
In any case I think the best thing to do before the try is:
$mail = new PHPMailer(true);
so that with each pass of the while loop the $mail object would be instantiated again. Is that proper? I know that the clearAllRecipients() function exists but i also want to be sure the body is cleaned as well.
it looks like your looping through an array of $tracked. Why not use a foreach loop? Then you don't need to use a counter.
foreach ($tracked as $track ) {
try {
//Server settings
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.office365.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'support#mycompany.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('support#mycompany.com', 'mycompany');
$mail->addAddress($track['customers_email_address'], $track['customers_name']); // Add a recipient
$mail->addReplyTo('support#mycompany.com', 'mycompany');
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = EMAIL_TEXT_SUBJECT;
$mail->Body = $html;
$mail->AltBody = 'Your order with mycompany.com has shipped';
$mail->send();
echo 'order confirmation sent to order#:'.$track['orders_id'].'<br/>';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
It's because addAddress() does exactly what its name suggests; it adds an address that a message will be sent to. It doesn't set the addresses a message will be sent to.
Unsurprisingly there is a method that allows you to clear the list of addresses, called clearAddresses(), so just call that at the end of your sending loop after send() and your issue will be solved.
I'd also recommend that you base your code on the mailing list example provided with PHPMailer as it will be much faster than the code you have here. Also read the docs on sending to listshttps://github.com/PHPMailer/PHPMailer/wiki/Sending-to-lists) for furtehr advice.
I have been developing a page that will automatically send emails from a Gmail account to mail accounts of various origin for specific users of my site. I have this working perfectly on Localhost. The mails are received by the users and also deposited in the "Sent Items" of the Gmail account.
However, as soon as I migrate this to the server we are using it no longer works. The code is as follows :-
$mail = new PHPMailer();
try
{
$mail->CharSet = "UTF-8";
// telling the class to use SMTP
$mail->IsSMTP();
// enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPDebug = 0;
// enable SMTP authentication
$mail->SMTPAuth = true;
// sets the prefix to the servier
$mail->SMTPSecure = "tls";
// sets GMAIL as the SMTP server
$mail->Host = "smtp.gmail.com";
// set the SMTP port for the GMAIL server
$mail->Port = 587;
// GMAIL username
$mail->Username = "xxxxxxx#gmail.com";
// GMAIL password
$mail->Password = "P4ssw0rd";
//Set reply-to email this is your own email, not the gmail account
//used for sending emails
$mail->SetFrom($from);
$mail->FromName = "XXX";
$mail->AddReplyTo('xxxxxxx#gmail.com' , 'XXX');
// Mail Subject
$mail->Subject = $subject;
//Main message
$mail->MsgHTML($message);
$mail->AddAddress($to, "");
$mail->Send();
}
catch (phpmailerException $e)
{
echo $e->errorMessage(); //Pretty error messages from PHPMailer
}
catch (Exception $e)
{
echo $e->getMessage(); //Boring error messages from anything else!
}
Setting the Debug to 1 on localhost produces the expected errors if something goes wrong. However, nothing seems to happen on the Server. Please help.
I'm trying to send a message from a script on a site, using SMTP. I can successfully read the mail on my personal email, although my work email does not receive it. I think it might be bouncing.
In the Google admin panel, I have added test.domain.com as an alias for domain.com.
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->SMTPDebug = 2;
$mail->From = "smtp#test.domain.co.uk";
$mail->FromName = "No-Reply # Domain";
$to = 'harry#test.domain.co.uk, personalEmail#gmail.com';
$mail->AddAddress('harry#test.domain.co.uk');
$mail->AddAddress('personalEmail#gmail.com');
$mail->AddReplyTo($mail->From, $mail->FromName);
$mail->WordWrap = 80; // set word wrap to 50 characters
$mail->IsHTML(false); // set email format to HTML
$mail->Subject = $subject[PAGE];
$mail->Body = $body;
if ( $mail->Send() )
{
header('location: '.$_SERVER['REQUEST_URI'].'?sent1');
exit;
}
else
{
$errors[] = 'Sorry, your message could not be sent, '.$mail->ErrorInfo;
}
I have created an MX record for test.domain.com as well as an SMTP relay service in Google Admin, as well as allowing per-user outbound gateways (Allow users to send mail through an external SMTP server when configuring a "from" address hosted outside your email domains).
When we set up PHP mailer we need to add the credentials of SMTP mail Server in it.In you code example you skipped those lines and it is ncessary to send an email
mail->Host = "smtp.yourmail.com"; // Your SMTP server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = 'tls'; //SMTP secure type like ssl/tls
$mail->Username = "dont.reply.m#yourmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$mail->From = "dont.reply.m#yourmail.com"; // SMTP username
Please try with proper credentials on the top after initialisation of object $mail
The following code gives the message
Mailer Error: SMTP Error: The following SMTP Error: Data not accepted. But when I replace $EmailAdd with a a#yahoo.com. The mail was sent.
What's wrong with my code? I'm kind of new in php, especially in dealing with mail functions.
$sql1 = "SELECT Email_Address FROM participantable where IDno=$studId";
$result1 = mysql_query($sql1);
while ($row1 = mysql_fetch_assoc($result1)){
$EmailADD = $row1["Email_Address"];
}
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
include("class.phpmailer.php");
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = $mail->getFile('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "jsbadiola#gmail.com"; // GMAIL username
$mail->Password = "********"; // GMAIL password
$mail->AddReplyTo("jsbadiola#gmail.com","Lord Skyhawk");
$mail->From = "jsbadiola#gmail.com";
$mail->FromName = "Update";
$mail->Subject = "PHPMailer Test Subject via gmail";
$mail->Body = "Hi,<br>This is the HTML BODY<br>"; //HTML Body
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 50; // set word wrap
$mail->MsgHTML($body);
$mail->AddAddress($EmailADD, "Agta ka");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->IsHTML(true); // send as HTML
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
$status = "Successfully Save!";
header("location: User_retsched.php?IDno=$studId&status=$status&Lname=$Lname&Fname=$Fname&Course=$Course&Year=$Year");
}
Most times I've seen this message the email gets successfully sent anyway, but not always. To debug, set:
$mail->SMTPDebug = true;
You can either echo the debug messages or log them using error_log():
// 'echo' or 'error_log'
$mail->Debugoutput = 'echo';
A likely candidate especially on a heavily loaded server are the SMTP timeouts:
// default is 10
$mail->Timeout = 60;
class.smtp.php also has a Timelimit property used for reads from the server.
It also helps if you have not exceeded your daily sending limit of Googles Spam inforcement.
check you query to the db output & variable value at that point in the script. make sure it's returning the desired value for $EmailADD .
do a var_dump($EmailADD, true);
or try to echo somewhere the output of that query. If your actually receiving an email value from that query, I don't see why it shouldn't work, specially when you mention that assigning a value directly works; without sql query.
Try a different SMTP server? See if that works?
Or just don't use an smtp server, most servers with PHP on also have sendmail/postfix, so can relay email themselves.
remove this bit ...
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "jsbadiola#gmail.com"; // GMAIL username
$mail->Password = "alucar"; // GMAIL password