Jump to content

send html format with gmail smtp


RieqyNS13

Recommended Posts

are this smtp commands is valid ?

DATA
From: "Edited Out" <[email protected]>
To: "Edited Out" <[email protected]>
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


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 

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);

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('[email protected]', '[email protected]' => 'A name'))
        ->setBody($body);

That's all.

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('[email protected]')
  ->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('[email protected]' => 'jazzman'))
        ->setBody($body);

// Send the message
$numSent = $mailer->send($message);

printf("Sent %d messages\n", $numSent);

Result:

 

from:     jazzman <[email protected]>
to:         jazzman <[email protected]>
date:    Thu, May 16, 2013 at 4:42 PM
subject:  Wonderful Subject From SwiftMailer
mailed-by:     gmail.com
signed-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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.