Asked  1 Year ago    Answers:  5   Viewed   8 times

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();
}
?>

 Answers

1

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();
}
?>
Saturday, May 29, 2021
 
Student
 
3

To paraphrase the guide, download the CA bundle from curl and store it somewhere on your file system. Take the path you saved it to and add a line to your php.ini file saying:

openssl.cafile = $path

Where $path is where you saved the CA certs to. Then restart your web server to pick up the ini change.

If that worked, you should see that setting in the output from phpinfo(), and it should also give PHP what it needs to validate the certificate when PHPMailer uses it. Be aware that if the server is presenting a truly invalid or expired certificate, this won’t help, but given that this a well-known problem and solution, I expect it to work.

Thursday, April 1, 2021
 
Sufi
 
2

You wrote:

Instead of actually sending the email, it just seems to print the above [code] to screen of the user as if it's a webpage. it's as if the server thinks it's a html page and is just serving it.

I think your problem is unrelated to phpMailer, or indeed email at all.

This is almost certain to be simply a small difference between the PHP configuration on your old and new servers.

Your PHP code is using the short-style PHP tags -- ie starting with <?.

Many PHP servers are configured not to allow this short-style tag, and only run PHP code that starts with the full <?php tag.

Given what you stated in the remark I quoted above, I suspect this is your problem, because what you describe is exactly the symptom I would expect from that.

You have two options:

  1. Change your code to use the long style <?php tag.

  2. Change your server config to allow the short <? tag.

I would recommend taking option (1) if possible, because the long-form tags are considered best practice (the short form is not recommended because it can have a potential clash with the <?xml marker at the start of most XML documents).

If you have a lot of short-form <? tags (and assuming you're allowed to do so), changing the server config may be worth considering, but if it's easy enough to fix the problem by switching to long tags, I'd say that would be better.

Hope that helps.

Thursday, April 1, 2021
4

Try:

if (isset($_FILES['uploaded_file']) &&
    $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) {
    $mail->AddAttachment($_FILES['uploaded_file']['tmp_name'],
                         $_FILES['uploaded_file']['name']);
}

Basic example can also be found here.

The function definition for AddAttachment is:

public function AddAttachment($path,
                              $name = '',
                              $encoding = 'base64',
                              $type = 'application/octet-stream')
Tuesday, June 1, 2021
 
5

The problem with this

for line in open('data.txt'):
   # etc

Is that you don't keep an explicit reference to the open file, so how do you close it? The lazy way is wait for the garbage collector to clean it up, but that may mean that the resources aren't freed in a timely manner.

So you can say

inf = open('data.txt')
for line in inf:
   # etc
inf.close()

Now what happens if there is an exception while you are inside the for loop? The file won't get closed explicitly.

Add a try/finally

inf = open('data.txt')
try:
    for line in inf:
       # etc
finally:
    inf.close()

This is a lot of code to do something pretty simple, so Python added with to enable this code to be written in a more readable way. Which gets us to here

with open('data.txt') as inf:
    for line in inf:
       #etc

So, that is the preferred way to open the file. If your Python is too old for the with statement, you should use the try/finally version for production code

Friday, October 29, 2021
 
Statsar
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :