PHPMailer and 100K file limit for attachments? - php

I'm trying to send a file as an email attachment, but for some reason if the file is > 100k then the email doesn't go through, even though I get the email sent message.
It may also be a limit on the attachments in IIS smtp setting, but when I unchecked the Limit session size and limit message size options, it didn't change anything. I may have to restart the server tonight...
I don't know if it's a php.ini setting, or what.
<?
$path_of_attached_file = "Images/marsbow_pacholka_big.jpg";
require 'include/PHPMailer_5.2.1/class.phpmailer.php';
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$body = $message; //"<p><b>Test</b> another test 3.</p>";
$mail->AddReplyTo("admin#example.com","Admin");
$mail->From = "admin#example.com";
$mail->FromName = "Admin";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
if($attach){
$mail->AddAttachment($path_of_attached_file);
}
$mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>

I might be wrong because I don't use IIS but the code you provided would actually use a native MTA not SMTP. As far as I know you have to use the IsSMTP() method to let PHPMailer know that you intend to use SMTP.
Something like this:
<?
$path_of_attached_file = "Images/marsbow_pacholka_big.jpg";
require 'include/PHPMailer_5.2.1/class.phpmailer.php';
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$body = $message; //"<p><b>Test</b> another test 3.</p>";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 25; // set the SMTP port
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->AddReplyTo("admin#example.com","Admin");
$mail->From = "admin#example.com";
$mail->FromName = "Admin";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
if($attach){
$mail->AddAttachment($path_of_attached_file);
}
$mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>

Your code is not actually checking if the message was sent or not.
You need to change your code to check the return of the send method
if ($mail->Send())
echo 'Message has been sent.';
else
echo 'Sorry there was an error '.$mail->ErrorInfo;
This should give you the error message saying what is up if it does go wrong.

Related

check smtp connection is working or not using PHP Mailer and get status receipt OK

I want to check whether the SMTP Server is working or not by sending mail or pinging server and get response Receipt OK.
i want to check whether my smtp is down or up using php . I tried to test it using below PHPmailer code. Which is the best method to test whether smtp running using php script.
/**
* Simple example script using PHPMailer with exceptions enabled
* #package phpmailer
* #version $Id$
*/
require ('mailer/class.phpmailer.php');
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$body = "Please return read receipt to me.";
$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 465; // set the SMTP server port
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Username = "bhakuq#gmail.com"; // SMTP server username
$mail->Password = "bhakuxxx"; // SMTP server password
$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("bhakuq#gmail.com","Riya Patel");
$mail->From = "bhakuq#gmail.com";
$mail->FromName = "Riya Patel";
$to = "bhakuq#gmail.com";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message[Test Read Receipt]";
$mail->ConfirmReadingTo = "someone#something.com"; //this is the command to request for read receipt. The read receipt email will send to the email address.
$mail->AltBody = "Please return read receipt to me."; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
I tried above code but still no use.

PHPMailer and Gmail smtp, not getting sent or failed message

Instead of using php's mail() function, I've been trying to set up PHPMailer with no success. I put in "echo here" for debugging purposes, and that is all it shows. I do not get any emails, or the sent or error messages. I'm stumped, and after researching it on here may switch to swift mailer. I'd really like to know what I screwed up though.
In my code, address is set to my email, and the username and password are set to a dummy account I made.
<?php
include('class.phpmailer.php');
$mail = new PHPMailer();
$address = "test#gmail.com";
$body = "test email";
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "username#gmail.com";
$mail->Password = "password";
$mail->SetFrom('name#yourdomain.com', 'Web App');
$mail->Subject = "A Transactional Email From Web App";
$mail->MsgHTML($body);
$mail->AddAddress($address, $name);
echo "Here";
if($mail->Send()) {
echo "Message sent!";
}
else {
echo "Mailer Error: " ; $mail->ErrorInfo;
}
?>
As you are using Gmail I think you must make sure the gmail account has the insecure application auth activated

Setting up mail server with Zoho & making own phpMail function for it

I don't know if my code is the issue or if I had configured Zoho's SMTP's settings incorrectly.
Basically I want the ability to dynamically send emails with a simple php function like for example
phpMail("to#example.com", $subject, $body, "from#example.com", "replyto#example.com");
This is my PHPMailer.php script (it sees the function and its settings)
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.zoho.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '**REMOVED**'; // SMTP username
$mail->Password = '**REMOVED**'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->isHTML(true); // Set email format to HTML
// SYTAX: phpMail($from, $reply, $to, $subject, $body);
function phpMail($to, $subject, $body, $from = "from#example.com", $reply = "replyto#example.com") {
if (isset($from))
$mail->From = $from;
$mail->FromName = "testing";
if (isset($to))
$mail->addAddress($to);
if (isset($reply))
$mail->addReplyTo($reply);
if (isset($subject))
$mail->Subject = $subject;
if (isset($body))
$mail->Body = $body;
$mail->AltBody = $body;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
Now the issue with this is that, I'm not receiving emails nor am I receiving any errors / messages but it's also not sending me an email.
When you make a PHP function, make sure your objects aren't outside of your function.
My error wasn't that though, it was basically my own stupidity which had nothing to do with the code but more of my "form post data".
The following configuration works for me using zoho mail.
Give it a try:
$mail=new JPhpMailer;
$mail->IsSMTP();
$mail->Host='smtp.zoho.com';
// Enable this option to see deep debug info
// $mail->SMTPDebug = 4;
$mail->SMTPSecure = 'ssl';
$mail->Port='465';
$mail->SMTPAuth=true;
$mail->Username='your_email_address';
$mail->Password='your_eamil_address_password';
$mail->isHTML(true);
$mail->SetFrom('your_email_address','Your Name');
$mail->Subject='PHPMailer Test Subject via smtp, basic with authentication';
$mail->AltBody='To view the message, please use an HTML compatible email viewer!';
$mail->MsgHTML('<h1>JUST A TEST!</h1>');
$mail->AddAddress('destination_email_address','John Doe');
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}

PHPMailer, no Email and no Error

I want to send a large mail from my webspace. This mail contains many variables, which are inputed through a form.
First I tried the php mail(), but I didn't receive any mail. I used mail() in another part of my website and from this script I receive a mail. So the function should work on my webspace.
For the large mail I tried to use PHPMailer, because the mail() didn't work as expected. The code for the PHPMailer is:
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'xxx.xxx.de';
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->Username = 'xxx';
$mail->Password = 'xxx';
$mail->SMTPSecure = 'tls';
$mail->From = 'xxx#xxx.de';
$mail->FromName = 'xxx';
$mail->addAddress('xxx#xxx.de', 'xxx xxx');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $mess;
$mail->AltBody = $mess;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
After the script has finished, I don't get an error and I don't receive a mail. What could be a problem for this? How can I check, why my PHPMailer is not working? Has the require path to be absolute or relative?
Try this way:
//... another option code
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
//...another your code
dump($mail->send());
Each time ,you open this php-page,the mail result will let you know.what are you missed.
use try/catch as follow :
$mail = new PHPMailer(true); // enable exceptions
try {
$mail->isSMTP();
//....
}
catch (phpmailerException $e) {
echo $e->errorMessage();
}

Sending 2 emails with PHP mailer fails

I am rather puzzled with this one.
//SMTP servers details
$mail->IsSMTP();
$mail->Host = "mail.hostserver.com";
$mail->SMTPAuth = false;
$mail->Username = $myEmail; // SMTP usr
$mail->Password = "****"; // SMTP pass
$mail->SMTPKeepAlive = true;
$mail->From = $patrickEmail;
$mail->FromName = "***";
$mail->AddAddress($email, $firstName . " " . $lastName);
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $client_subject;
$mail->Body = $client_msg;
if($mail->Send())
{
$mail->ClearAllRecipients();
$mail->ClearReplyTos();
$mail->ClearCustomHeaders();
...
$mail->From = "DO_NOT_REPLY#...";
$mail->FromName = "****";
$mail->AddAddress($ToEmail1, "***"); //To: (recipients).
$mail->AddAddress($ToEmail2, "***"); //To: (recipients).
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $notification_subject;
$mail->Body = $notification_msg;
if($mail->Send())
{
...
The first email sends fine. The second one doesn't. What could be the reason for that behavior? Am I missing some kind of reset?
Update: using a different mail server seems to work so apparently it's a setting of that specific mail server causing problems. Any idea what that could be?
Some providers impose restrictions on the number of messages that can be sent within a specific time span. To determine if your problem depends by a provider "rate limit", you should try to add a pause after the first send. For example:
if ($mail->Send()) {
sleep(10); // Seconds
...
if ($mail->Send()) {
...
}
}
Then, by progressively lowering the sleep time, you should be able to determine which is the rate limit.
Try this:
As #Felipe Alameda A mentioned Remove $mail->SMTPKeepAlive = true;
// for every mail
if(!$mail->Send())
{
echo 'There was a problem sending this mail!';
}
else
{
echo 'Mail sent!';
}
$mail->SmtpClose();
IMHO you need to create new PHPMailer object for every sent email. If you want to share some common setup, use something like this:
$mail = new PHPMailer();
/* Configure common settings */
while ($row = mysql_fetch_array ($result)) {
$mail2 = clone $mail;
$mail2->MsgHTML("Dear ".$row["fname"].",<br>".$cbody);
$mail2->AddAddress($row["email"], $row["fname"]);
$mail2->send();
}
I think your problem is $mail->SMTPAuth = false;
It is hard to believe there are ISP or SMTP providers that don't require authentication, even if they are free.
You may try this to check for errors instead of or in addition to checking for send() true:
if ( $mail->IsError() ) { //
echo ERROR;
}
else {
echo NO ERRORS;
}
//Try adding this too, for debugging:
$mail->SMTPDebug = 2; // enables SMTP debug information
Everything else in your code looks fine. We use PHPMailer a lot and never had any problems with it
The key may lie in the parts you have omitted. Is the domain of the sender of both emails the same? Otherwise the SMTP host may see this as a relay attempt. If you have access to the SMTP server logs, check these; they might offer a clue.
Also, check what $mail->ErrorInfo says... it might tell you what the problem is.
i personally would try to make small steps like sending same email.. so just clear recipients and try to send identical email (this code works for me). If this code passes you can continue to adding back your previous lines and debug where it fails
and maybe $mail->ClearCustomHeaders(); doing problems
//SMTP servers details
$mail->IsSMTP();
$mail->Host = "mail.hostserver.com";
$mail->SMTPAuth = false;
$mail->Username = $myEmail; // SMTP usr
$mail->Password = "****"; // SMTP pass
$mail->SMTPKeepAlive = true;
$mail->From = $patrickEmail;
$mail->FromName = "***";
$mail->AddAddress($email, $firstName . " " . $lastName);
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $client_subject;
$mail->Body = $client_msg;
// all above is copied
if($mail->Send()) {
sleep(5);
$mail->ClearAllRecipients();
$mail->AddAddress('another#email.com'); //some another email
}
...
Try with the following example.,
<?php
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.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->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address1 = "whoto#otherdomain.com";
$address2 = "whoto#otherdomain.com";
$mail->AddAddress($address1, "John Doe");
$mail->AddAddress($address2, "John Peter");
$mail->AddAttachment("images/phpmailer.gif"); // attachment if any
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment if any
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Note : Better you can make a multiple user email and name as an ARRAY, like
<?php
$recipients = array(
'person1#domain.com' => 'Person One',
'person2#domain.com' => 'Person Two',
// ..
);
foreach($recipients as $email => $name)
{
$mail->AddCC($email, $name);
}
(or)
foreach($recipients as $email => $name)
{
$mail->AddAddress($email, $name);
}
?>
i think this may help you to resolve your problem.
I think you've got organizational problems here.
I recommend:
Set your settings (SMTP, user, pass)
Create new email object with info from an array holding messages and to addresses
Send email
Goto step 2

Categories