Jump to content

Email using PHP


eskimo87

Recommended Posts

Hi All,

I have a website hosted at say, abc.com and I have database of registered users with their email id.

Now I want to run a logic and get a subset of the users, and send them an email once a month. Something similar to reminder mail.

As I see, this task is divided in three sub modules-

1.      A PHP script with a logic which will get me that subset of users

 

2.      A sendmail($email_id) kind of function which will do the work of sending mail to given address

 

3.      A way to schedule the execution of PHP script (of module#1) and sendmail function, so that email would be sent once in a month automatically

 

 

I have module#1 ready but I am stuck with module #2 and #3.

I am new to doing mail stuff using PHP, I want to know

-          what all things/settings I should be knowing to do send mail coding in PHP

 

-          I want to send all those reminder emails to users with [email protected] as sender , how to achieve that?

 

-          Any pointers to start off with this would be of great help

 

 

When I search on how to go about implementing module#3, I found that CRON is preferred way to implement scheduling. But I enquired to my hosting provider and got to know that It is not accessible to me. So please let me know any other alternate ways to implement the scheduling of a PHP script.

 

Thanks,

Ravi

Link to comment
https://forums.phpfreaks.com/topic/200038-email-using-php/
Share on other sites

eskimo87,

 

Google Found This:

 

http://www.phpjobscheduler.co.uk/

 

Also,

 

Here is a working email script:  (change from / to addresses )

 


<?php

$message = <<<EOM

 Hi,

 <br />
 <br />

<ul> I got the following from  Coop today:

<br />
<br />

<ul>"I have received junk email from my address to my address. When I check the source it lists my email address. I have no idea what is happening. Any ideas?<br />
Jim C."

</ul>
<br />

I sent him the following:
<br />

</ul>
<br />

Scot

<br />
<br />


EOM;

$recipient = '[email protected], ';
$from      = "<[email protected]>";
$name      = "[email protected]";
$replyto   = "[email protected]";
$subject   = $subject   = "Like this...";

$isJim = stristr($from, 'coop');

if ($isJim) {
	$message   .= '<br /><font color="red">***</font> <font color="blue">Coop sent this Spam ! <font color="red">***</font></font><br />';
}
else {
	$message   .= '<br /><font color="red">***</font> <font color="blue">Coop DID NOT send this Spam ! <font color="red">***</font></font><br />';
}

	$HTML = <<<EOH


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>myMailTest.php</title>
<style type="text/css">
<!--
body.hl	{ background-color:#ffffff; }
pre.hl	{ color:#205e75; background-color:#ffffff; font-size:11 pt; font-family:Courier New;}
.num	{ color:#ff0000; font-weight:normal; }
.esc	{ color:#ff00ff; font-weight:normal; }
.str	{ color:#00add9; font-weight:normal; }
.dstr	{ color:#0000ff; font-weight:normal; }
.slc	{ color:#cc0044; font-weight:normal; font-style:italic; }
.com	{ color:#cc0044; font-weight:normal; font-style:italic; }
.dir	{ color:#00b800; font-weight:normal; }
.sym	{ color:#800080; font-weight:normal; }
.line	{ color:#cc0044; font-weight:normal; }
.kwa	{ color:#4169e1; font-weight:normal; }
.kwb	{ color:#8080c0; font-weight:normal; }
.kwc	{ color:#0080c0; font-weight:normal; }
.kwd	{ color:#004466; font-weight:normal; }
/* ERROR: Could not include C:\WebServ\www\trunk\hlOverride.css.*/
//-->
</style>
</head>
<body class="hl">

<br />
<br />



<center> <h4>HERE IS THE CODE I USED TO GENERATE THE SPAM</h4></center>
<br />
<br />

<pre class="hl"><span class="sym"><</span>?php

    \$recipient <span class="sym">=</span> <span class="str">'ScotDiddle&#64;xxx.com, '</span><span class="sym">;</span>
    \$from      <span class="sym">=</span> <span class="str">"<coop&#64;xxx.com>"</span><span class="sym">;</span>
    \$name      <span class="sym">=</span> <span class="str">"coop&#64;xxx.com"</span><span class="sym">;</span>
    \$replyto   <span class="sym">=</span> <span class="str">"coop&#64;xxx.com"</span><span class="sym">;</span>
    \$subject   <span class="sym">=</span> \$subject   <span class="sym">=</span> <span class="str">"Like this..."</span><span class="sym">;</span>

    \$isJim <span class="sym">=</span> <span class="kwa">stristr</span><span class="sym">(</span>\$from<span class="sym">,</span> <span class="str">'coop'</span><span class="sym">);</span>

    <span class="kwa">if</span> <span class="sym">(</span>\$isJim<span class="sym">) {</span>
        \$message   <span class="sym">=</span> <span class="str">'<br /><font color="red">***</font> <font color="blue">Jim Cooper sent this Spam ! <font color="red">***</font></font><br />'</span><span class="sym">;</span>
    <span class="sym">}</span>
    <span class="kwa">else</span> <span class="sym">{</span>
        \$message   <span class="sym">=</span> <span class="str">'<br /><font color="red">***</font> <font color="blue">Jim Cooper DID NOT send this Spam ! <font color="red">***</font></font><br />'</span><span class="sym">;</span>
    <span class="sym">}</span>

    <span class="kwa">if</span> <span class="sym">(</span>\$recipient <span class="sym">!=</span> <span class="str">''</span><span class="sym">) {</span>
        \$mailRC1 <span class="sym">=</span> mail_attachment<span class="sym">(</span>\$recipient<span class="sym">,</span> \$from<span class="sym">,</span> \$name<span class="sym">,</span> \$replyto<span class="sym">,</span> \$subject<span class="sym">,</span> \$message<span class="sym">);</span>
    <span class="sym">}</span>

    <span class="kwa">echo</span> <span class="str">"<html></span> <span class="esc">\\n</span><span class="str">"</span><span class="sym">;</span>
    <span class="kwa">echo</span> <span class="str">"<body onLoad='javascript:scroll(0,0);'></span> <span class="esc">\\n</span> <span class="str">"</span><span class="sym">;</span>

    <span class="kwa">if</span> <span class="sym">(</span>\$mailRC1<span class="sym">) {</span>
        <span class="kwa">echo</span> <span class="str">"The Spam has been sent to: <br /><br /><ul></span><span class="esc">\"</span><span class="str">\$recipient</span><span class="esc">\"</span><span class="str"></ul><br /></br /></span> <span class="esc">\\n</span> <span class="str">"</span><span class="sym">;</span>
    <span class="sym">}</span>
    <span class="kwa">else</span> <span class="sym">{</span>
        <span class="kwa">echo</span> <span class="str">"The Spam has NOT been sent to: <br /><br /><ul></span><span class="esc">\"</span><span class="str">\$recipient</span><span class="esc">\"</span><span class="str"></ul><br /><h4><font color=</span><span class="esc">\"</span><span class="str">red</span><span class="esc">\"</span><span class="str">>Please inform the Help Desk about this error...</font></h4></span> <span class="esc">\\n</span><span class="str">"</span><span class="sym">;</span>
    <span class="sym">}</span>

    <span class="kwa">echo</span> <span class="str">"</body></span> <span class="esc">\\n</span><span class="str">"</span><span class="sym">;</span>
    <span class="kwa">echo</span> <span class="str">"</html></span> <span class="esc">\\n</span><span class="str">"</span><span class="sym">;</span>

    <span class="kwa">function</span> mail_attachment<span class="sym">(</span>\$mailto<span class="sym">,</span> \$from_mail<span class="sym">,</span> \$from_name<span class="sym">,</span> \$replyto<span class="sym">,</span> \$subject<span class="sym">,</span> \$message<span class="sym">) {</span>

        \$uid <span class="sym">=</span> <span class="kwa">md5</span><span class="sym">(</span><span class="kwa">uniqid</span><span class="sym">(</span><span class="kwa">time</span><span class="sym">()));</span>

        $<span class="kwa">header</span>  <span class="sym">=</span> <span class="str">"From: \$from_name</span><span class="esc">\\r\\n</span><span class="str">"</span><span class="sym">;</span>
        $<span class="kwa">header</span> .<span class="sym">=</span> <span class="str">"Reply-To: "</span>.\$replyto.<span class="str">"</span><span class="esc">\\r\\n</span><span class="str">"</span><span class="sym">;</span>

        $<span class="kwa">header</span> .<span class="sym">=</span> <span class="str">"MIME-Version: 1.0</span><span class="esc">\\r\\n</span><span class="str">"</span><span class="sym">;</span>
        $<span class="kwa">header</span> .<span class="sym">=</span> <span class="str">"Content-Type: multipart/mixed; boundary="</span>.\$uid.<span class="str">"</span><span class="esc">\\r\\n\\r\\n</span><span class="str">"</span><span class="sym">;</span>
        $<span class="kwa">header</span> .<span class="sym">=</span> <span class="str">"This is a multi-part message in MIME format.</span><span class="esc">\\r\\n\\n</span><span class="str">"</span><span class="sym">;</span>

        <span class="slc">// Here is the body</span>
        $<span class="kwa">header</span> .<span class="sym">=</span> <span class="str">"--"</span>.\$uid.<span class="str">"</span><span class="esc">\\r\\n</span><span class="str">"</span><span class="sym">;</span>
        $<span class="kwa">header</span> .<span class="sym">=</span> <span class="str">"Content-type:text/html; charset=iso-8859-1</span><span class="esc">\\r\\n</span><span class="str">"</span><span class="sym">;</span>
        $<span class="kwa">header</span> .<span class="sym">=</span> <span class="str">"Content-Transfer-Encoding: 7bit</span><span class="esc">\\r\\n\\r\\n</span><span class="str">"</span><span class="sym">;</span>
        $<span class="kwa">header</span> .<span class="sym">=</span> \$message.<span class="str">"</span><span class="esc">\\r\\n\\r\\n</span><span class="str">"</span><span class="sym">;</span>

        $<span class="kwa">header</span> .<span class="sym">=</span> <span class="str">"--"</span>.\$uid.<span class="str">"</span><span class="esc">\\r\\n</span><span class="str">"</span><span class="sym">;</span>

        <span class="kwa">if</span> <span class="sym">(</span><span class="kwa">mail</span><span class="sym">(</span>\$mailto<span class="sym">,</span> \$subject<span class="sym">,</span> <span class="str">""</span><span class="sym">,</span> $<span class="kwa">header</span><span class="sym">)) {</span>
            <span class="kwa">return</span> TRUE<span class="sym">;</span>
        <span class="sym">}</span>
        <span class="kwa">else</span> <span class="sym">{</span>
            <span class="kwa">return</span> FALSE<span class="sym">;</span>
        <span class="sym">}</span>

    <span class="sym">}</span>


    <span class="com">/**</span>
<span class="com">     *</span>
<span class="com">     *  "So long, and thanks for all of the fish"</span>
<span class="com">     *</span>
<span class="com">     */</span>

?<span class="sym">></span>
</pre>
</body>
</html>
<!--HTML generated by highlight 2.4.7, http://www.andre-simon.de/-->

EOH;

	$message .= $HTML;

if ($recipient != '') {
	$mailRC1 = mail_attachment($recipient, $from, $name, $replyto, $subject, $message);
}

echo "<html> \n";
echo "<body onLoad='javascript:scroll(0,0);'> \n ";

if ($mailRC1) {
	echo "The Email has been sent to: <br /><br /><ul>\"$recipient\"</ul><br /></br /> \n ";
}
else {
	echo "The Email has NOT been sent to: <br /><br /><ul>\"$recipient\"</ul><br /><h4><font color=\"red\">Please inform the Help Desk about this error...</font></h4> \n";
}

echo "</body> \n";
echo "</html> \n";

function mail_attachment($mailto, $from_mail, $from_name, $replyto, $subject, $message) {

	$uid = md5(uniqid(time()));

	$header  = "From: $from_name\r\n";
	$header .= "Reply-To: ".$replyto."\r\n";

	$header .= "MIME-Version: 1.0\r\n";
	$header .= "Content-Type: multipart/mixed; boundary=".$uid."\r\n\r\n";
	$header .= "This is a multi-part message in MIME format.\r\n \n";

	// Here is the body
	$header .= "--".$uid."\r\n";
	$header .= "Content-type:text/html; charset=iso-8859-1\r\n";
	$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";


	$header .= $message."\r\n\r\n";



	$header .= "--".$uid."\r\n";

	if (mail($mailto, $subject, "", $header)) {
		return TRUE;
	}
	else {
			return FALSE;
	}

}


/**
 *
 *  "So long, and thanks for all of the fish"
 *
 */

?>

 

Scot L. Diddle, Richmond VA

Link to comment
https://forums.phpfreaks.com/topic/200038-email-using-php/#findComment-1049933
Share on other sites

<shameless plug>

I built an open-source system to send e-mail for you. It's not automated (yet), but it has all the RFC stuff already built in (opt-in/opt-out), multi-user support, and you can modify the code so you can have it grab your users, or I can write a script to import them into my scripts database.  Remember, It's open source. Free as in beer and freedom:

http://sourceforge.net/projects/mailgroup/

</shameless plug>

Link to comment
https://forums.phpfreaks.com/topic/200038-email-using-php/#findComment-1050061
Share on other sites

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.