Trimiterea de mailuri

postat acum 4 ani de Stefanescu Mihai in categorie PHP

In primul rand trebuie sa setam totul corect in php.ini pentru a putea trimite mailuri. Deschide fisierul php.ini si cauta urmatoarea sectiune: [mail function]

Utilizatorii de windows trebuie sa asigure doua directive. Prima se numeste SMTP si defineste conexiunea la serverul de mail. A doua se numeste sendmail_from si defineste propria adresa de email.

Fisierul de configurare din windows ar trebuii sa arate ceva de genul:

[mail function]
; For Win32 only.
SMTP = smtp.server.net

; For win32 only
sendmail_from = [email protected]

In linux trebuie doar sa-i da-ti PHP-ului o locatie pentru aplicatia sendmail. Calea trebuie setata in directiva sendmail_path.

[mail function]
; For Win32 only.
SMTP = 

; For win32 only
sendmail_from = 

; For Unix only
sendmail_path = /usr/sbin/sendmail -t -i

Trimiterea de mailuri textPHP pune la dispozitie functia mail() pentru a trimite emailuri. Aceasta functie are nevoie de 3 parametri pentru a functiona, adresa de email la care sunt trimise mailurile, subiectul si mesajul, dar mai sunt si 2 parametri aditionali.

mail( to, subject, message, headers, parameters );

Descrierea parametrilor:

Parametru Descriere
to Obligatoriu. Specifica emailul destinatarului.
subject Obligatoriu. Specifica subiectul emailului.
message Obligatoriu. Mesajul emialului.
headers Optional. Specifica headere optionale precum Cc, Bcc, etc.
parameters Optional. Parametri optionali

Imediat ce functia mail() este apelata PHP va incerca sa trimita emailul si va returna true in caz de succes si false in caz contrar.

Pot fi specificate mai multe recipiente pe post de prim argumen, separate de virgula.

   $to = "[email protected]";
   $subject = "Subiectul";
   $message = "Mesajul.";
   $header = "From: [email protected] \r\n";
   $retval = mail ($to,$subject,$message,$header);
   if( $retval == true ) {
      echo "Mail trimis!";
   }else{
      echo "Mailul nu a putut fi trimis!";
   }

Trimiterea de emailuri HTML

Cand trimiti un mesaj folosind PHP tot continutul va fi tratat ca simplu text.  Chiar daca includeti si tag-uri HTML in acel mesaj, tot v-or fi afisate ca simplu text. Dar PHP ne pune la dispozitie o optiune ce ne permite sa trimitem mailuri cu mesaj HTML.

Cand trimitem un mail putem specifica tipul de Mime.Iata si un exemplu:

<html>
<head>
<title>Trimitere email cu HTML</title>
</head>
<body>
<?php
   $to = "[email protected]";
   $subject = "Subiectul aici";
   $message = "<b>Mesajul cu cod HTML.</b>";
   $message .= "<h1>Continuarea mesajului.</h1>";
   $header = "From:[email protected] \r\n";
   $header .= "MIME-Version: 1.0\r\n";
   $header .= "Content-type: text/html\r\n";
   $retval = mail ($to,$subject,$message,$header);
   if( $retval == true ){
      echo "Mesaj Trimis!";
   }else{
      echo "Mesaj netrimis";
   }
?>
</body>
</html>

Trimitere de emailuri cu Atasament

Pentru a trimite un email cu continut mixt trebuie sa setam header-ul Content-type la multipart/mixed. Apoi putem specifica un anumit atasament.

As putea sa va explic linie cu linie codul pe care am sa-l scriu mai jos, dar am sa va arat exemplul direct pentru ca este mai simplu sa vedeti voi cum sunt asezate toate lucrurile:

<html>
<head>
<title>Mailuri cu atasament</title>
</head>
<body>
<?php
  $to = "[email protected]";
  $subject = "Subiectul";
  $message = "Mesajul";
  # Deschidem fisierul
  $file = fopen( "/tmp/fisier.txt", "r" );
  if( $file == false )
  {
     echo "Nu pot deschide fisierul";
     exit();
  }
  # Citeste continutul si salveaza-l intr-o avriabila
  $size = filesize("/tmp/fisier.txt");
  $content = fread( $file, $size);

  # encodare informatii pentru transmitere in siguranta
  $encoded_content = chunk_split( base64_encode($content));
  
  # Obtinem un string de 32 bit folosind un md5 de time()
  $num = md5( time() );

  # Definim headere
  $header = "From:[email protected]\r\n";
  $header .= "MIME-Version: 1.0\r\n";
  $header .= "Content-Type: multipart/mixed; ";
  $header .= "boundary=$num\r\n";
  $header .= "--$num\r\n";

  # mesajul
  $header .= "Content-Type: text/plain\r\n";
  $header .= "Content-Transfer-Encoding:8bit\r\n\n";
  $header .= "$message\r\n";
  $header .= "--$num\r\n";

  # atasamentul
  $header .= "Content-Type:  multipart/mixed; ";
  $header .= "name=\"test.txt\"\r\n";
  $header .= "Content-Transfer-Encoding:base64\r\n";
  $header .= "Content-Disposition:attachment; ";
  $header .= "filename=\"test.txt\"\r\n\n";
  $header .= "$encoded_content\r\n";
  $header .= "--$num--";

  # trimitere mail
  $retval = mail ( $to, $subject, "", $header );
  if( $retval == true )   {
      echo "Mesaj trimis.";
   }else{
      echo "Mesajul nu a putut fi trimis!";
   }
?>
</body>
</html>

Avand in vedere ca aceasta este o lectie mai complicata am sa va rog sa postati orice intrebari aveti in comentarii (mai jos).

Sunt un tanar programator din Bucuresti ce lucreaza in PHP/Mysql (MySqli/PDO), Laravel, CodeIgniter, MySQL, PostgreSQL, Wordpress, HTML5/CSS3, Sass, Photoshop si multe altele.
Google+ Community Facebook Group
Acest articol a fost mutat de pe vechea platforma.
Pentru orice eroare aparuta la mutare va rog sa ma contactati!

Comentariu postate de Tekhattan la data de 13.05.2019
We're a group of volunteers and starting a new scheme in our community. Your site offered us with valuable information to work on. You have done a formidable job and our entire community will be grateful to you.
Comentariu postate de Remap tuning ecu la data de 14.05.2019
Having read this I thought it was really enlightening. I appreciate you finding the time and energy to put this short article together. I once again find myself spending way too much time both reading and commenting. But so what, it was still worth it!
Comentariu postate de Tekhattan la data de 14.05.2019
Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You obviously know what youre talking about, why throw away your intelligence on just posting videos to your weblog when you could be giving us something enlightening to read?
Comentariu postate de Remap tuning ecu la data de 14.05.2019
Thanks for finally talking about >Trimiterea de mailuri - Invata-Programare : Tutoriale gratuite de PHP, HTML5, CSS3, jQuery si multe altele - Tutoriale gratuite de PHP, HTML5, CSS, JavaScript, jQuery, Java, C++, Python si mutle aletele
Comentariu postate de Remap tuning ecu la data de 14.05.2019
Why viewers still use to read news papers when in this technological globe all is accessible on web?
Comentariu postate de Remap tuning ecu la data de 14.05.2019
Hi, its nice piece of writing regarding media print, we all be aware of media is a great source of data.
Comentariu postate de Remap tuning ecu la data de 14.05.2019
Hello mates, nice paragraph and pleasant arguments commented here, I am genuinely enjoying by these.
Comentariu postate de Gisele la data de 15.05.2019
Hello it's me, I am also visiting this web page daily, this website is really fastidious and the users are genuinely sharing fastidious thoughts.
Comentariu postate de Lonna la data de 15.05.2019
Hey there! This is my first visit to your blog! We are a collection of volunteers and starting a new initiative in a community in the same niche. Your blog provided us valuable information to work on. You have done a outstanding job!
Comentariu postate de icahf.org la data de 15.05.2019
Link exchange is nothing else but it is only placing the other person's webpage link on your page at appropriate place and other person will also do similar in support of you.
Comentariu postate de memory foam pillow kurlon la data de 15.05.2019
What's up, I desire to subscribe for this weblog to get hottest updates, thus where can i do it please help.
Comentariu postate de binary options signals la data de 16.05.2019
This post is priceless. When can I find out more?
Comentariu postate de best binary software la data de 16.05.2019
When some one searches for his required thing, thus he/she desires to be available that in detail, so that thing is maintained over here.
Comentariu postate de signal app review la data de 16.05.2019
A fascinating discussion is definitely worth comment. I do believe that you need to write more about this subject matter, it might not be a taboo subject but typically folks don't talk about these subjects. To the next! Best wishes!!
Comentariu postate de Nelson la data de 16.05.2019
I have read so many posts concerning the blogger lovers however this paragraph is really a pleasant post, keep it up.
Comentariu postate de Home Improvement Idea la data de 16.05.2019
Hello, the whole thing is going nicely here and ofcourse every one is sharing facts, that's truly excellent, keep up writing.
Comentariu postate de binary options signals la data de 17.05.2019
This post presents clear idea in support of the new visitors of blogging, that truly how to do running a blog.
Comentariu postate de Lane la data de 17.05.2019
you're really a excellent webmaster. The site loading velocity is incredible. It kind of feels that you're doing any unique trick. Also, The contents are masterpiece. you have performed a magnificent task on this subject!
Comentariu postate de Home Improvement Idea la data de 17.05.2019
Hi to all, how is everything, I think every one is getting more from this website, and your views are nice for new people.
Comentariu postate de Home Improvement Idea la data de 17.05.2019
Good respond in return of this issue with solid arguments and telling the whole thing on the topic of that.
Comentariu postate de Home Improvement Blogs la data de 17.05.2019
This is a very good tip particularly to those fresh to the blogosphere. Simple but very accurate information… Thanks for sharing this one. A must read article!
Comentariu postate de Kristie la data de 17.05.2019
Hello There. I found your blog using msn. This is a very well written article. I'll be sure to bookmark it and return to read more of your useful info. Thanks for the post. I'll definitely return.
Comentariu postate de Home Improvement Idea la data de 17.05.2019
Hey there this is somewhat of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I'm starting a blog soon but have no coding experience so I wanted to get guidance from someone with experience. Any help would be enormously appreciated!
Comentariu postate de signal app review la data de 17.05.2019
Do you have a spam problem on this blog; I also am a blogger, and I was wondering your situation; many of us have created some nice practices and we are looking to swap solutions with others, why not shoot me an e-mail if interested.
Comentariu postate de Home Improvement Blogs la data de 18.05.2019
It's amazing to visit this site and reading the views of all mates about this article, while I am also eager of getting knowledge.
Comentariu postate de Home Improvement Blogs la data de 18.05.2019
Hmm is anyone else having problems with the images on this blog loading? I'm trying to find out if its a problem on my end or if it's the blog. Any feedback would be greatly appreciated.
Comentariu postate de Home Improvement Blogs la data de 18.05.2019
If some one wants expert view about blogging afterward i propose him/her to go to see this weblog, Keep up the fastidious work.
Comentariu postate de coach outlet store la data de 18.05.2019
Thanks so much for giving everyone an exceptionally nice possiblity to read from this website. It's always very superb and as well , full of a great time for me personally and my office friends to visit your blog not less than thrice in 7 days to find out the newest guidance you will have. And of course, I am also certainly fulfilled concerning the gorgeous opinions served by you. Certain 3 areas in this post are clearly the simplest we've ever had.