其他分享
首页 > 其他分享> > 代码发送了两次,没有正确的图像

代码发送了两次,没有正确的图像

作者:互联网

我从帖子中尝试了此代码,但是此代码在一次运行文件时发送了两封电子邮件.

Email file is sending email two times using php mail function

让我知道我做错了-

<?php
function mytextoverimage( $mytext ) {
$headurl = 'http://dummyimage.com/600x400/ffffee/00d5ff.jpg';
header('Content-type: image/jpeg');
$jpg_image = imagecreatefromjpeg($headurl);
$black = imagecolorallocate($jpg_image, 1, 1, 1);
$font_path = 'myfont/arial.ttf';
$text = $mytext;
imagettftext($jpg_image, 24, 0, 175, 85, $black, $font_path, $text);
imagejpeg($jpg_image);
imagedestroy($jpg_image);
}

$to = "myemail@gmail.com";
$subject = "This is a image conversion from Developer Zone";
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= 'From: developer@phpdev.com' . "\r\n" .
'Reply-To: testabc@testabc.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Title</title>
</head>
<body>
<table width="100%" cellspacing="5" cellpadding="0" border="0" bgcolor="#f0f0f0" style="color:666666;text-align:left; font:12px Verdana, Geneva, sans-serif">
<tr>
<td >'.mytextoverimage('Developer').'</td></tr></table></body></html>';

    mail($to,$subject,$message,$headers); die;

让我知道我做错了什么,这是我使用的正确方法吗?

<img src="'.mytextoverimage('Developer').'" />

我遵循了此URL,但是很难从此页面获得任何帮助-http://php.net/manual/en/function.imagejpeg.php

我什至尝试将该方法mytextoverimage()保存在另一个文件中,但仍然没有帮助,电子邮件发送了两次:(

解决方法:

您的mytextoverimage()函数不返回任何内容-只是将jpeg图片发送到浏览器.

我对您的代码进行了重新设计,以通过电子邮件发送相同的图像-请注意,仅发送了图像,没有HMTL.

如果您想将图像作为HTML文档的一部分发送,则需要更进一步并创建多部分消息-签出How to attach and show image in mail using php?

这适用于Iceweasel 10.0.11上的Gmail.

<?php
function mytextoverimage( $mytext )
{
    $headurl = 'http://dummyimage.com/600x400/ffffee/00d5ff.jpg';
    $jpg_image = imagecreatefromjpeg($headurl);
    $black = imagecolorallocate($jpg_image, 1, 1, 1);
    $font_path = 'myfont/arial.ttf';
    $text = $mytext;
    imagettftext($jpg_image, 24, 0, 175, 85, $black, $font_path, $text);
    ob_start(); //Get the image data from the output buffer
    imagejpeg($jpg_image);
    imagedestroy($jpg_image);
    return chunk_split(base64_encode(ob_get_clean())); //return the image data, encoded for email transfer
}

$to = "myemail@gmail.com";
$subject = "This is a image conversion from Developer Zone";
// --- Note the change from text/html to image/jpeg ---
$headers = "Content-type: image/jpeg;\r\n";
//$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= 'From: developer@phpdev.com' . "\r\n" .
'Reply-To: testabc@testabc.com' . "\r\n" .
'Content-Transfer-Encoding: base64' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = mytextoverimage('Developer');

    mail($to,$subject,$message,$headers); die;

标签:image,php-gd,php
来源: https://codeday.me/bug/20191031/1975924.html