RieqyNS13 Posted May 16, 2013 Share Posted May 16, 2013 how do send html format with gmail smtp ? I just know some commands of smtp gmail to manage email they are MAIL FROM and RCPT TO. does it can be combined with mail($to, $sbj, $msg, $hdrs) ? Quote Link to comment https://forums.phpfreaks.com/topic/278076-send-html-format-with-gmail-smtp/ Share on other sites More sharing options...
RieqyNS13 Posted May 16, 2013 Author Share Posted May 16, 2013 are this smtp commands is valid ? DATA From: "Edited Out" <editedout@yahoo.com> To: "Edited Out" <editedout@yahoo.com> Subject: Testing 4 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="boundary-type-1234567892-alt" --boundary-type-1234567890 --boundary-type-1234567892-alt Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Quote Link to comment https://forums.phpfreaks.com/topic/278076-send-html-format-with-gmail-smtp/#findComment-1430503 Share on other sites More sharing options...
jazzman1 Posted May 16, 2013 Share Posted May 16, 2013 (edited) The easiest way to achieve that is to use some php mail library as swiftmailer, phpMailer.... I'm a swiftmailer fan or you want to create everything from scratch? http://swiftmailer.org/docs/sending.html Edited May 16, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/278076-send-html-format-with-gmail-smtp/#findComment-1430506 Share on other sites More sharing options...
RieqyNS13 Posted May 16, 2013 Author Share Posted May 16, 2013 I want to learn about manage the smtp with php. I have read some of PHPMail class, but there too many lines code. so, I decided to ask to this forum to learn step by step. do you know slices code of $mail->getFile('contents.html') in phpMailer class ? I have not founded it in class.smtp.php and class.phpmailer.php Quote Link to comment https://forums.phpfreaks.com/topic/278076-send-html-format-with-gmail-smtp/#findComment-1430511 Share on other sites More sharing options...
jazzman1 Posted May 16, 2013 Share Posted May 16, 2013 I don't have a previous attempt with phpMailer, so I will give you an example in swiftMailer. It's a pretty simple: <?php // include the library require_once 'lib/swift_required.php'; // Create the Transport $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465) ->setUsername('your username') ->setPassword('your password') ; // Create the Mailer using your created Transport $mailer = Swift_Mailer::newInstance($transport); $subject = 'Wonderful Subject From SwiftMailer'; $body = '<p>Here is the message itself</p>'; // that's your html content $message = Swift_Message::newInstance() ->setContentType("text/html") ->setMaxLineLength(100) ->setSubject("' . $subject . '") ->setFrom(array("centos-box@localdomain" => "jazzman")) ->setBody($body); // Send the message $numSent = $mailer->send($message); printf("Sent %d messages\n", $numSent); Quote Link to comment https://forums.phpfreaks.com/topic/278076-send-html-format-with-gmail-smtp/#findComment-1430521 Share on other sites More sharing options...
jazzman1 Posted May 16, 2013 Share Posted May 16, 2013 (edited) Ops....I just forgot to add setTo() or if you prefer setBcc() methods. So the message should be something like: $message = Swift_Message::newInstance() ->setContentType("text/html") ->setMaxLineLength(100) ->setSubject($subject) ->setFrom(array("centos-box@localdomain" => "jazzman")) ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name')) ->setBody($body); That's all. Edited May 16, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/278076-send-html-format-with-gmail-smtp/#findComment-1430526 Share on other sites More sharing options...
jazzman1 Posted May 16, 2013 Share Posted May 16, 2013 (edited) So, here is the test using my local CentOS machine. Check the status of sendmail client to be sure that we don't use it: [jazz@centos-box ~]$ su -c '/etc/init.d/sendmail status' Password: sendmail is stopped sm-client is stopped Now I will try to use my google account sending a message(s) to my yahoo mail account. <?php // include the library require_once 'Swift-5.0.0/lib/swift_required.php'; date_default_timezone_set('America/Toronto'); // Create the Transport $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl') ->setUsername('accountName@gmail.com') ->setPassword('myPassword') ; // Create the Mailer using your created Transport $mailer = Swift_Mailer::newInstance($transport); $subject = 'Wonderful Subject From SwiftMailer'; $body = '<p>Here is the message itself</p>'; // that's your html content $message = Swift_Message::newInstance() ->setContentType("text/html") ->setMaxLineLength(100) ->setSubject($subject) ->setFrom(array("centos-box@localdomain" => "jazzman")) ->setTo(array('myEmail@yahoo.com' => 'jazzman')) ->setBody($body); // Send the message $numSent = $mailer->send($message); printf("Sent %d messages\n", $numSent); Result: from: jazzman <my_google_account@gmail.com>to: jazzman <my_yahoo_account@yahoo.com>date: Thu, May 16, 2013 at 4:42 PMsubject: Wonderful Subject From SwiftMailermailed-by: gmail.comsigned-by: gmail.com MIME-Version: 1.0Content-Type: text/html; charset=utf-8Content-Transfer-Encoding: quoted-printable<p>Here is the message itself</p> So, to use this method you don't have to have a mail server on the machine just we're using a remote mail server over ssl. Edited May 16, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/278076-send-html-format-with-gmail-smtp/#findComment-1430542 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.