Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD Imagens into body of message #2332

Closed
carcleo opened this issue May 5, 2021 · 37 comments
Closed

ADD Imagens into body of message #2332

carcleo opened this issue May 5, 2021 · 37 comments

Comments

@carcleo
Copy link

carcleo commented May 5, 2021

How ADD Imagens into body of message whay that all navigators can see it?

For example: Gmail, Bol, etc...

Way above its not works to Gmail, but it works to BOL fine

@XL-2000
Copy link

XL-2000 commented May 5, 2021

please post your code and debug output

Did you check the Wiki, examples, docs, previous issues etc???

Also, a hint, the documententation on the "msgHTML" function in PHPMailer.php could prove very useful

@Synchro
Copy link
Member

Synchro commented May 5, 2021

There is no way to guarantee that someone can see your images. All major email clients block linked images by default, and embedded images are very inefficient, and are more likely to be blocked due to content or size restrictions. Your best bet is to keep your layouts minimal and make sure they are meaningful and legible without images, and provide a link to a web version that doesn't suffer the limitations of email.

@carcleo
Copy link
Author

carcleo commented May 5, 2021

My CODE:

<?php
  
  namespace src\support;

  use PHPMailer\PHPMailer\PHPMailer;

  class SendMail {
      
    public function __construct() {}
        
    public function sender($email)  {

      $sendToName     = $email["name"];
      $sendToEmail    = $email["email"];
       
        
      // DADOS FICTÍCIOS
      $serverName     = 'Titulo do site';
      $serverEmail    = '[email protected]';
      $serverPassword = 'senha';
      
      $senderName     = $serverName;
      $senderEmail    = $serverEmail;
      $sunject        = contactsSubjects($email["subject"]);
      $what           = $email["what"];
      $message        = $email["message"];

      $messageConcatenated = 
      "<img style='width: 300px' src='" . assets("imgs/logo.png") . "'><br />
       <h1>Formulário gerado via website</h1>
       <h2>Por favor, não responda esse e-mail!</h2>
       Sua mensagem com o assunto: <b>" . $sunject . "</b/><br />
       E mensagem como abaixo<br />
       <b>" . $message . "</b><br />
       Foi recebida!<br /><br />
       <h2>Por favor, aguarde nossa resposta e obrigado por falar conosco!</h2>";
            
                

      $mail = new PHPMailer();
       
      $mail->IsSMTP();
      $mail->IsHTML(true);
      $mail->SMTPAuth  = true;
      $mail->CharSet   = 'UTF-8';
      $mail->Host      = 'smtp.'.substr(strstr($serverEmail, '@'), 1);
      $mail->Port      = '587';
      $mail->Username  = $serverEmail;
      $mail->Password  = $serverPassword;
      $mail->From      = $senderEmail;
      $mail->FromName  = utf8_decode($senderName);
      $mail->Subject   = utf8_decode($sunject);
      $mail->Body      = $messageConcatenated; 
      $mail->AddAddress($sendToEmail,utf8_decode($sendToName));

      return 
            $mail->Send()
                    ? array("success"=>1,"errors"=>"0K")
                    : array("success"=>0,"errors"=>$mail->ErrorInfo); 
      
    }   
      
  }

?>

@carcleo
Copy link
Author

carcleo commented May 5, 2021

the output, in the Gmail, its a link altered this value. But, in the BOL, it works fine

@2624292255
Copy link

Excuse me, why can't I send attachments because I don't have addattachment

@Synchro
Copy link
Member

Synchro commented Jun 8, 2021

@2624292255 I don't understand your question, and it doesn't seem to have anything to do with this thread.

@Synchro Synchro closed this as completed Jun 8, 2021
@2624292255
Copy link

@2624292255我不明白你的问题,它似乎与这个线程没有任何关系。

Unable to transfer attachment

@Synchro
Copy link
Member

Synchro commented Jun 8, 2021

That's not a PHPMailer error message, and I can't tell you anything about it without code and debug output.

@2624292255
Copy link

这不是 PHPMailer 错误消息,如果没有代码和调试输出,我无法告诉您任何有关它的信息。

Class file has no function to send attachments

@2624292255
Copy link

这不是 PHPMailer 错误消息,如果没有代码和调试输出,我无法告诉您任何有关它的信息。

class文件没有发送附件的功能

Where is the attachments function?

@Synchro
Copy link
Member

Synchro commented Jun 8, 2021

Right here. If you don't have that, you need to get a fresh copy of PHPMailer. Like I said, if you don't post your code or error output, I can't help.

@2624292255
Copy link

就在这里。如果没有,则需要获取 PHPMailer 的新副本。就像我说的,如果你不发布你的代码或错误输出,我无能为力。

微信图片_20210608144435

@Synchro
Copy link
Member

Synchro commented Jun 8, 2021

You're not checking the return value from the calls to addAttachment, so you can't tell if they are working or not. Don't use single-line constructs unnecessarily, it just makes your code harder to read and test. Try this:

if ($attachment) { // 添加附件
    if (is_string($attachment)) {
        if (is_file($attachment) && !$mail->addAttachment($attachment)) {
            echo 'File attachment failed.';
        }
    } elseif (is_array($attachment)) {
        foreach ($attachment as $file) {
            if (is_file($file) && !$mail->addAttachment($file)) {
                echo 'File attachment failed.';
            }
        }
    }
}

You also made a simple mistake – you used $this->addAttachment instead of $mail->addAttachment. This is why it looked like there was no addAttachment method – you were looking at the wrong object.

Please don't post code as images; learn to use markdown fences.

Also, since you're calling msgHtml, you don't need to call isHtml. From the style of your code, it looks like you have based it on an example from a very old version of PHPMailer, so make sure you're using the latest version.

@2624292255
Copy link

您没有检查对 的调用的返回值addAttachment,因此您无法判断它们是否正常工作。不要不必要地使用单行结构,它只会使您的代码更难阅读和测试。试试这个:

if ( $ attachment ) { // 添加附件
    if ( is_string ( $ attachment )) {
         if ( is_file ( $ attachment ) && ! $ this -> AddAttachment ( $ attachment )) {
             echo  '文件附件失败。' ;
        }
    } elseif ( is_array ( $ attachment )) {
         foreach ( $ attachment  as  $ file ) {
             if ( is_file ( $ file ) && ! $ this -> AddAttachment ( $ file )) {
                 echo  '文件附件失败。' ;
            }
        }
    }
}

请不要将代码发布为图片;学习使用降价围栏。

此外,由于您正在调用msgHtml,因此您无需调用`isHtml'。从您的代码风格来看,您似乎是基于一个非常旧版本的 PHPMailer 的示例,因此请确保您使用的是最新版本。
Thank you very much,Maybe I found the reason,Object operation error,

@2624292255
Copy link

您检查一下对的调用的返回值addAttachment,因此您无法判断它们是否正常工作。不要使用本地使用单行结构,它可以使您的代码更难阅读和测试。

if ( $ attachment ) { // 添加附件
    if ( is_string ( $ attachment )) {
         if ( is_file ( $ attachment ) && ! $ this -> AddAttachment ( $ attachment )) {
             echo  '文件附件失败。' ;
        }
    } elseif ( is_array ( $ attachment )) {
         foreach ( $ attachment  as  $ file ) {
             if ( is_file ( $ file ) && ! $ this -> AddAttachment ( $ file )) {
                 echo  '文件附件失败。' ;
            }
        }
    }
}

请不要将代码发布为图片;学习使用降价围栏。
此外,由于您正在调用msgHtml,因此您不需要调用`isHtml'。从您的代码风格来看,您似乎是基于一个非常旧版本的 PHPMailer 的示例,因此请确保您使用的是最新版本。
非常感谢,也许我找到了原因,对象操作错误,

if ( $ attachment ) { // 添加附件
if ( is_string ( $ attachment )) {
if ( is_file ( $ attachment ) && ! $ this -> AddAttachment ( $ attachment )) {
echo '文件附件失败。' ;
}
} elseif ( is_array ( $ attachment )) {
foreach ( $ attachment as $ file ) {
if ( is_file ( $ file ) && ! $ this -> AddAttachment ( $ file )) {
echo '文件附件失败。' ;
}
}
}
}
dump($mail->AddAttachment($attachment)) == ‘false’;

@Synchro
Copy link
Member

Synchro commented Jun 8, 2021

See what I said about $this vs $mail above; you're still looking at the wrong object.

@2624292255
Copy link

看看我上面说的$thisvs $mail;你仍然看错了对象。

I've changed everything to $mail
Still not

@Synchro
Copy link
Member

Synchro commented Jun 8, 2021

So check paths, permissions, and ownership of the files. If it's saying it can't read the files, it's because it can't read the files!

@2624292255
Copy link

因此,请检查文件的路径、权限和所有权。如果说它无法读取文件,那是因为它无法读取文件!

Can send mail, but you can't receive attachments
There is no problem with permissions

@Synchro
Copy link
Member

Synchro commented Jun 8, 2021

Show debug output with SMTPDebug = 2; you will be able to see the attachments if they are being sent.

@2624292255
Copy link

使用SMTPDebug = 2;显示调试输出 如果附件正在发送,您将能够看到它们。

微信图片_20210608153812

@2624292255
Copy link

使用SMTPDebug = 2;显示调试输出如果附件正在发送,您将能够看到它们。

微信图片_20210608153812

微信图片_20210608154018

@Synchro
Copy link
Member

Synchro commented Jun 8, 2021

Well, yes, that's exactly as documented. PHPMailer is (deliberately) not an HTTP client, and addAttachment will not accept URLs. If you want to send a remote file, fetch it first, then give it to PHPMailer, for example with:

$mail->addStringAttachment('file.txt', file_get_contents($url));

Fetching the file is your responsibility, not PHPMailer's.

@2624292255
Copy link

Well, yes, that's exactly as documented. PHPMailer is (deliberately) not an HTTP client, and addAttachment will not accept URLs. If you want to send a remote file, fetch it first, then give it to PHPMailer, for example with:

$mail->addStringAttachment('file.txt', file_get_contents($url));

Fetching the file is your responsibility, not PHPMailer's.

Still no, I'm stupid
微信图片_20210608161656
微信图片_20210608161700

@Synchro
Copy link
Member

Synchro commented Jun 8, 2021

That output is not telling you much because it's telling you whether addStringAttachment works, which won't work if the file_get_contents fails – but you can't tell which is failing because you're looking at the wrong thing. Test these things separately. Also, using dump() on booleans isn't easy to read; use var_dump instead.

@2624292255
Copy link

该输出并没有告诉您太多信息,因为它告诉您是否addStringAttachment有效,如果file_get_contents失败则无效 - 但您无法判断哪个失败,因为您看错了。分别测试这些东西。此外,dump()在布尔值上使用不容易阅读;使用var_dump来代替。

Using var_dump() doesn't change。
微信图片_20210608164541

@Synchro
Copy link
Member

Synchro commented Jun 8, 2021

That wasn't my point. Use var_dump on each variable individually – you are concatenating them first which means you can't see their values, because false . 'string' looks the same as 'string'. Do this instead:

$data = file_get_contents($url);
$att = $mail->addStringAttachment('file.txt', $data);
var_dump($data, $att);

@Synchro
Copy link
Member

Synchro commented Jun 8, 2021

Oops, just realised I have the arguments in the wrong order. It should be:

$att = $mail->addStringAttachment($data, 'file.txt');

@2624292255
Copy link

糟糕,刚刚意识到我的参数顺序错误。它应该是:

$att = $mail->addStringAttachment($data, 'file.txt');

微信图片_20210608165656

@Synchro
Copy link
Member

Synchro commented Jun 8, 2021

That says you are getting null returned from addStringAttachment, which should only ever return a bool. Are you using the latest version of PHPMailer? It might be useful to check what is in $attachment too:

var_dump($attachment, $data, $att);

@2624292255
Copy link

这表示您正在从null返回addStringAttachment,它应该只返回 a bool。您使用的是最新版本的 PHPMailer 吗?检查内容也可能很有用$attachment

var_dump($attachment, $data, $att);

6.0.1版本
微信图片_20210608170907

@Synchro
Copy link
Member

Synchro commented Jun 8, 2021

You should be running 6.4.1. Always run latest versions, of everything.

When I run this code:

$attachment = 'https://www.ikaiyin.com/email/123.txt';
$data = file_get_contents($attachment);
$att = $mail->addStringAttachment($data, 'file.txt');
var_dump($attachment, $data, $att);

I get:

string(37) "https://www.ikaiyin.com/email/123.txt"
string(18) "我是文件内容"
bool(true)

@2624292255
Copy link

您应该运行 6.4.1。始终运行最新版本的所有内容。

当我运行此代码时:

$附件= 'https://www.ikaiyin.com/email/123.txt' ;
$ data = file_get_contents ( $ attachment );
$ att = $ mail -> addStringAttachment ( $ data , 'file.txt' );
var_dump ( $ attachment , $ data , $ att );

我得到:

string(37) "https://www.ikaiyin.com/email/123.txt"
string(18) "我是文件内容"
bool(true)

oh my god。。。。Can I have a copy of the link?

@Synchro
Copy link
Member

Synchro commented Jun 8, 2021

To PHPMailer? You're already on the site!

@2624292255
Copy link

到PHPMailer?您已经在网站上

This is not the latest one

@Synchro
Copy link
Member

Synchro commented Jun 8, 2021

Yes it is

@2624292255
Copy link

是的

OKOKOKOK、THANK YOU VERY MUCH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants