50Webs Forum
Welcome, Guest. Please login or register.
Did you miss your activation email?
January 25, 2021, 06:27:48 AM

Login with username, password and session length
Search:     Advanced search
93037 Posts in 22981 Topics by 28977 Members
Latest Member: ontimetaxi
* Home Help Search Login Register
+  50Webs Forum
|-+  50Webs Services
| |-+  Support questions (Paid plans).
| | |-+  Cannot send php mail to gmail account
« previous next »
Pages: [1] Go Down Print
Author Topic: Cannot send php mail to gmail account  (Read 3629 times)
JEfromCanada
Members
**
Posts: 118


View Profile
« on: March 10, 2008, 04:58:08 AM »

There appears to be lots of problems using mail() to send to a gmail address.  I've researched the web and can't find a suitable solution.

Can anyone here offer a PHP script that can send from the 50webs.net SMTP server to a gmail account.

Thanks for the help.



Jack
Logged
XYLEM
Administrator
Experienced Member
*****
Posts: 7440


View Profile
« Reply #1 on: March 10, 2008, 08:29:50 AM »

Try this :

Code:
<?
$from = "From: You <you@yourdomain.com>";
$to = "you@yourdomain.com";
$subject = "Hi! ";
$body = "TEST";

if(mail($to,$subject,$body,$from)) echo "MAIL - OK";
else echo "MAIL FAILED";
?>


Regards
Gazurkoff
Logged

JEfromCanada
Members
**
Posts: 118


View Profile
« Reply #2 on: March 11, 2008, 06:49:36 PM »

I'm a bit surprised that this simple solution worked.  Now, I'll ask if you've successfully sent a multi-part message (plain text + HTML) to a gmail address.

I don't need to send attachments, but do need to send plain vs. HTML versions of the mail.

Here is the code I've been using.  It works with all recipients except Gmail.  When sent to GMail users, the message is received, but the contents are not displayed.

Code:

<?php
function send_mail&#40;$emailaddress, $body, $emailsubject, $fromaddress, $fromname, $fileattach = false&#41; 
&#123;

//define the receiver of the email
$to "$emailaddress";
//define the subject of the email
$subject "$emailsubject";
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash md5&#40;date&#40;'r', time&#40;&#41;&#41;&#41;;
//define the headers we want passed. Note that they are separated with \r\n
$headers "From&#58; $fromname<$fromaddress>\r\nReply-To&#58; $fromname<$fromaddress>";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type&#58; multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";

$stripped strip_tags&#40;str_replace&#40;"<br>", "\r\n", $body&#41;&#41;;
//$stripped = "This is stripped";

//define the body of the message.
ob_start&#40;&#41;; //Turn on output buffering
?>

--PHP-alt-<?php echo "$random_hash\n"?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<?php echo $stripped?>

--PHP-alt-<?php echo "$random_hash\n"?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<?php echo $body?>

--PHP-alt-<?php echo $random_hash?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
return $mail_sent;
}

$resultset = TraverseData();  // This creates the HTML body of the message
send_mail("recipient@gmail.com", $resultset, "Results of reminder check", "sentby@domainregisteredwith50websnet.com", "Sent by string", false)

?>

Logged
XYLEM
Administrator
Experienced Member
*****
Posts: 7440


View Profile
« Reply #3 on: March 11, 2008, 08:16:33 PM »

monkey56657 or SawanBanna can help here with this PHP code.

Regards
Gazurkoff
Logged

freshwire
Global Moderators
Experienced Member
*****
Posts: 6072



View Profile WWW
« Reply #4 on: March 11, 2008, 08:28:07 PM »

I'll take a look later once I have more time. Need to lookup on the email structure for multiple content type.
Logged

Jonathan | 50Webs Support Volunteer
sawanbanna
Administrator
Experienced Member
*****
Posts: 7607



View Profile WWW
« Reply #5 on: March 11, 2008, 08:58:04 PM »

I could get it to show either, however when I tried on multi-part it's working on html

Code:
<?php
function send_mail&#40;$emailaddress, $body, $emailsubject, $fromaddress, $fromname, $fileattach = false&#41;
&#123;

//define the receiver of the email
$to "$emailaddress";
//define the subject of the email
$subject "$emailsubject";
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash md5&#40;date&#40;'r', time&#40;&#41;&#41;&#41;;
//define the headers we want passed. Note that they are separated with \r\n
$headers "From&#58; $fromname<$fromaddress>\r\nReply-To&#58; $fromname<$fromaddress>";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type&#58; multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";

$stripped strip_tags&#40;str_replace&#40;"<br>", "\r\n", $body&#41;&#41;;
//$stripped = "This is stripped";

//define the body of the message.
ob_start&#40;&#41;; //Turn on output buffering
?>

--PHP-alt-<?php echo "$random_hash\n"?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<?php echo $stripped?>

--PHP-alt-<?php echo "$random_hash\n"?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Content-Type: multipart/mixed; boundary="_----------=_10167391557129230"
MIME-Version: 1.0


<?php echo $body?>

--PHP-alt-<?php echo $random_hash?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
return $mail_sent;
}

$resultset = TraverseData();  // This creates the HTML body of the message
send_mail("recipient@gmail.com", $resultset, "Results of reminder check", "sentby@domainregisteredwith50websnet.com", "Sent by string", false)

?>
also using "quoted-printable" is working instead of "7bit"
Logged

Best Regards,
sawanbanna
50Webs Support Volunteer

-="DO NOT ADJUST YOUR MIND, THERE IS A FAULT IN REALITY"=-
JEfromCanada
Members
**
Posts: 118


View Profile
« Reply #6 on: March 11, 2008, 11:05:42 PM »

Thanks for taking time to help.  I tried your code, substituting hard-coded HTML for my message content instead of using my "TraverseData()" function.

When I sent the email to my gmail address, it was readable, but only in plain text.  None of the enhanced formatting showed up, and the "A HREF" links were not clickable.
Logged
Pages: [1] Go Up Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.14 | SMF © 2006-2011, Simple Machines LLC Valid XHTML 1.0! Valid CSS!